Repository navigation
Run the tests with Node's built-in test runner instead of Jest - #106
Draft
exercism-agent[bot] wants to merge 4 commits into
Draft
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
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>
exercism-agent
Bot
force-pushed
the
tsc-emit
branch
from
November 24, 2026 11:32
ea05cb1 to
34e4bf1
Compare
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>
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 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:src/setup.ctswith--require. Before the test file loads,setup.ctsregisters a module hook, loadssrc/jest-globals.mtsand loads the core-js polyfills (see below).src/jest-globals.mtsdefines Jest's API:describe,test,itand their skip variants, the hooks,expect(Jest's own package) andjest(fromjest-mock). It also makes them globals, as Jest did. Tests keep Jest's 5 second timeout anddonecallbacks.jest-globals.mtsalso records console messages with Jest'sBufferedConsole.src/reporter.mtsturns Node's test events into Jest-style results and passes them tosrc/output.ts, soresults.jsonkeeps the same shape and content.src/tests.tsfinds the tests in the test file. The reporter uses them fortest_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'stests.tsfollows the same rules with acorn. On all 119 test files in the track and the fixtures,tests.tsfinds the same tests, names, task IDs, code and locations as@exercism/static-analysis.bin/compile.cjsalways writes ES modules, even iftsconfig.jsonasks for another module format. It puts apackage.jsonwith"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/globalsin anode_modulesfolder. The module hook insetup.ctshandles both, as in the JavaScript runner. It points@jest/globalsat the runner'sjest-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.jsfile and then the folder'sindex.js. So'./two-fer.ts'and'./two-fer'both loadtwo-fer.js. The track's current test files import the.tsfile, 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.ctsnow 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 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 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_modulessince #102, and.yarnrc.ymlnow makes CI do the same.run.shno longer needs its Plug'n'Play path, where it copied the yarn cache and ranyarn install, so that path is gone. The CI onmainnow uses Node 24 and 26, as the PR CI already does.Each flag on the
nodecommand keeps a Jest behaviour:--enable-source-mapsmakes stack traces and console origins name the.tslines.--test-isolation=noneruns the tests in the runner's process, so the reporter can read the console messages.--test-force-exitstops the run once the tests have finished, even if the solution leaves a timer running.This removes Jest, Babel,
babel-jest,@exercism/babel-preset-typescriptand@exercism/static-analysis, along withjest.runner.config.jsandbabel.config.js. run.sh no longer moves a student'sbabel.config.jsaside, because nothing reads it now. The image is 57 MB smaller than with #105.Smaller changes:
eslint.config.mjstells 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 andfs. 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.--requireloads it now. It also usedmodule.stripTypeScriptTypesto prepare the test file for acorn intests.ts, which needed--disable-warning=ExperimentalWarning.tests.tsuses 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.cjswith about 30 lines insetup.cts, nearly the same as the JavaScript runner's, and takes the same time.How the TypeScript files differ from the JavaScript runner's
setup.ctsalso maps an import of a.tsfile to the compiled.jsfile.jest-globals.mtsleaves outjest.mock(and the code that runs it before the test file loads), because no TypeScript exercise uses it.tests.tswalks TypeScript's syntax tree, where the JavaScript runner's walks acorn's.output.tsalso removes the folder of compiled files from messages. There is nosyntax-error.ts, because tsc reports syntax errors.Why Node doesn't run the TypeScript files directly
Node can also run the
.tsfiles directly with--experimental-transform-types, which would let us deletebin/compile.cjs. That flag isn't stable. It also changes which solutions pass, so tsc still writes the JavaScript. Two examples:import type(import { Shape } from './types.ts'), tsc accepts it, but Node fails to load it: "does not provide an export named 'Shape'".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 onmain. 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:two-fer/fail/emptyfixture moves totwo-fer/error/emptyfor this, as in the JavaScript runner.require,__dirnameand__filenamedon't exist in ES modules. If a solution uses them, the whole test file will fail to run.Math.sumPrecise, theUint8Arraybase64 and hex methods orMap.prototype.getOrInsert, it will now work. Onmain, 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 anas anycast or theesnextlibrary intsconfig.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