jest icon indicating copy to clipboard operation
jest copied to clipboard

Config file doesn't get picked up

Open mbrowne opened this issue 4 years ago • 4 comments

When I used the example config in the readme, I got an error:

SyntaxError: Cannot use import statement outside a module

It turned out this was because it wasn't reading the config file. If I add the configFile option, then it works:

'^.+\\.(t|j)sx?$': ['@swc/jest', { configFile: '.swcrc' }],

Oddly, it also works if I keep the array syntax but change the regex (since I don't need to support .jsx or .ts files in my repo): '^.+\\.(js)$': ['@swc/jest'],

But if I take away the brackets then it stops working:

'^.+\\.(js)$': '@swc/jest',

I'm running @swc/[email protected] and [email protected]

mbrowne avatar Aug 17 '21 18:08 mbrowne

I'm getting this error after upgrading from 0.1.4 to 0.2.2

It seems like @swc/jest used to output cjs but after the upgrade, it starts to output esm?

EDIT: Apologies. My issue has nothing to do with version change. I removed the roots field in the jest.config.js after upgrade. After adding the roots back, it starts to work as expected.

chuanqisun avatar Aug 21 '21 20:08 chuanqisun

Strange things happening for me as well.

With '\\.(js|jsx|ts|tsx)$': ['@swc/jest'] and '\\.(js|jsx|ts|tsx)$': '@swc/jest' I get these errors:

Failed to deserialize argument at `2` as swc::config::Options
    JSON: {"0":{"test":".*.jsx?$","jsc":{"parser":{"dynamicImport":true,"jsx":true,"syntax":"ecmascript"},"target":"es2016"}},"1":{"test":".*.tsx?$","jsc":{"parser":{"dynamicImport":true,"syntax":"typescript","tsx":true},"target":"es2016"}},"jsc":{"transform":{"hidden":{"jest":true}}},"filename":"/Users/me/myproject/jest.init.js"}

    Caused by:
        unknown field `0` at line 1 column 334

And with '\\.(js|jsx|ts|tsx)$': ['@swc/jest', { configFile: '.swcrc' }], I get these errors instead:

/Users/me/myproject/jest.init.js:4
    import 'react-dates/initialize';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

So far I haven't been able to find a way to make @swc/jest work in my project. My project doesn't have roots defined.

UPDATE: I was able to fix this by upgrading from Jest 24 to Jest 27. I guess @swc/jest doesn't support the older Jest versions.

EvHaus avatar Sep 07 '21 06:09 EvHaus

This config seems to work for me

module.exports = {
  preset: "ts-jest",
  verbose: true,
  testEnvironment: "jsdom",
  roots: ["<rootDir>/src/"],
  setupFilesAfterEnv: ["@testing-library/jest-dom/extend-expect"],
  transform: {
    "^.+\\.(t|j)sx?$": ["@swc/jest", { configFile: "./swcrc.json" }],
  },
  moduleNameMapper: {
    "\\.(css|less|scss|sss|styl)$": "<rootDir>/node_modules/identity-obj-proxy",
  },
};

floydjones1 avatar Sep 11 '21 22:09 floydjones1

For my case, I just figured out that @swc compile the output to es6, switch to commonjs solve it.

const swcConfigs = {
  module: {
    type: 'commonjs',
  },
}

module.exports = {
  transform: {
    '^.+\\.(t|j)sx?$': ['@swc/jest', swcConfigs],
  },
}

cloudle avatar Sep 18 '21 23:09 cloudle