我正在为使用process
中的事件的代码运行一些Jest测试。即使我使用--runInBand
运行Jest,我的测试中的“重叠”事件还是有问题。
test('My Test', () => {
// some code that calls the "something" event once
...
})
test('Another Test', () => {
const spy = jest.fn()
process.on('something', spy)
// some code that calls the "something" event once
...
expect(spy).toHaveBeenCalledTimes(1)
})
如果我当时使用test.only
进行了一项测试,则测试通过。但是,当我全部运行它们时-无论有无--runInBand
,第二个都无法说我的间谍被叫了两次。我该如何解决这个问题?