-
-
Notifications
You must be signed in to change notification settings - Fork 13
feat: support rs.hoisted
#717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ import { bar } from './bar'; | |
| import { foo } from './foo'; | ||
|
|
||
| export const sum = foo + bar; | ||
| export { bar, foo }; | ||
| 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); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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. | ||||||
|
||||||
| 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. |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -169,3 +169,8 @@ __webpack_require__.rstest_reset_modules = () => { | |||||||
| }); | ||||||||
| }; | ||||||||
| //#endregion | ||||||||
|
|
||||||||
| //#region rs.hoisted | ||||||||
| __webpack_require__.rstest_hoisted = (fn) => { | ||||||||
| return fn(); | ||||||||
| }; | ||||||||
|
||||||||
| }; | |
| }; | |
| //#endregion |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -82,7 +82,11 @@ export const createRstestUtilities: ( | |||||||||||||
| }, | ||||||||||||||
| resetModules: () => { | ||||||||||||||
| // The actual implementation is managed by the built-in Rstest plugin. | ||||||||||||||
| return rstest; | ||||||||||||||
| return {} as any; | ||||||||||||||
|
||||||||||||||
| return {} as any; | |
| return rstest; |
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
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.
| 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(); |
There was a problem hiding this comment.
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.