Skip to content

Run the tests with Node's built-in test runner instead of Jest - #106

Draft
exercism-agent[bot] wants to merge 4 commits into
tsc-emitfrom
node-test
Draft

exercism-agent[bot] wants to merge 4 commits into
tsc-emitfrom
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 draft moves the runner from Jest to Node's built-in test runner, the direction discussed in exercism/javascript-test-runner#1042, and makes it around 5x faster with the PRs below it. 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 is the second of three draft PRs, stacked on #105. Merge #105 first, then this one, then #107.

What changed

After #105, tsc writes the JavaScript during the type check, and Jest only runs it. Jest still costs about 1.2s of each run: it loads its config, haste map, runtime and test environment before it runs a single test. The reporter also loads the whole TypeScript compiler a second time, through @exercism/static-analysis, to find the tests in the test file. That costs about 0.4s.

The runner now runs the test file with node --test, as native ES modules, in the runner's own process. It uses nearly the same files as the JavaScript runner's exercism/javascript-test-runner#1047, with the same names:

  • Node preloads src/setup.cts with --require. Before the test file loads, setup.cts registers a module hook, loads src/jest-globals.mts and loads the core-js polyfills (see below).
  • src/jest-globals.mts defines Jest's API: describe, test, it and their skip variants, the hooks, expect (Jest's own package) and jest (from jest-mock). It also makes them globals, as Jest did. Tests keep Jest's 5 second timeout and done callbacks. jest-globals.mts also records console messages with Jest's BufferedConsole.
  • src/reporter.mts turns Node's test events into Jest-style results and passes them to src/output.ts, so results.json keeps the same shape and content.
  • src/tests.ts finds the tests in the test file. The reporter uses them for test_code, the task IDs and each test's console messages. It walks TypeScript's own syntax tree. This takes only a few tens of milliseconds, because Node loads TypeScript from the image's compile cache. The JavaScript runner's tests.ts follows the same rules with acorn. On all 119 test files in the track and the fixtures, tests.ts finds the same tests, names, task IDs, code and locations as @exercism/static-analysis.

bin/compile.cjs always writes ES modules, even if tsconfig.json asks for another module format. It puts a package.json with "type": "module" in its output folder. The type check itself doesn't change.

Node only finds a relative import if it names the compiled file in full, and only finds @jest/globals in a node_modules folder. The module hook in setup.cts handles both, as in the JavaScript runner. It points @jest/globals at the runner's jest-globals.mjs, so solutions still can't import the runner's other packages. If Node can't find a relative import, the hook tries the .js file and then the folder's index.js. So './two-fer.ts' and './two-fer' both load two-fer.js. The track's current test files import the .ts file, but test files from exercise versions before July 2024 leave out the extension.

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, as in the JavaScript runner. 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 moves from 3.37 to 3.49, the version used by the JavaScript runner.

This repo's CI used Yarn Plug'n'Play. The Docker image has installed packages into node_modules since #102, and .yarnrc.yml now makes CI do the same. run.sh no longer needs its Plug'n'Play path, where it copied the yarn cache and ran yarn install, so that path is gone. The CI on main now uses Node 24 and 26, as the PR CI already does.

Each flag on the node command keeps a Jest behaviour:

  • --enable-source-maps makes stack traces and console origins name the .ts lines.
  • --test-isolation=none runs the tests in the runner's process, so the reporter can read the console messages.
  • --test-force-exit stops the run once the tests have finished, even if the solution leaves a timer running.

This removes Jest, Babel, babel-jest, @exercism/babel-preset-typescript and @exercism/static-analysis, along with jest.runner.config.js and babel.config.js. run.sh no longer moves a student's babel.config.js aside, because nothing reads it now. The image is 57 MB smaller than with #105.

Smaller changes:

  • The warm-up at image build now also checks that the warm-up test ran. If the runner finds no tests, the build will fail.
  • eslint.config.mjs tells eslint-plugin-jest which Jest version to assume, because Jest is no longer installed.

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, --enable-source-maps, --test-isolation, --test-force-exit, --test-reporter, node:test, require() of an ES module, the compile cache and fs. The one release candidate is synchronous module hooks (module.registerHooks), which exercism/javascript-test-runner#1042 (comment) confirmed is fine.

An earlier version of this PR used --import (Stability: 1, Experimental) to load the setup file. --require loads it now. It also used module.stripTypeScriptTypes to prepare the test file for acorn in tests.ts, which needed --disable-warning=ExperimentalWarning. tests.ts uses TypeScript's parser now, and the flag is gone.

Another earlier version avoided module hooks by rewriting the import paths with a transformer while tsc wrote the JavaScript. The hook replaces about 100 lines in bin/compile.cjs with about 30 lines in setup.cts, nearly the same as the JavaScript runner's, and takes the same time.

How the TypeScript files differ from the JavaScript runner's

  • The module hook in setup.cts also maps an import of a .ts file to the compiled .js file.
  • jest-globals.mts leaves out jest.mock (and the code that runs it before the test file loads), because no TypeScript exercise uses it.
  • tests.ts walks TypeScript's syntax tree, where the JavaScript runner's walks acorn's.
  • output.ts also removes the folder of compiled files from messages. There is no syntax-error.ts, because tsc reports syntax errors.

Why Node doesn't run the TypeScript files directly

Node can also run the .ts files directly with --experimental-transform-types, which would let us delete bin/compile.cjs. That flag isn't stable. It also changes which solutions pass, so tsc still writes the JavaScript. Two examples:

  • If a solution imports a type without import type (import { Shape } from './types.ts'), tsc accepts it, but Node fails to load it: "does not provide an export named 'Shape'".
  • If a solution imports a file without its extension (from './helper'), tsc accepts it, but Node can't find the file.

What students will see

On all 112 of the track's example solutions, every test passes or fails as before, with the same messages, test_code, task IDs and output as Jest on main. That holds for the test files as they are in the track, and for test files in the style used before July 2024, which import without the extension. Six cases behave differently:

  • 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, each test that used the missing name failed with "two_fer_1.twoFer is not a function". The two-fer/fail/empty fixture moves to two-fer/error/empty for this, as in the JavaScript runner.
  • require, __dirname and __filename don't exist in ES modules. If a solution uses them, the whole test file will fail to run.
  • If a solution leaves a timer running, the run will now finish and report the results. Jest wrote the same results but kept running until the run timed out.
  • If a test takes longer than 5 seconds, its message will be "test timed out after 5000ms". Jest said "Exceeded timeout of 5000 ms for a test" with a hint about raising the timeout.
  • If a test file has no tests, the message will be "Your test suite must contain at least one test." without Jest's "Test suite failed to run" heading.
  • If a solution uses Math.sumPrecise, the Uint8Array base64 and hex methods or Map.prototype.getOrInsert, it will now work. On main, core-js 3.37 had no polyfills for them, so the solution failed with "Math.sumPrecise is not a function". tsc only accepts these with an as any cast or the esnext library in tsconfig.json.

The differences from #105 still apply: stack frames name functions the way Node does, and a syntax error in the test file uses TypeScript's wording with no code frame.

The test files don't change, so the track and the website need no changes.

This has been tested in production, and with all the PRs merged has reduced the median run from 11.8s to 2.5s.

🤖 Generated with Claude Code

iHiD and others added 3 commits November 24, 2026 19:48
bin/compile.cjs already writes the JavaScript during the type check. Node now runs those files with node --test, in one process. A small setup module gives the tests describe, it, xit, the hooks, Jest's own expect and jest-mock, and a node:test reporter writes the same results.json. Jest, Babel and core-js are removed.

Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com>

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reporter found the tests in the test file with @exercism/static-analysis, which loads the whole TypeScript compiler on every run. src/tests.ts now removes the types with Node's stripTypeScriptTypes, which keeps every line and column in place, and parses the result with acorn. On all 119 test files in the track and the fixtures, it finds the same tests, names, task IDs, code and locations.

bin/compile.cjs now writes ES modules, and Node runs them with the same files as the JavaScript runner: src/setup.mts registers a module hook, src/jest-globals.mts provides the Jest API, and src/reporter.mts writes the results through src/output.ts. The hook finds the compiled file for an import of './two-fer' or './two-fer.ts', so compile.cjs no longer rewrites imports. The hook doesn't work with Yarn Plug'n'Play, so .yarnrc.yml installs the packages into node_modules in CI as well as in the Docker image, and run.sh drops its Plug'n'Play path.

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 the compiled file for each relative import, and stripTypeScriptTypes in the test finder.

Node now preloads setup.cjs with --require. bin/compile.cjs rewrites the import paths in the JavaScript written by tsc: './two-fer.ts' and './two-fer' become './two-fer.js', and '@jest/globals' points at the runner's jest-globals.mjs. src/tests.ts now finds the tests with TypeScript's own parser, which Node loads from the image's compile cache. On all 119 test files in the track and the fixtures that the old code could parse, it 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>
Node's synchronous module hooks (module.registerHooks) are marked "Release candidate", which is now acceptable. setup.cts registers the same resolve hook as the JavaScript runner's, so Node finds @jest/globals and the compiled .js file for each relative import, as Jest did. This replaces the transformer in bin/compile.cjs, which rewrote the import paths in the JavaScript written by tsc. Errors about an import now quote the path as written in the test file.

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. core-js moves from 3.37 to 3.49, the version used by the JavaScript runner. The list was generated with core-js-compat, and the comment above it says how to regenerate it.

@types/node moves to 22.16, which declares registerHooks.

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