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

The coverage percent of babel-jest and @swc/jest are different #166

Closed
Sh031224 opened this issue Nov 10, 2023 · 2 comments
Closed

The coverage percent of babel-jest and @swc/jest are different #166

Sh031224 opened this issue Nov 10, 2023 · 2 comments

Comments

@Sh031224
Copy link

Sh031224 commented Nov 10, 2023

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',
  },
};
@kdy1
Copy link
Member

kdy1 commented Nov 10, 2023

swc-project/swc#3854

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 10, 2023
@kdy1
Copy link
Member

kdy1 commented Nov 10, 2023

Btw, your test is wrong according to the ESM spec

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

2 participants