Repository navigation
Run the tests with Node's built-in test runner and native ES modules - #1047
Open
exercism-agent[bot] wants to merge 4 commits into
Open
exercism-agent[bot] wants to merge 4 commits into
exercism-agent[bot] wants to merge 4 commits into
Conversation
This was referenced Nov 23, 2026
Draft
The reporter parsed the test file with @exercism/static-analysis, which uses @typescript-eslint/typescript-estree and so loads the whole TypeScript compiler on every run. Test files are plain JavaScript, so acorn reads them as well. On all 180 spec files in the track and the fixtures, the new code finds the same tests, names, task IDs, code and locations. Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Jest took almost all of each run: it set up its runtime and loaded Babel, which compiled every file to CommonJS. Node's test runner now runs the test file as a native ES module in the same process. src/setup.mts preloads a module hook, src/jest-globals.mts provides the Jest API the track uses (on Jest's expect and jest-mock), and src/reporter.mts turns the runner's events into the results src/output.ts writes, as before. jest.mock calls run before the test file loads, as Jest hoisted them, using Node's mock.module. A test without `done` waits one turn of the event loop for unhandled rejections, unless it has already used up its 5 seconds. Node reports a test file that defines no tests as one passing test, and the reporter reports that the file failed to run, as Jest did. Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The runner used three Node features that aren't marked stable: --import to preload setup.mts, module.registerHooks to find @jest/globals and files imported without their extension, and mock.module (with --experimental-test-module-mocks) for jest.mock. It also called util.getCallSites. Node now preloads setup.cjs with --require. Before the test file loads, src/imports.ts parses the test file, and each file it imports, with acorn. It adds the missing extension to relative imports and points @jest/globals at the runner's jest-globals.mjs. jest.mock writes a small module in place of the mocked file, which exports what the factory returned, and puts the original back when the process exits. Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
exercism-agent
Bot
force-pushed
the
node-test
branch
from
November 24, 2026 11:31
9fb4547 to
23ee3c5
Compare
exercism-agent
Bot
changed the base branch from
find-tests-with-acorn
to
main
November 24, 2026 11:31
Node's synchronous module hooks (module.registerHooks) are marked "Release candidate", which is now acceptable. setup.cts registers a resolve hook, so Node finds @jest/globals and files imported without their extension, as Jest did. This replaces src/imports.ts, which parsed the test file and every file it imported, and rewrote their import paths on disk. jest.mock now uses a load hook, which hands Node a small module in place of the mocked file, so the file itself is never changed. Errors about an import now quote the path as written in the file, as they did with Jest. Jest's Babel preset added a core-js polyfill for each built-in a file used that Node lacked. setup.cts now loads the 15 core-js modules for the stable built-ins missing from Node 24.18 on every run, with no parsing, such as Math.sumPrecise and Uint8Array.fromBase64. The list was generated with core-js-compat, and the comment above it says how to regenerate it. Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hello 🙂 I'm working with Claude to try and get our tooling faster.
This PR makes this project around 4x faster. I've run it in production to benchmark things in the real world against the most recent few hundred solutions, so I'm confident that it works robustly. However, I've not written the code. I have tried my hardest to get Claude to write sensible messages and comments that explain stuff, but I'd ask for your patience in the fact I'm sending AI code to you. Everything below here is from your friendly neighbourhood LLM...
This PR switches the runner to Node's built-in test runner with native ES modules, as agreed in #1042. It includes the changes from #1046 and replaces the earlier version of this PR. It uses only Node APIs marked stable or release candidate (see below). Once this merges, #1045 (
--forceExit) will no longer be needed.The track change in exercism/javascript#2877 (full extensions on every relative import) can merge before or after this PR. The runner works with the test files from either version.
What changed
Almost all of each run was Jest starting up: reading its config, building its haste map, creating a VM context, and loading Babel to compile the test file and solution to CommonJS. The reporter then parsed the test file with
@exercism/static-analysis, which loads the whole TypeScript compiler and took about 0.4s more. The tests themselves took a few milliseconds.The runner now runs the test file with
node --test, as native ES modules, with no transformer. The track's test files still use Jest's API from@jest/globals, so a few small files provide it:src/setup.ctsis preloaded with--require. Before the test file loads, it registers a module hook, loadssrc/jest-globals.mtsand loads the core-js polyfills (see below).@jest/globalsat the runner'sjest-globals.mjs. This is the only runner file that solutions can import, so they still can't import the runner's other packages. If Node can't find a relative import, the hook tries it with.jsand then as a folder withindex.js, as Jest's resolver did. The hook changes no files.src/jest-globals.mtsdefines Jest's API:describe,test,itand their skip variants, the hooks,expect(Jest's own package) andjest(the functions fromjest-mock, plusjest.mock). It also makes them globals, as Jest did. Tests keep Jest's 5 second timeout anddonecallbacks.src/reporter.mtsturns Node's test events into Jest-style results, and passes them to the existingsrc/output.ts. Soresults.jsonkeeps the same shape and content.src/tests.tsfinds the tests in the test file with acorn, in place of@exercism/static-analysis. The reporter uses them for each test's code, its task ID and its console messages. On all 180 test files in the track and the fixtures, it finds the same test names, task IDs, test code and locations as before.Once the track PR merges, the track's test files will name every file in full, so the hook will only be needed for solutions to earlier versions of an exercise. Those solutions keep the test file from when the student started, and that file leaves out the extensions.
Polyfills
Jest's Babel preset added a core-js polyfill to each file for each built-in used by the file and missing from the image's Node.
setup.ctsnow loads the polyfills for all 15 stable built-ins missing from Node 24.18 on every run, without reading the files. They includeMath.sumPrecise, theUint8Arraybase64 and hex methods, andMap.prototype.getOrInsert. core-js-compat generated the list, and a comment above it says how to regenerate it. Loading them adds about 15ms to each run. core-js stays at 3.49, as onmain.Node APIs
Every Node API, flag and loader feature used by the runner is marked "Stability: 2 - Stable" or "Stability: 1.2 - Release candidate" in the Node 24 and Node 26 docs. The stable ones are
node --test,--require,--test-isolation,--test-force-exit,--test-reporter,node:test,require()of an ES module,vm.runInThisContextandfs. Two are release candidates, which #1042 (comment) confirmed are fine:module.registerHooks).setup.ctsuses a resolve hook for imports, andjest-globals.mtsuses a load hook forjest.mock.importorexportas an ES module and any other file as CommonJS. That lets grep's CommonJS test file and CommonJS solutions keep working. This is Node's default behaviour, with no flag and no warning.Earlier versions of this PR used features below that level, and each has been replaced:
--import(Stability: 1, Experimental) loaded the setup file.--requireloads it now.mock.module(Stability: 1.0, Early development, behind--experimental-test-module-mocks) ranjest.mock. The load hook does that now.util.getCallSites(Stability: 1.1, Active development) found the file that calledjest.mock. The mocks are always resolved from the test file now.One earlier version avoided module hooks. It parsed the test file and every file it imported, rewrote their import paths on disk, and overwrote each mocked file. The hooks replace that with about 40 lines, leave the student's files unchanged, and take the same time.
Details that keep results matching Jest
run.shpasses--test-isolation=none, so the tests run in the runner's process.jest-globals.mtsrecords console messages there with Jest'sBufferedConsole. The reporter reads them, sooutput.tsstill attributes each message to the right test.jest.mockdoesn't apply. Before Node loads the test file,jest-globals.mtsfinds its top-leveljest.mockcalls with acorn and runs each factory. When the test file or the solution imports a mocked file, the load hook hands Node a small module that exports what the factory returned. Only fruit-picker usesjest.mock.src/syntax-error.tsfinds the error's location with acorn and prints it in the same layout as Babel's code frame.setup.ctsadds--colorstoprocess.argv, soexpectcolours its messages as it did under Jest.run.shpasses--test-force-exit, which ends the process once the tests finish.Jest, Babel,
@exercism/static-analysis,@typescript-eslint/typescript-estreeand the Jest reporter packages are no longer dependencies.run.shno longer linksnode_modulesinto the solution folder. It runs the one test file named in the exercise's config, and writes the "no tests" error itself if that file is missing.What students will see
All 169 example solutions give the same results and messages as
main, both with the track's test files from before the track PR and with the full extensions after it. The only exception is lens-person, which fails to load onmaintoo, because its example imports a file missing from the exercise. The error message is now Node's.The earlier version of this PR also ran 210 recent production solutions (30 each for bank-account, fruit-picker, grep, parallel-letter-frequency, promises, react and translation-service). 204 of the 210 gave the same result for every test. The other 6 differed, and the first four bullets below explain them. The remaining bullets list other changes that can apply to any run.
__filename,__dirnameandrequiredon't exist in a solution that usesimportorexport. One parallel-letter-frequency solution goes from pass to fail. Only 8 of 11,083 JavaScript submissions from 15 to 22 November mixedrequirewithimportorexport.import, Node warned on stderr about thepackage.jsonthatrun.shused to copy into the solution folder, so every test failed. That warning no longer appears, and one solution goes from fail to pass.module.exports = { twoFer }andexports.twoFer = ...work, butmodule.exports = { twoFer: () => ... }doesn't. With that form, the whole file will fail to run, with "Named export 'twoFer' not found".at get balancewhere Babel's output gaveat BankAccount.balance), and an unhandled rejection's error appears once instead of twice.test timed out after 5000ms.This has been tested in production, and has reduced the median run from 4.6s to 1.1s.
🤖 Generated with Claude Code