Skip to content

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
mainfrom
node-test
Open

exercism-agent[bot] wants to merge 4 commits into
mainfrom
node-test

Conversation

@exercism-agent

@exercism-agent exercism-agent Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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.cts is preloaded with --require. Before the test file loads, it registers a module hook, loads src/jest-globals.mts and loads the core-js polyfills (see below).
  • The module hook points @jest/globals at the runner's jest-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 .js and then as a folder with index.js, as Jest's resolver did. The hook changes no files.
  • src/jest-globals.mts defines Jest's API: describe, test, it and their skip variants, the hooks, expect (Jest's own package) and jest (the functions from jest-mock, plus jest.mock). It also makes them globals, as Jest did. Tests keep Jest's 5 second timeout and done callbacks.
  • src/reporter.mts turns Node's test events into Jest-style results, and passes them to the existing src/output.ts. So results.json keeps the same shape and content.
  • src/tests.ts finds 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.cts now loads the polyfills for all 15 stable built-ins missing from Node 24.18 on every run, without reading the files. They include Math.sumPrecise, the Uint8Array base64 and hex methods, and Map.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 on main.

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.runInThisContext and fs. Two are release candidates, which #1042 (comment) confirmed are fine:

  • Synchronous module hooks (module.registerHooks). setup.cts uses a resolve hook for imports, and jest-globals.mts uses a load hook for jest.mock.
  • Syntax detection. The runner doesn't set a module type, so Node loads a file with import or export as 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. --require loads it now.
  • mock.module (Stability: 1.0, Early development, behind --experimental-test-module-mocks) ran jest.mock. The load hook does that now.
  • util.getCallSites (Stability: 1.1, Active development) found the file that called jest.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.sh passes --test-isolation=none, so the tests run in the runner's process. jest-globals.mts records console messages there with Jest's BufferedConsole. The reporter reads them, so output.ts still attributes each message to the right test.
  • Native ES modules load all imports before any code runs, so Jest's hoisting of jest.mock doesn't apply. Before Node loads the test file, jest-globals.mts finds its top-level jest.mock calls 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 uses jest.mock.
  • Node reports a syntax error without its file or line. src/syntax-error.ts finds the error's location with acorn and prints it in the same layout as Babel's code frame.
  • If the solution throws an error or leaves a promise rejection unhandled while it loads, the runner holds it until the first test starts. The reporter then reports the file as failed to run, as Jest did. Each test also waits one turn of the event loop, so that an unhandled rejection fails it, as in Jest. A test that has already used its 5 seconds skips that wait. Without that, Node's timeout could fire during the wait and fail a slow test that had finished. Jest passed such tests.
  • Node reports a test file with no tests as one passing test. The reporter reports it as failed to run, with Jest's message.
  • setup.cts adds --colors to process.argv, so expect colours its messages as it did under Jest.
  • run.sh passes --test-force-exit, which ends the process once the tests finish.

Jest, Babel, @exercism/static-analysis, @typescript-eslint/typescript-estree and the Jest reporter packages are no longer dependencies. run.sh no longer links node_modules into 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 on main too, 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.

  • If the test file imports a name missing from the solution's exports, the whole file will fail to run, with an error that names the missing export. Before, only tests using the missing export failed, and the others could still pass. Three translation-service solutions go from pass to error.
  • ES modules always run in strict mode, so assigning to an undeclared variable will now throw. One promises solution goes from pass to fail.
  • __filename, __dirname and require don't exist in a solution that uses import or export. One parallel-letter-frequency solution goes from pass to fail. Only 8 of 11,083 JavaScript submissions from 15 to 22 November mixed require with import or export.
  • grep's tests fail if the solution writes to stderr. Before, if a solution used import, Node warned on stderr about the package.json that run.sh used to copy into the solution folder, so every test failed. That warning no longer appears, and one solution goes from fail to pass.
  • A CommonJS solution can only be imported by name if Node can see its exports. module.exports = { twoFer } and exports.twoFer = ... work, but module.exports = { twoFer: () => ... } doesn't. With that form, the whole file will fail to run, with "Named export 'twoFer' not found".
  • If a solution leaves a timer running, the run used to last until the 30 second timeout. It now finishes in about a second, with the same results.
  • Syntax errors keep the same shape and code frame, but acorn words the messages differently. For example, its message has no full stop after "Unexpected keyword 'const'".
  • If a file fails to load, the message will be Node's error, without Jest's "Test suite failed to run" heading.
  • Stack frames name functions as Node does (at get balance where Babel's output gave at BankAccount.balance), and an unhandled rejection's error appears once instead of twice.
  • If a test runs out of time, its message will be 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

iHiD and others added 3 commits November 24, 2026 19:47
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
exercism-agent Bot changed the base branch from find-tests-with-acorn to main November 24, 2026 11:31
@exercism-agent
exercism-agent Bot marked this pull request as ready for review November 24, 2026 11:31
@exercism-agent
exercism-agent Bot requested a review from a team as a code owner 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant