Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest: The coverage percent of babel-jest and @swc/jest are different. #230

Closed
Sh031224 opened this issue Nov 10, 2023 · 1 comment
Closed

Comments

@Sh031224
Copy link

Problem

I are migrating from babel-jest to @swc/jest.
And I confirmed that the test worked successfully, but I found that the coverage was different from when using babel-jest.

I wrote a new function for simple reproduction.

// index.ts
export const anyFunction = (): number => {
  if (Math.random() > 0.5) {
    return 1;
  }

  return 0;
};
// index.test.ts
import { anyFunction } from '.';

jest.mock('.');

test('testing mock', () => {
  (anyFunction as jest.Mock).mockImplementation(() => 3);

  expect(anyFunction()).toBe(3);
});

And then run the command: jest --coverage

Result

Each has a different percentage of statements and functions.

@swc/jest

image

babel-jest

image

Config

swc

// jest.config.js
const fs = require('node:fs');

const swcConfig = JSON.parse(fs.readFileSync('./.swcrc'));

module.exports = {
  coverageDirectory: 'coverage',
  moduleDirectories: ['node_modules'],
  moduleFileExtensions: ['js',  ts'],
  transform: {
    '^.+\\.(j|t)sx?$': ['@swc/jest', { ...swcConfig }],
  },
};
// .swcrc
{
  "$schema": "https://json.schemastore.org/swcrc",
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "decorators": false
    },
    "loose": true,
    "externalHelpers": true
  },
  "module": {
    "type": "commonjs"
  },
  "minify": true
}

babel

// babel.config.js
module.exports = {
  presets: [
    [
      '@babel/env',
      {
        modules: false,
      },
    ],
    '@babel/preset-typescript',
  ],
  plugins: [
    '@babel/plugin-transform-modules-commonjs',
  ],
};
// jest.config.js
module.exports = {
  coverageDirectory: 'coverage',
  moduleDirectories: ['node_modules'],
  moduleFileExtensions: ['js',  ts'],
  transform: {
    '^.+\\.(j|t)sx?$': 'babel-jest',
  },
};
@Sh031224
Copy link
Author

moved: swc-project/jest#166

@Sh031224 Sh031224 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant