Skip to content

Commit 98180f2

Browse files
committed
feat: support rs.hoisted
1 parent 77f4915 commit 98180f2

File tree

7 files changed

+68
-364
lines changed

7 files changed

+68
-364
lines changed

e2e/mock/src/sum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ import { bar } from './bar';
22
import { foo } from './foo';
33

44
export const sum = foo + bar;
5+
export { bar, foo };

e2e/mock/tests/hoisted.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { expect, it, rs } from '@rstest/core';
2+
import { foo } from '../src/sum';
3+
4+
// `rs` should can be accessed in hoisted function.
5+
const mocks = rs.hoisted(() => {
6+
return {
7+
hoistedFn: rs.fn(),
8+
};
9+
});
10+
11+
rs.mock('../src/sum', () => {
12+
return { foo: mocks.hoistedFn };
13+
});
14+
15+
it('hoisted', () => {
16+
mocks.hoistedFn(42);
17+
expect(mocks.hoistedFn).toHaveBeenCalledOnce();
18+
expect(mocks.hoistedFn).toHaveBeenCalledWith(42);
19+
expect(foo).toBe(mocks.hoistedFn);
20+
});

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
"pnpm": ">=10.12.4"
5959
},
6060
"pnpm": {
61+
"overrides": {
62+
"@rspack/core": "link:../rspack/packages/rspack"
63+
},
6164
"ignoredBuiltDependencies": [
6265
"@biomejs/biome",
6366
"nx",

packages/core/src/core/plugins/mockRuntimeCode.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,8 @@ __webpack_require__.rstest_reset_modules = () => {
169169
});
170170
};
171171
//#endregion
172+
173+
//#region rs.hoisted
174+
__webpack_require__.rstest_hoisted = (fn) => {
175+
return fn();
176+
};

packages/core/src/runtime/api/utilities.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ export const createRstestUtilities: (
8282
},
8383
resetModules: () => {
8484
// The actual implementation is managed by the built-in Rstest plugin.
85-
return rstest;
85+
return {} as any;
86+
},
87+
hoisted: () => {
88+
// The actual implementation is managed by the built-in Rstest plugin.
89+
return {} as any;
8690
},
8791

8892
setConfig: (config) => {

packages/core/src/types/mock.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ export interface RstestUtilities {
238238
moduleFactory?: () => T,
239239
) => void;
240240

241+
/**
242+
* Hoisted mock function.
243+
*/
244+
hoisted: <T = unknown>(fn: () => T) => T;
245+
241246
/**
242247
* Removes module from the mocked registry.
243248
*/

0 commit comments

Comments
 (0)