mock-spawn
mock-spawn copied to clipboard
Reset the mock spawn
I have tests that need to check a function spawning the right commands.
However, because multiple tests calling that function, it's hard to rely on mockSpawn.calls
to know which call was the one used in one particular test.
Is there a way that I can reset the mock spawn in my setUp or tearDown?
For anyone else that needs this to work, here's a solution in jest:
...
jest.mock('child_process', () => ({
...jest.requireActual('child_process'),
spawn: jest.fn()
}))
let spawnMock
...
describe('myTest', () => {
beforeEach(() => {
spawnMock = mockSpawn()
spawn.mockImplementation(spawnMock)
})
...
})
and then you just access spawnMock
in each test when you need to set behaviour, check calls, etc.