@swc/jest
SWC binding for Jest.
Installation
# if you use npm
npm i -D jest @swc/core @swc/jest
# if you use yarn
yarn add -D jest @swc/core @swc/jest
Usage
jest.config.js:
module.exports = {
transform: {
'^.+\\.(t|j)sx?
It will load the SWC configuration from .swcrc by default. You also can customize it:
const fs = require('fs')
const config = JSON.parse(fs.readFileSync(`${__dirname}/.swcrc`, 'utf-8'))
module.exports = {
transform: {
'^.+\\.(t|j)sx?
Q & A
Q: Jest uses CommonJS by default. But I want to use ESM.
A: Setup Jest following this Guide.
For JavaScript, edit package.json as follows:
{
// ...
"type": "module"
}
For TypeScript, edit jest.config.js as follows:
module.exports = {
// ...
extensionsToTreatAsEsm: ['.ts', '.tsx'],
}
Run test with --experimental-vm-modules:
cross-env NODE_OPTIONS=--experimental-vm-modules jest
# or
node --experimental-vm-modules ./node_modules/jest/bin/jest.js
Q: What ECMAScript target is set by jsc.target?
A: By default, the version supported by your Node runtime.
Node version
Default jsc.target
12
'es2018'
13
'es2019'
14
'es2020'
15
'es2021'
16
'es2021'
17
'es2022'
18
'es2023'
19
'es2023'
20
'es2023'
You can customize this by setting an explicit version in jest.config.js:
module.exports = {
transform: {
"^.+\\.(t|j)sx?$": [
"@swc/jest",
{
jsc: {
target: "es2021",
},
},
],
},
}
License
MIT
: '@swc/jest',
},
}
It will load the SWC configuration from __INLINE_CODE_1__ by default. You also can customize it:
__CODE_BLOCK_2__
Q & A
Q: Jest uses CommonJS by default. But I want to use ESM.
A: Setup Jest following this Guide.
For JavaScript, edit __INLINE_CODE_2__ as follows:
__CODE_BLOCK_3__
For TypeScript, edit __INLINE_CODE_3__ as follows:
__CODE_BLOCK_4__
Run test with __INLINE_CODE_4__:
__CODE_BLOCK_5__
Q: What ECMAScript target is set by __INLINE_CODE_5__?
A: By default, the version supported by your Node runtime.
Node version
Default __INLINE_CODE_6__
12
'es2018'
13
'es2019'
14
'es2020'
15
'es2021'
16
'es2021'
17
'es2022'
18
'es2023'
19
'es2023'
20
'es2023'
You can customize this by setting an explicit version in __INLINE_CODE_7__:
__CODE_BLOCK_6__
License
MIT
: ['@swc/jest', { ...config, /* custom configuration in Jest */ }],
},
}
Q & A
Q: Jest uses CommonJS by default. But I want to use ESM.
A: Setup Jest following this Guide.
For JavaScript, edit __INLINE_CODE_2__ as follows:
__CODE_BLOCK_3__For TypeScript, edit __INLINE_CODE_3__ as follows:
__CODE_BLOCK_4__Run test with __INLINE_CODE_4__:
__CODE_BLOCK_5__Q: What ECMAScript target is set by __INLINE_CODE_5__?
A: By default, the version supported by your Node runtime.
| Node version | Default __INLINE_CODE_6__ |
|---|---|
| 12 | 'es2018' |
| 13 | 'es2019' |
| 14 | 'es2020' |
| 15 | 'es2021' |
| 16 | 'es2021' |
| 17 | 'es2022' |
| 18 | 'es2023' |
| 19 | 'es2023' |
| 20 | 'es2023' |
You can customize this by setting an explicit version in __INLINE_CODE_7__:
__CODE_BLOCK_6__License
MIT
: '@swc/jest', }, }It will load the SWC configuration from __INLINE_CODE_1__ by default. You also can customize it:
__CODE_BLOCK_2__Q & A
Q: Jest uses CommonJS by default. But I want to use ESM.
A: Setup Jest following this Guide.
For JavaScript, edit __INLINE_CODE_2__ as follows:
__CODE_BLOCK_3__For TypeScript, edit __INLINE_CODE_3__ as follows:
__CODE_BLOCK_4__Run test with __INLINE_CODE_4__:
__CODE_BLOCK_5__Q: What ECMAScript target is set by __INLINE_CODE_5__?
A: By default, the version supported by your Node runtime.
| Node version | Default __INLINE_CODE_6__ |
|---|---|
| 12 | 'es2018' |
| 13 | 'es2019' |
| 14 | 'es2020' |
| 15 | 'es2021' |
| 16 | 'es2021' |
| 17 | 'es2022' |
| 18 | 'es2023' |
| 19 | 'es2023' |
| 20 | 'es2023' |
You can customize this by setting an explicit version in __INLINE_CODE_7__:
__CODE_BLOCK_6__License
MIT