Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.runner.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
verbose: true,
modulePathIgnorePatterns: ['package.json'],
transform: {
'^.+\\.[t|j]sx?$': 'babel-jest',
'^.+\\.[t|j]sx?$': '<rootDir>/jest.transformer.js',
},
reporters: [],
}
49 changes: 49 additions & 0 deletions jest.transformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Transforms solutions and test files for Jest.
//
// Most files go through SWC. It produces the same CommonJS as Babel, but it
// is written in Rust and loads and runs much faster than Babel.
//
// Babel still handles two cases, so that every test passes or fails exactly
// as it did with Babel alone:
//
// - Files that call jest.mock (or a related call). SWC does not move these
// calls above the imports when `jest` is imported from '@jest/globals'.
// - Files that SWC fails to compile. Babel then compiles them, or reports the
// same syntax error as before.
//
// Babel is only loaded when one of these cases comes up.
const swcJest = require('@swc/jest')

const HOISTED_JEST_CALL =
/\bjest\s*\.\s*(mock|unmock|enableAutomock|disableAutomock|deepUnmock)\s*\(/

const swc = swcJest.createTransformer({
swcrc: false,
env: {
targets: { node: process.versions.node },
mode: 'usage',
coreJs: '3.44',
},
})

let babel = null
function getBabel() {
if (!babel) {
babel = require('babel-jest').default.createTransformer()
}
return babel
}

module.exports = {
process(sourceText, sourcePath, options) {
if (HOISTED_JEST_CALL.test(sourceText)) {
return getBabel().process(sourceText, sourcePath, options)
}

try {
return swc.process(sourceText, sourcePath, options)
} catch {
return getBabel().process(sourceText, sourcePath, options)
}
},
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"@jest/reporters": "^30.4.0",
"@jest/test-result": "^30.4.0",
"@jest/types": "^30.4.0",
"@swc/core": "^1.16.22",
"@swc/jest": "^0.2.39",
"@typescript-eslint/typescript-estree": "^8.37.0",
"@typescript-eslint/visitor-keys": "^8.37.0",
"babel-jest": "^30.4.0",
Expand Down
179 changes: 179 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions test/fixtures/two-fer/fail/empty/expected_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
{
"name": "twoFer() > no name given",
"status": "fail",
"message": "TypeError: (0 , _twoFer.twoFer) is not a function",
"message": "TypeError: (0 , _twofer.twoFer) is not a function",
"output": null,
"test_code": "expect(twoFer()).toEqual('One for you, one for me.');"
},
{
"name": "twoFer() > a name given",
"status": "fail",
"message": "TypeError: (0 , _twoFer.twoFer) is not a function",
"message": "TypeError: (0 , _twofer.twoFer) is not a function",
"output": null,
"test_code": "const name = 'Alice';\nexpect(twoFer(name)).toEqual('One for Alice, one for me.');"
},
{
"name": "twoFer() > another name given",
"status": "fail",
"message": "TypeError: (0 , _twoFer.twoFer) is not a function",
"message": "TypeError: (0 , _twofer.twoFer) is not a function",
"output": null,
"test_code": "const name = 'Bob';\nexpect(twoFer(name)).toEqual('One for Bob, one for me.');"
}
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/two-fer/fail/tests/expected_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"name": "twoFer() > no name given",
"status": "pass",
"message": "",
"output": "[log] undefined",
"output": "[log] Some global log\n[log] undefined",
"test_code": "expect(twoFer()).toEqual('One for you, one for me.');"
},
{
"name": "twoFer() > a name given",
"status": "fail",
"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",
"output": "[log] Some global log\n[log] Alice",
"output": "[log] Alice",
"test_code": "const name = 'Alice';\nexpect(twoFer(name)).toEqual('One for Alice, one for me.');"
},
{
Expand Down
Loading