What problem does this feature solve?
ts-jest users migrating to rstest commonly hit this: business code uses runtime require('./something.ts') (plugin loaders, dynamic config loaders, dynamically loaded modules, etc.). Under jest + ts-jest this works out of the box — ts-jest installs a hook on Node's CJS loader, so require('./foo.ts') is supported natively.
After migrating to rstest, any runtime require('./x.ts') fails with:
Error: Cannot find module './foo.ts'
or (if the caller strips the extension):
SyntaxError: Unexpected token 'type'
The reason: rstest does not register a Node CJS loader hook like ts-jest does, and Node has no native support for .ts. This is a common friction point on the ts-jest migration path.
Current workaround: register a runtime TS loader at the top of setupFiles:
// test/rstest.setup.ts
import '@swc-node/register';
// or: import 'ts-node/register';
After this, runtime require('./foo.ts') loads correctly.
What does the proposed API look like?
Two options:
(a) Documentation approach (lowest cost): explicitly document in the "Migration from Jest" guide that if business code uses runtime require('./foo.ts'), users need to register @swc-node/register or ts-node/register in setupFiles. The docs currently do not mention this at all, leaving users to trial-and-error.
(b) Built-in approach (more complete): provide an opt-in flag so rstest registers a swc-based runtime loader (rstest already depends on swc) inside workers:
// rstest.config.ts
export default defineConfig({
test: {
runtimeTsLoader: true, // or 'swc' | 'off'
},
});
Semantics: during worker setup, register a Node CJS hook that transpiles .ts/.tsx/.mts/.cts in transpile-only mode and hands the output to Node's require. Equivalent to the user manually doing import '@swc-node/register', but zero-config and aligned with rstest's own transform semantics (decorators version, target, etc. inherited from source.*).
(a) is enough to unblock migration; (b) is more "just works" and the incremental cost is small given rstest already embeds swc.
What problem does this feature solve?
ts-jest users migrating to rstest commonly hit this: business code uses runtime
require('./something.ts')(plugin loaders, dynamic config loaders, dynamically loaded modules, etc.). Under jest + ts-jest this works out of the box — ts-jest installs a hook on Node's CJS loader, sorequire('./foo.ts')is supported natively.After migrating to rstest, any runtime
require('./x.ts')fails with:or (if the caller strips the extension):
The reason: rstest does not register a Node CJS loader hook like ts-jest does, and Node has no native support for
.ts. This is a common friction point on the ts-jest migration path.Current workaround: register a runtime TS loader at the top of
setupFiles:After this, runtime
require('./foo.ts')loads correctly.What does the proposed API look like?
Two options:
(a) Documentation approach (lowest cost): explicitly document in the "Migration from Jest" guide that if business code uses runtime
require('./foo.ts'), users need to register@swc-node/registerorts-node/registerinsetupFiles. The docs currently do not mention this at all, leaving users to trial-and-error.(b) Built-in approach (more complete): provide an opt-in flag so rstest registers a swc-based runtime loader (rstest already depends on swc) inside workers:
Semantics: during worker setup, register a Node CJS hook that transpiles
.ts/.tsx/.mts/.ctsin transpile-only mode and hands the output to Node's require. Equivalent to the user manually doingimport '@swc-node/register', but zero-config and aligned with rstest's own transform semantics (decorators version, target, etc. inherited fromsource.*).(a) is enough to unblock migration; (b) is more "just works" and the incremental cost is small given rstest already embeds swc.