Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions e2e/runner/test/fixtures/globals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'rstest-globals';

it('should run setup correctly', async () => {
expect(process.env.A).toBe('A');
});
16 changes: 16 additions & 0 deletions e2e/runner/test/fixtures/test-rstest-globals/index.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
11 changes: 11 additions & 0 deletions e2e/runner/test/fixtures/test-rstest-globals/package.json
Original file line number Diff line number Diff line change
@@ -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:*"
}
}
21 changes: 21 additions & 0 deletions e2e/runner/test/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
10 changes: 7 additions & 3 deletions packages/core/src/runtime/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}, {});
Comment on lines +17 to 24
Copy link
Preview

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function returns an empty object but doesn't populate the apis accumulator. Since the function is now registering globals directly on globalThis, it should either return void or properly populate and return the apis object.

Copilot uses AI. Check for mistakes.

};
Expand Down Expand Up @@ -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,
Copy link
Preview

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removal of the spread operator that conditionally included global APIs makes the code cleaner, but the context object now lacks the rstest APIs that were previously included when globals was true. Verify that this doesn't break existing functionality that relies on these APIs being available in the context.

Suggested change
Error,
Error,
...(globals ? api : {}),

Copilot uses AI. Check for mistakes.

...(globals ? getGlobalApi(api) : {}),
};

// @ts-expect-error
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading