From 2c2095ed2ba0d8b0419a95faad069deae6fa3897 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Wed, 10 Sep 2025 11:56:29 +0800 Subject: [PATCH 1/2] fix: support use rstest global APIs in external modules --- e2e/runner/test/fixtures/globals.test.ts | 5 +++++ .../fixtures/test-rstest-globals/index.js | 16 ++++++++++++++ .../fixtures/test-rstest-globals/package.json | 11 ++++++++++ e2e/runner/test/runner.test.ts | 21 +++++++++++++++++++ packages/core/src/runtime/worker/index.ts | 10 ++++++--- 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 e2e/runner/test/fixtures/globals.test.ts create mode 100644 e2e/runner/test/fixtures/test-rstest-globals/index.js create mode 100644 e2e/runner/test/fixtures/test-rstest-globals/package.json diff --git a/e2e/runner/test/fixtures/globals.test.ts b/e2e/runner/test/fixtures/globals.test.ts new file mode 100644 index 00000000..8127f708 --- /dev/null +++ b/e2e/runner/test/fixtures/globals.test.ts @@ -0,0 +1,5 @@ +import 'rstest-globals'; + +it('should run setup correctly', async () => { + expect(process.env.A).toBe('A'); +}); diff --git a/e2e/runner/test/fixtures/test-rstest-globals/index.js b/e2e/runner/test/fixtures/test-rstest-globals/index.js new file mode 100644 index 00000000..33a5d409 --- /dev/null +++ b/e2e/runner/test/fixtures/test-rstest-globals/index.js @@ -0,0 +1,16 @@ +beforeAll(() => { + process.env.A = 'A'; +}); + +describe('wrap test', () => { + it('should run', () => { + const fn = rs.fn(() => 'hello'); + + expect(fn()).toBe('hello'); + expect('call it').toBe('call it'); + }); + + it.todo('should not run', () => { + expect(1 + 1).toBe(3); + }); +}); diff --git a/e2e/runner/test/fixtures/test-rstest-globals/package.json b/e2e/runner/test/fixtures/test-rstest-globals/package.json new file mode 100644 index 00000000..c0186367 --- /dev/null +++ b/e2e/runner/test/fixtures/test-rstest-globals/package.json @@ -0,0 +1,11 @@ +{ + "private": true, + "name": "@rstest/tests-rstest-globals", + "main": "index.js", + "type": "module", + "sideEffects": true, + "version": "1.0.0", + "devDependencies": { + "@rstest/core": "workspace:*" + } +} diff --git a/e2e/runner/test/runner.test.ts b/e2e/runner/test/runner.test.ts index c520c33c..b85c4622 100644 --- a/e2e/runner/test/runner.test.ts +++ b/e2e/runner/test/runner.test.ts @@ -27,3 +27,24 @@ it('should import rstest correctly in node_modules', async () => { await expectExecSuccess(); expectLog('Tests 2 passed | 1 todo'); }); + +it('should use rstest global APIs correctly in node_modules', async () => { + await prepareFixtures({ + fixturesPath: join(__dirname, './fixtures/test-rstest-globals'), + fixturesTargetPath: join( + __dirname, + './fixtures/node_modules/rstest-globals', + ), + }); + const { expectExecSuccess, expectLog } = await runRstestCli({ + command: 'rstest', + args: ['run', 'globals.test.ts', '--globals'], + options: { + nodeOptions: { + cwd: join(__dirname, 'fixtures'), + }, + }, + }); + await expectExecSuccess(); + expectLog('Tests 2 passed | 1 todo'); +}); diff --git a/packages/core/src/runtime/worker/index.ts b/packages/core/src/runtime/worker/index.ts index 598447b9..e34e3d47 100644 --- a/packages/core/src/runtime/worker/index.ts +++ b/packages/core/src/runtime/worker/index.ts @@ -14,11 +14,12 @@ import { loadModule } from './loadModule'; import { createForksRpcOptions, createRuntimeRpc } from './rpc'; import { RstestSnapshotEnvironment } from './snapshot'; -const getGlobalApi = (api: Rstest) => { +const registerGlobalApi = (api: Rstest) => { return globalApis.reduce<{ [key in keyof Rstest]?: Rstest[key]; }>((apis, key) => { - apis[key] = api[key] as any; + // @ts-expect-error register to global + globalThis[key] = api[key] as any; return apis; }, {}); }; @@ -141,11 +142,14 @@ const preparePool = async ({ throw new Error(`Unknown test environment: ${testEnvironment}`); } + if (globals) { + registerGlobalApi(api); + } + const rstestContext = { global, console: global.console, Error, - ...(globals ? getGlobalApi(api) : {}), }; // @ts-expect-error From b9d783158aeac0bce48198e85e1818c46f5657ac Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Wed, 10 Sep 2025 11:57:31 +0800 Subject: [PATCH 2/2] fix: lock --- pnpm-lock.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 97e14495..de4e8211 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -259,6 +259,12 @@ importers: specifier: workspace:* version: link:../../packages/core + e2e/runner/test/fixtures/test-rstest-globals: + devDependencies: + '@rstest/core': + specifier: workspace:* + version: link:../../../../../packages/core + e2e/runner/test/fixtures/test-rstest-import: devDependencies: '@rstest/core':