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: [],
}
51 changes: 51 additions & 0 deletions jest.transformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Transforms solutions and test files for Jest.
//
// Most files go through esbuild. It turns modern JavaScript into CommonJS
// like Babel does, but it is written in Go and loads and runs much faster.
//
// 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). Only Babel moves these
// calls above the imports, which Jest needs for the mock to apply.
// - Files that esbuild 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 esbuild = require('esbuild')

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

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 {
const { code, map } = esbuild.transformSync(sourceText, {
format: 'cjs',
loader: 'js',
target: `node${process.versions.node}`,
sourcefile: sourcePath,
// Babel runs every file in strict mode, so that, for example,
// assigning to a getter-only property throws.
banner: '"use strict";',
sourcemap: 'external',
sourcesContent: false,
})
return { code, map }
} catch {
return getBabel().process(sourceText, sourcePath, options)
}
},
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"babel-jest": "^30.4.0",
"chalk": "^6.0.0",
"core-js": "^3.44.0",
"esbuild": "^0.28.2",
"jest": "^30.4.0",
"shelljs": "^0.10.0"
},
Expand Down
271 changes: 271 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 , import_two_fer.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 , import_two_fer.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 , import_two_fer.twoFer) is not a function",
"output": null,
"test_code": "const name = 'Bob';\nexpect(twoFer(name)).toEqual('One for Bob, one for me.');"
}
Expand Down
Loading