[SWC plugin] workaround for jest

Crates.io npm

Test with @swc/core@latest

This is a SWC plugin to handle jest compatibility issues.

This SWC plugin should be used with @swc/jest.

usage

install

bash npm i -D jest @swc/core @swc/jest jest_workaround

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

const swcrc = JSON.parse(fs.readFileSync(".swcrc", "utf8"));

// If you have other plugins, change this line. ((swcrc.jsc ??= {}).experimental ??= {}).plugins = [["jest_workaround", {}]];

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

Make sure that module.type is commonjs in your .swcrc since this plugin does not touch non-workaround parts, such as import statements.

FAQ

1. When do I need this?

If you're using the swc compiler to transform your code to comply with the ESM specification, but you're also using Jest to test it in a CJS environment, you may encounter issues due to the immutable issue of exports.

This plugin can help by transforming the export statements into mutable exports.

2. Do I have a better choice?

You may have other options depending on your specific needs:

These options may be worth considering before using this plugin.

CJS syntax

JavaScript exports.foo = function foo() { return 42; };

CTS(CJS in TypeScript) syntax

TypeScript export = { foo: function foo() { return 42; }, };

Notes:

3. After upgrading the plugin version, the changes have not taken effect.

This is a known issue. You could remove the Jest cache by running jest --clearCache as a workaround.