rstest icon indicating copy to clipboard operation
rstest copied to clipboard

[Bug]: Stripping types is currently unsupported for files under node_modules

Open colinaaa opened this issue 4 months ago • 3 comments

Version

System:
    OS: Linux 5.15 veLinux GNU/Linux 2 (lyra) 2 (lyra)
    CPU: (64) x64 Intel(R) Xeon(R) Platinum 8457C
    Memory: 246.59 GB / 251.61 GB
    Container: Yes
    Shell: 5.2.15 - /bin/bash
  Binaries:
    Node: 22.18.0 - /run/user/1001/fnm_multishells/3890718_1754797642501/bin/node
    npm: 10.9.3 - /run/user/1001/fnm_multishells/3890718_1754797642501/bin/npm
    pnpm: 10.14.0 - /run/user/1001/fnm_multishells/3890718_1754797642501/bin/pnpm
  npmPackages:
    @rstest/core: ^0.1.3 => 0.1.3

Details

When using a .ts file from node_modules, Rstest would have this error.

I think the error message came from Node.js type stripping, which is enabled by default in v22.18.0.

If switch Node.js to < v22.18, the error message become

Unknown file extension ".ts"

It seems that Rstest is not transforming .ts files in node_modules by default (which is the default behavior for source.include of Rsbuild). But setting source.include to [/node_modules/] does not work either.

Reproduce link

https://github.com/colinaaa/rsbuild-plugin-arethetypeswrong

Reproduce Steps

  1. pnpm add -D @rstest/core
  2. Replace vitest with @rstest/core (and vi to rs, too)
  3. Run npx rstest

colinaaa avatar Aug 11 '25 03:08 colinaaa

In the Node.js test environment, all packages in node_modules are externalized by default. If you want all dependencies to be bundled, you can configure it through the following configuration:

import { defineConfig } from '@rstest/core';

export default defineConfig({
  tools: {
    rspack: (config) => {
      config.externals = [];
    },
  },
});

Reference: https://rstest.rs/config/build/output#outputexternals

9aoy avatar Aug 11 '25 08:08 9aoy

What do you think of adding a default module hook when using Node.js environment, which would use the SWC in Rspack to transform the TypeScript files that is not bundled by Rstest.

The pseudo code would look like this:

/**
 * The `load` hook provides a way to define a custom method of determining how a URL should be interpreted, retrieved, and parsed.
 * It is also in charge of validating the import assertion.
 *
 * @type {import('module').LoadHook}
 */
export const load = async function load(url, context, nextLoad) {
  const isTS = /\.tsx?$/.test(url)

  if (!isTS) {
    return;
  }

  const result = await nextLoad(url, context)

  const transformResult = rspack.experiments.swc.transform(
    result.source.toString(),
    config.tools.swc,
  )

  return {
    format: 'commonjs',
    shortCircuit: true,
    source: result.code,
  }
}

This would make it much more easier to migrate from Vitest (which transform all TypeScript files by default).

colinaaa avatar Aug 12 '25 17:08 colinaaa

What do you think of adding a default module hook when using Node.js environment, which would use the SWC in Rspack to transform the TypeScript files that is not bundled by Rstest.

Good idea!

This would make it much more easier to migrate from Vitest (which transform all TypeScript files by default).

This behavior is the same as vitest and jest, where all packages inside node_modules are externalized by default.

https://vitest.dev/config/#server-deps-external

https://jestjs.io/docs/configuration#transformignorepatterns-arraystring

9aoy avatar Aug 13 '25 03:08 9aoy