mock-spawn icon indicating copy to clipboard operation
mock-spawn copied to clipboard

Reset the mock spawn

Open sntran opened this issue 6 years ago • 1 comments

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?

sntran avatar Aug 17 '18 15:08 sntran

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.

zjullion avatar Jul 09 '20 19:07 zjullion