proposal: add `MockModuleOptions.preserveOthers`
@JakobJingleheimer 已經在處理了。
開始於 2025年5月24日。
評估
這個 Issue 還沒有評估資料。
描述
This would simplify mocking only a subset of a module's exports. A common use-case for this is a method that will do something undesirable in a test environment, like start a server.
Currently, it's possible with some non-obvious gymnastics:
describe('Example', () => {
let example;
before(async () => {
example = await import('./example.mjs');
const theMock = mock.module('./example.mjs', {
namedExports: {
...example,
theOneThingToMock: mock.fn(),
},
});
// Must do this again!
example = await import('./example.mjs');
});
});
Instead, with preserveOthers, it's more straightforward:
describe('Example', () => {
let example;
before(async () => {
const theMock = mock.module('./example.mjs', {
namedExports: {
theOneThingToMock: mock.fn(),
},
preserveOthers: true,
});
example = await import('./example.mjs');
});
});
There is some hidden complexity in the implementation though: When a module is loaded, it’s immediately added to the module cache (which is controlled by V8—node can’t manipulate it).
So we’ll need to do some trickery like initially load the module with a query param appended to the specifier (which may already have a query param of its own 🤪), grab the exports and cache them for as long as the module is in scope: A module can be mocked multiple times, potentially with different replacements. We’ll need to diff replacements and original.
This would also facilitate supporting a feature like Jest's requireActual: theMock.getOriginal.
- 主要語言
- 沒有語言資料
- 星號
- 3
- 分支
- 4
- PR 合併指標
- 30 天內沒有已合併 PR
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
nodejs/test-runner 的其他 Issue
-
難度 1/5 1 小時以內 新手友好度 1/100
nodejs/test-runner#46 ·
-
難度 1/5 1 小時以內 新手友好度 5/100
nodejs/test-runner#45 ·
-
難度 1/5 1 小時以內 新手友好度 10/100
nodejs/test-runner#44 ·
-
難度 1/5 1 小時以內 新手友好度 15/100
nodejs/test-runner#43 ·
-
難度 1/5 1 小時以內 新手友好度 15/100
nodejs/test-runner#42 ·