Crates.io | jest_workaround |
lib.rs | jest_workaround |
version | 0.79.19 |
source | src |
created_at | 2022-07-14 17:18:41.693956 |
updated_at | 2023-07-19 05:43:07.159944 |
description | [SWC plugin] workaround for jest |
homepage | https://github.com/magic-akari/jest_workaround |
repository | https://github.com/magic-akari/jest_workaround |
max_upload_size | |
id | 625797 |
size | 134,283 |
This is a SWC plugin to handle jest compatibility issues.
This SWC plugin should be used with @swc/jest
.
https://swc.rs/docs/plugin/selecting-swc-core
install
npm i -D jest @swc/core @swc/jest jest_workaround
// 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.
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
.
You may have other options depending on your specific needs:
If you're able to run Jest in an ESM environment, you can use swc to transpile
TypeScript/JSX syntax or downgrade JavaScript syntax without module
conversion. Simply set the value of module.type
to es6
to achieve this.
It's possible that some issues related to running Jest in an ESM environment will be resolved over time. Keep an eye on facebook/jest#9430 for updates.
If you don't need the behavior of ESM specifically, you can stick with the CJS
syntax to get the CJS behavior of exports
.
These options may be worth considering before using this plugin.
CJS syntax
exports.foo = function foo() {
return 42;
};
CTS(CJS in TypeScript) syntax
export = {
foo: function foo() {
return 42;
},
};
Notes:
This is a known issue. You could remove the Jest cache by running
jest --clearCache
as a workaround.