Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset the mock spawn #14

Open
sntran opened this issue Aug 17, 2018 · 1 comment
Open

Reset the mock spawn #14

sntran opened this issue Aug 17, 2018 · 1 comment

Comments

@sntran
Copy link

sntran commented Aug 17, 2018

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?

@zjullion
Copy link

zjullion commented Jul 9, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants