Skip to content
Merged
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
14 changes: 5 additions & 9 deletions rstest/mocking/tests/dynamic-mock.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, rs, rstest } from '@rstest/core';
import { formatDate as actualFormatDate } from '../src/utils' with { rstest: 'importActual' };

describe('rs.doMock() - Dynamic Module Mocking', () => {
/**
Expand Down Expand Up @@ -87,15 +88,10 @@ describe('rs.importActual() - Import Original Module', () => {
*/

it('should import actual module inside mock', async () => {
rs.doMock('../src/utils', async () => {
const actual = await rs.importActual<typeof import('../src/utils')>('../src/utils');

return {
...actual,
// Override only specific functions
generateId: () => 'overridden-id',
};
});
rs.doMock('../src/utils', () => ({
formatDate: actualFormatDate,
generateId: () => 'overridden-id',
}));
Comment on lines +91 to +94

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve actual exports in the partial mock

In this partial-mock example, the factory now returns only formatDate and the overridden generateId, so the other real exports from ../src/utils (getTimestamp, sleep, log) are no longer available from the mocked module. A user copying this “use the original module implementation alongside mocked parts” pattern and importing one of those original exports will not get the real implementation; import the actual namespace with the rstest: 'importActual' attribute and spread it before overriding generateId.

Useful? React with 👍 / 👎.


const utils = await import('../src/utils');

Expand Down
Loading