proposal: add `MockModuleOptions.preserveOthers`
@JakobJingleheimer ci sta già lavorando.
Dal 24/5/2025.
Valutazione
Questa issue non è ancora stata valutata.
Descrizione
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.
- Lingua principale
- Nessun dato sulla lingua
- Stelle
- 3
- Fork
- 4
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di nodejs/test-runner
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 1/100
nodejs/test-runner#46 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 5/100
nodejs/test-runner#45 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 10/100
nodejs/test-runner#44 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 15/100
nodejs/test-runner#43 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 15/100
nodejs/test-runner#42 ·