Skip to content

Run the type tests in the same process as the type check - #107

Draft
exercism-agent[bot] wants to merge 1 commit into
node-testfrom
faster-type-tests
Draft

exercism-agent[bot] wants to merge 1 commit into
node-testfrom
faster-type-tests

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 makes runs of exercises with type tests about twice as fast, so an exercise can have both runtime tests and tstyche type tests without a slow run. It follows on from exercism/javascript-test-runner#1042 (comment). 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 third of three draft PRs, stacked on #106. Merge #105 and #106 first.

What changed

The tstyche command took about 3 seconds for lasagna's type tests. The type check in bin/compile.cjs takes about 1.5 seconds, so adding the type tests made the time spent on types about three times longer. I profiled the command, and three things made it slow:

  • tstyche loads its own copy of the TypeScript compiler. tstyche patches that copy's source so that the type checker exposes the relations needed by its matchers, then compiles the patched source with vm.compileFunction.
  • It opens each test file in TypeScript's project service. The runner's tsconfig.json excludes __typetests__, so the service first loads the project for tsconfig.json, finds that the test file isn't in it, and then builds a second, default project for the test file. That second project loads TypeScript's full ESNext library and all 13 @types packages in node_modules. Together the two projects read 333 files.
  • Each project parses and binds the standard library and the Node types. That is almost all of the work. In the type check, parsing takes 0.67s, binding 0.33s and checking the solution 0.01s.

The compile cache doesn't help here. With a cache built for tstyche's copy of TypeScript, the command still took 3.1s.

Now bin/compile.cjs runs the type tests itself, straight after the type check, in the same process:

  • run.sh looks for __typetests__/*.tst.ts before the build step, and sets TYPE_TESTS=true for bin/compile.cjs if it finds any.
  • If TYPE_TESTS is set, bin/compile.cjs loads TypeScript through tstyche's StoreService. The type check and the type tests then both use tstyche's copy.
  • After the type check, the new bin/type-tests.cjs builds one more program, with the test files added. Its compiler host hands back the files already parsed and bound by the type check, so only the test files are parsed again.
  • tstyche gets its language service from TypeScript's project service. bin/type-tests.cjs gives tstyche a small project service that answers from that program. The small service has only the four methods that tstyche calls. If a later tstyche calls a method missing from it, bin/compile.cjs will crash with a TypeError (for example "closeClientFile is not a function"), and results.json will hold that stack trace. yarn.lock pins tstyche at 2.1.1, and the three tstyche fixtures in test/types.test.mjs would then fail in CI on the PR that upgrades it.
  • bin/type-tests.cjs uses tstyche's own code, through its exports, for everything else: ConfigService reads the options and any tstyche.config.json, SelectService finds the test files, and TSTyche runs them with --failFast. TSTyche writes its output to tstyche.stdout.txt and tstyche.stderr.txt as before, and run.sh reads those files in the same way.

The type tests now add about 0.3s to the type check. Exercises without type tests take the same path as before, with require('typescript') from the compile cache.

Smaller changes:

  • bin/compile.cjs used to let tsc exit as soon as it had reported the diagnostics. It now records the exit code, runs the type tests, writes the JavaScript and then exits with the same code.
  • With type tests, Node can't cache tstyche's copy of TypeScript. So bin/warm-up.sh now runs the warm-up exercise a second time without its type test, which puts the normal TypeScript module in the cache.
  • .dockerignore lets bin/type-tests.cjs into the image.

Why the type tests and runtime tests run in sequence

I tried running the tstyche command at the same time as bin/compile.cjs. On a 2 vCPU machine (one physical core), both together took 4.7s, and one after the other took 4.6s, because both use the CPU the whole time and so only take turns. With this PR the type tests take about 0.3s and node --test about 0.4s, so running those two in parallel would save at most a few tenths of a second. I run them one after the other.

What students will see

All 135 solutions I checked give byte-identical results.json files before and after this change. They were the track's example solutions, the runner's edge cases and a set of lasagna variants: runtime and type tests passing, a failing runtime test, a failing type test, type tests only (passing and failing), a syntax error in the type test file, a missing export, and a tsc error.

The type tests now use the same compiler options as the type check, including its lib and types settings. Before, tstyche used its own defaults, because the test files fell outside tsconfig.json. The exercises list __typetests__/*.tst.ts in their own tsconfig.json, so students running yarn test:types locally already get these options. A type test would only behave differently if it relied on ESNext library types or on the looser defaults (for example, a catch variable typed any instead of unknown).

The JavaScript runner

Each JavaScript speed-up has a TypeScript counterpart in this stack, or doesn't apply:

JavaScript TypeScript
exercism/javascript-test-runner#1043 compile cache and prefetch #104 (merged)
exercism/javascript-test-runner#1044 drop --detectOpenHandles Doesn't apply, because the TypeScript runner never passed it.
exercism/javascript-test-runner#1045 --forceExit #106 passes --test-force-exit. #105 still runs Jest without --forceExit.
exercism/javascript-test-runner#1047 node --test, native ES modules and the new test finder (now including exercism/javascript-test-runner#1046) #106

#105 and this PR have no JavaScript counterpart, because the JavaScript runner has no type check and no type tests.

This has been tested in production, and has reduced the median run of an exercise with type tests from 5.2s to 2.4s.

🤖 Generated with Claude Code

The tstyche command took about 3 seconds for lasagna's type tests. It loaded its own copy of TypeScript, then parsed and bound the standard library and the Node types twice: once for tsconfig.json, which excludes the type tests, and once for a default project for the test file. bin/compile.cjs now loads TypeScript through tstyche, and after the type check runs tstyche in the same process on a program that reuses the type check's parsed files. The type tests now add about 0.3 seconds. They use the compiler options from tsconfig.json, as they do when students run them locally.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@iHiD
iHiD force-pushed the faster-type-tests branch from 6fc74e0 to 77a2a6f Compare November 24, 2026 13:27
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