Skip to content

Commit 3925aa1

Browse files
iHiDclaude
authored andcommitted
Transform files with SWC, falling back to Babel
SWC loads and converts files much faster than Babel. Babel still handles files that call jest.mock and files SWC can't compile. Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f8844c3 commit 3925aa1

6 files changed

Lines changed: 236 additions & 6 deletions

File tree

‎jest.runner.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
verbose: true,
33
modulePathIgnorePatterns: ['package.json'],
44
transform: {
5-
'^.+\\.[t|j]sx?$': 'babel-jest',
5+
'^.+\\.[t|j]sx?$': '<rootDir>/jest.transformer.js',
66
},
77
reporters: [],
88
}

‎jest.transformer.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Transforms solutions and test files for Jest.
2+
//
3+
// Most files go through SWC. It produces the same CommonJS as Babel, but it
4+
// is written in Rust and loads and runs much faster than Babel.
5+
//
6+
// Babel still handles two cases, so that every test passes or fails exactly
7+
// as it did with Babel alone:
8+
//
9+
// - Files that call jest.mock (or a related call). SWC does not move these
10+
// calls above the imports when `jest` is imported from '@jest/globals'.
11+
// - Files that SWC fails to compile. Babel then compiles them, or reports the
12+
// same syntax error as before.
13+
//
14+
// Babel is only loaded when one of these cases comes up.
15+
const swcJest = require('@swc/jest')
16+
17+
const HOISTED_JEST_CALL =
18+
/\bjest\s*\.\s*(mock|unmock|enableAutomock|disableAutomock|deepUnmock)\s*\(/
19+
20+
const swc = swcJest.createTransformer({
21+
swcrc: false,
22+
env: {
23+
targets: { node: process.versions.node },
24+
mode: 'usage',
25+
coreJs: '3.44',
26+
},
27+
})
28+
29+
let babel = null
30+
function getBabel() {
31+
if (!babel) {
32+
babel = require('babel-jest').default.createTransformer()
33+
}
34+
return babel
35+
}
36+
37+
module.exports = {
38+
process(sourceText, sourcePath, options) {
39+
if (HOISTED_JEST_CALL.test(sourceText)) {
40+
return getBabel().process(sourceText, sourcePath, options)
41+
}
42+
43+
try {
44+
return swc.process(sourceText, sourcePath, options)
45+
} catch {
46+
return getBabel().process(sourceText, sourcePath, options)
47+
}
48+
},
49+
}

‎package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
"@jest/reporters": "^30.4.0",
3434
"@jest/test-result": "^30.4.0",
3535
"@jest/types": "^30.4.0",
36+
"@swc/core": "^1.16.22",
37+
"@swc/jest": "^0.2.39",
3638
"@typescript-eslint/typescript-estree": "^8.37.0",
3739
"@typescript-eslint/visitor-keys": "^8.37.0",
3840
"babel-jest": "^30.4.0",

‎pnpm-lock.yaml‎

Lines changed: 179 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/two-fer/fail/empty/expected_results.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
{
55
"name": "twoFer() > no name given",
66
"status": "fail",
7-
"message": "TypeError: (0 , _twoFer.twoFer) is not a function",
7+
"message": "TypeError: (0 , _twofer.twoFer) is not a function",
88
"output": null,
99
"test_code": "expect(twoFer()).toEqual('One for you, one for me.');"
1010
},
1111
{
1212
"name": "twoFer() > a name given",
1313
"status": "fail",
14-
"message": "TypeError: (0 , _twoFer.twoFer) is not a function",
14+
"message": "TypeError: (0 , _twofer.twoFer) is not a function",
1515
"output": null,
1616
"test_code": "const name = 'Alice';\nexpect(twoFer(name)).toEqual('One for Alice, one for me.');"
1717
},
1818
{
1919
"name": "twoFer() > another name given",
2020
"status": "fail",
21-
"message": "TypeError: (0 , _twoFer.twoFer) is not a function",
21+
"message": "TypeError: (0 , _twofer.twoFer) is not a function",
2222
"output": null,
2323
"test_code": "const name = 'Bob';\nexpect(twoFer(name)).toEqual('One for Bob, one for me.');"
2424
}

‎test/fixtures/two-fer/fail/tests/expected_results.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
"name": "twoFer() > no name given",
66
"status": "pass",
77
"message": "",
8-
"output": "[log] undefined",
8+
"output": "[log] Some global log\n[log] undefined",
99
"test_code": "expect(twoFer()).toEqual('One for you, one for me.');"
1010
},
1111
{
1212
"name": "twoFer() > a name given",
1313
"status": "fail",
1414
"message": "Error: \u001b[2mexpect(\u001b[22m\u001b[31mreceived\u001b[39m\u001b[2m).\u001b[22mtoEqual\u001b[2m(\u001b[22m\u001b[32mexpected\u001b[39m\u001b[2m) // deep equality\u001b[22m\n\nExpected: \u001b[32m\"One for \u001b[7mAlice\u001b[27m, one for me.\"\u001b[39m\nReceived: \u001b[31m\"One for \u001b[7myou\u001b[27m, one for me.\"\u001b[39m",
15-
"output": "[log] Some global log\n[log] Alice",
15+
"output": "[log] Alice",
1616
"test_code": "const name = 'Alice';\nexpect(twoFer(name)).toEqual('One for Alice, one for me.');"
1717
},
1818
{

0 commit comments

Comments
 (0)