Skip to content

[Feature]: Support require() of .ts files at runtime (ts-jest migration path) #1182

@fi3ework

Description

@fi3ework

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions