Skip to content

Commit 74a89c3

Browse files
authored
fix: support use rstest global APIs in external modules (#554)
1 parent 45084fd commit 74a89c3

File tree

6 files changed

+66
-3
lines changed

6 files changed

+66
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'rstest-globals';
2+
3+
it('should run setup correctly', async () => {
4+
expect(process.env.A).toBe('A');
5+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
beforeAll(() => {
2+
process.env.A = 'A';
3+
});
4+
5+
describe('wrap test', () => {
6+
it('should run', () => {
7+
const fn = rs.fn(() => 'hello');
8+
9+
expect(fn()).toBe('hello');
10+
expect('call it').toBe('call it');
11+
});
12+
13+
it.todo('should not run', () => {
14+
expect(1 + 1).toBe(3);
15+
});
16+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"private": true,
3+
"name": "@rstest/tests-rstest-globals",
4+
"main": "index.js",
5+
"type": "module",
6+
"sideEffects": true,
7+
"version": "1.0.0",
8+
"devDependencies": {
9+
"@rstest/core": "workspace:*"
10+
}
11+
}

e2e/runner/test/runner.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,24 @@ it('should import rstest correctly in node_modules', async () => {
2727
await expectExecSuccess();
2828
expectLog('Tests 2 passed | 1 todo');
2929
});
30+
31+
it('should use rstest global APIs correctly in node_modules', async () => {
32+
await prepareFixtures({
33+
fixturesPath: join(__dirname, './fixtures/test-rstest-globals'),
34+
fixturesTargetPath: join(
35+
__dirname,
36+
'./fixtures/node_modules/rstest-globals',
37+
),
38+
});
39+
const { expectExecSuccess, expectLog } = await runRstestCli({
40+
command: 'rstest',
41+
args: ['run', 'globals.test.ts', '--globals'],
42+
options: {
43+
nodeOptions: {
44+
cwd: join(__dirname, 'fixtures'),
45+
},
46+
},
47+
});
48+
await expectExecSuccess();
49+
expectLog('Tests 2 passed | 1 todo');
50+
});

packages/core/src/runtime/worker/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import { loadModule } from './loadModule';
1414
import { createForksRpcOptions, createRuntimeRpc } from './rpc';
1515
import { RstestSnapshotEnvironment } from './snapshot';
1616

17-
const getGlobalApi = (api: Rstest) => {
17+
const registerGlobalApi = (api: Rstest) => {
1818
return globalApis.reduce<{
1919
[key in keyof Rstest]?: Rstest[key];
2020
}>((apis, key) => {
21-
apis[key] = api[key] as any;
21+
// @ts-expect-error register to global
22+
globalThis[key] = api[key] as any;
2223
return apis;
2324
}, {});
2425
};
@@ -141,11 +142,14 @@ const preparePool = async ({
141142
throw new Error(`Unknown test environment: ${testEnvironment}`);
142143
}
143144

145+
if (globals) {
146+
registerGlobalApi(api);
147+
}
148+
144149
const rstestContext = {
145150
global,
146151
console: global.console,
147152
Error,
148-
...(globals ? getGlobalApi(api) : {}),
149153
};
150154

151155
// @ts-expect-error

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)