Skip to content

Run the tests with Node's built-in test runner and native ES modules - #1047

Draft
exercism-agent[bot] wants to merge 2 commits into
find-tests-with-acornfrom
node-test
Draft

exercism-agent[bot] wants to merge 2 commits into
find-tests-with-acornfrom
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 and #1046 together make 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 explains 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 two draft PRs, on top of #1046. It follows the direction discussed in #1042: Node's built-in test runner with native ES modules. If it merges, #1044 and #1045 (Jest flags) are no longer needed.

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 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 three small files provide it:

  • src/setup.mts, preloaded with --import, registers a module hook. The hook points @jest/globals at the runner's Jest API, and lets relative imports leave out .js, as Jest did.
  • src/jest-globals.mts defines Jest's API: describe, test, it and their skip variants, the hooks, expect (Jest's own package) and jest (jest-mock's functions plus 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 the results Jest gave, and passes them to the existing src/output.ts. So results.json keeps the same shape and content.

Details that keep results matching Jest:

  • run.sh passes --test-isolation=none, so the tests run in the runner's process. That lets the reporter read console messages, which jest-globals.mts records with Jest's BufferedConsole, so output.ts still attributes them 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 them through Node's mock.module (--experimental-test-module-mocks). Only fruit-picker uses jest.mock.
  • run.sh no longer copies the runner's package.json into the solution folder, so no module type is set. Node then loads a file with import or export as an ES module and any other file as CommonJS, so CommonJS solutions and grep's test file keep working.
  • Node reports a syntax error without its file or line. src/syntax-error.ts finds them with acorn and prints the error in the same layout as Babel's code frame.
  • Errors thrown, and promise rejections left unhandled, while the solution loads are held until the first test starts, and 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 up its 5 seconds skips that wait. Otherwise Node's timeout would fire during the wait and fail a slow test that had finished, which Jest passed.
  • 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.mts 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, core-js and the Jest reporter packages are no longer dependencies. run.sh no longer links node_modules into the solution folder, so solutions can't import packages that only the runner uses. 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

The track needs no changes. All 169 example solutions give the same results and messages as before, and so do the seven async examples. I 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 give the same result for every test. The rest are covered below.

  • If the test file imports a name that the solution doesn't export, the whole file now fails 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 now throws. 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, a solution written with import made Node warn on stderr about the copied package.json, so every test failed. That warning no longer appears, and one solution goes from fail to pass.
  • A solution that leaves a timer running used to wait 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 writes the wording (for example, no full stop after "Unexpected keyword 'const'").
  • A file that fails to load shows 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.
  • A test that runs out of time says test timed out after 5000ms.

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

🤖 Generated with Claude Code

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.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A test without `done` waits one turn of the event loop for unhandled rejections. If a slow synchronous test had already used up its 5 seconds, Node's timeout fired during that wait and failed it, though Jest passed it. Such a test now skips the wait.

Node reports a test file that defines no tests as one passing test, named after the file. The reporter now reports that the file failed to run, with Jest's message.

Both fixes match the TypeScript runner.

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