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
1 change: 1 addition & 0 deletions e2e/mock/src/sum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import { bar } from './bar';
import { foo } from './foo';

export const sum = foo + bar;
export { bar, foo };
20 changes: 20 additions & 0 deletions e2e/mock/tests/hoisted.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, it, rs } from '@rstest/core';
import { foo } from '../src/sum';

// `rs` should can be accessed in hoisted function.
const mocks = rs.hoisted(() => {
return {
hoistedFn: rs.fn(),
};
});

rs.mock('../src/sum', () => {
return { foo: mocks.hoistedFn };
});

it('hoisted', () => {
mocks.hoistedFn(42);
expect(mocks.hoistedFn).toHaveBeenCalledOnce();
expect(mocks.hoistedFn).toHaveBeenCalledWith(42);
expect(foo).toBe(mocks.hoistedFn);
});
15 changes: 14 additions & 1 deletion e2e/mock/tests/mockHoist.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { expect, it, rs } from '@rstest/core';
import { foo } from '../src/foo';
// NOTE: '../src/d' MUST imported ahead of '../src/c' to avoid circular dependency
import { d1 } from '../src/d';
// @ts-expect-error
import { c, dd } from '../src/c';
import { expect, it, rs } from '@rstest/core';

rs.mock('../src/foo', () => {
return {
foo: rs.fn(),
};
});

it('@rstest/core should be accessible even if it is imported late, it is specifically hoisted', async () => {
// @ts-expect-error
foo('a', 'b');
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

Callee is not a function: it has type string or undefined.

Copilot uses AI. Check for mistakes.
expect(foo).toHaveBeenCalledWith('a', 'b');
});

it('mocked c', async () => {
// @ts-expect-error
Expand Down
2 changes: 1 addition & 1 deletion packages/core/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ Licensed under MIT license in the repository at https://github.com/chaijs/loupe.

### magic-string

Licensed under MIT license in the repository at git+https://github.com/Rich-Harris/magic-string.git.
Licensed under MIT license in the repository at https://github.com/rich-harris/magic-string.git.
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

This appears to be a fix to update the repository URL from "git+https://github.com/Rich-Harris/magic-string.git" to "https://github.com/rich-harris/magic-string.git" (note the lowercase username). While this correction improves accuracy, it's unrelated to the PR's stated purpose of adding support for rs.hoisted. Consider addressing this in a separate PR focused on license file maintenance.

Suggested change
Licensed under MIT license in the repository at https://github.com/rich-harris/magic-string.git.
Licensed under MIT license in the repository at https://github.com/Rich-Harris/magic-string.git.

Copilot uses AI. Check for mistakes.

> Copyright 2018 Rich Harris
>
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/core/plugins/mockRuntimeCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,8 @@ __webpack_require__.rstest_reset_modules = () => {
});
};
//#endregion

//#region rs.hoisted
__webpack_require__.rstest_hoisted = (fn) => {
return fn();
};
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

Missing //#endregion comment to close the rs.hoisted region. All other similar regions in this file (rs.unmock, rs.doUnmock, rs.requireActual, rs.mock, rs.mockRequire, rs.doMock, rs.doMockRequire, rs.reset_modules) properly close their regions with //#endregion comments for consistency.

Suggested change
};
};
//#endregion

Copilot uses AI. Check for mistakes.
6 changes: 5 additions & 1 deletion packages/core/src/runtime/api/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ export const createRstestUtilities: (
},
resetModules: () => {
// The actual implementation is managed by the built-in Rstest plugin.
return rstest;
return {} as any;
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

The return value for resetModules has changed from return rstest; to return {} as any;, which breaks the return type consistency. According to the type definition in mock.ts, resetModules should return RstestUtilities to allow method chaining. All other similar methods in this file return rstest to maintain this pattern.

Suggested change
return {} as any;
return rstest;

Copilot uses AI. Check for mistakes.
},
hoisted: () => {
// The actual implementation is managed by the built-in Rstest plugin.
return {} as any;
Comment on lines +87 to +89
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

The hoisted function implementation returns {} as any but the type signature indicates it should return type T which is the result of calling the provided function fn. This could lead to runtime errors if users rely on the return value. The implementation should call and return the result of fn(), similar to how the webpack runtime code handles it in mockRuntimeCode.js.

Suggested change
hoisted: () => {
// The actual implementation is managed by the built-in Rstest plugin.
return {} as any;
hoisted: <T>(fn: () => T): T => {
// The actual implementation is managed by the built-in Rstest plugin.
return fn();

Copilot uses AI. Check for mistakes.
},

setConfig: (config) => {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/types/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ export interface RstestUtilities {
moduleFactory?: () => T,
) => void;

/**
* Hoisted mock function.
*/
hoisted: <T = unknown>(fn: () => T) => T;

/**
* Removes module from the mocked registry.
*/
Expand Down
Loading
Loading