Javascript 如何在 Jest 中重置或清除间谍?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53350382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:06:07  来源:igfitidea点击:

How to reset or clear a spy in Jest?

javascripttestingjestjs

提问by sdgluck

I have a spy that is used in multiple assertions across multiple tests in a suite.

我有一个间谍,它在一个套件中的多个测试的多个断言中使用。

How do I clear or reset the spy so that in each test the method that the spy intercepts is considered not to have been invoked?

如何清除或重置间谍,以便在每次测试中都认为间谍拦截的方法未被调用?

For example, how to make the assertion in 'does not run method'be true?

例如,如何使断言'does not run method'为真?

const methods = {
  run: () => {}
}

const spy = jest.spyOn(methods, 'run')

describe('spy', () => {
  it('runs method', () => {
    methods.run()
    expect(spy).toHaveBeenCalled() //=> true
  })

  it('does not run method', () => {
    // how to make this true?
    expect(spy).not.toHaveBeenCalled() //=> false
  })
})

回答by ghiscoding

Thanks to @sdgluck for the answer, though I would like to add to this answer that in my case, I wanted a clear state after each tests since I have multiple tests with same spy. So instead of calling the mockClear()in previous test(s), I moved it into the afterEach()(or beforeEach) like so:

感谢@sdgluck 的回答,尽管我想补充一点,在我的情况下,我希望每次测试后都有一个清晰的状态,因为我对同一个间谍进行了多次测试。因此mockClear(),我没有在之前的测试中调用,而是将其移动到afterEach()(或beforeEach)中,如下所示:

afterEach(() => {    
  jest.clearAllMocks();
});

And finally, my tests are working like they should without the spy being called from previous test.

最后,我的测试工作正常,没有从之前的测试中调用间谍。

回答by sdgluck

Jest spies have the same API as mocks. The documentation for mocks is hereand specifies a method mockClearwhich:

Jest spies 与 mocks 具有相同的 API。模拟文档在这里并指定了一种方法mockClear

Resets all information stored in the mockFn.mock.callsand mockFn.mock.instancesarrays.

Often this is useful when you want to clean up a mock's usage data between two assertions.

重置存储在mockFn.mock.callsmockFn.mock.instances数组中的所有信息。

当您想要清理两个断言之间的模拟使用数据时,这通常很有用。

(emphasis my own)

(强调我自己)

So we can use mockClearto "reset" a spy. Using your example:

所以我们可以mockClear用来“重置”一个间谍。使用您的示例:

const methods = {
  run: () => {}
}

const spy = jest.spyOn(methods, 'run')

describe('spy', () => {
  it('runs method', () => {
    methods.run()
    expect(spy).toHaveBeenCalled() //=> true
    /* clean up the spy so future assertions
       are unaffected by invocations of the method
       in this test */
    spy.mockClear()
  })

  it('does not run method', () => {
    expect(spy).not.toHaveBeenCalled() //=> true
  })
})

回答by James Irwin

Iterating further on @ghiscoding's answer, you can specify clearMocksin the Jest config, which is equivalent to calling jest.clearAllMocks()between each test:

进一步迭代@ghiscoding 的答案,您可以clearMocks在 Jest 配置中指定,这相当于jest.clearAllMocks()在每个测试之间调用:

{
...
    clearMocks: true,
...
}

See the docs here.

请参阅此处的文档。

回答by Daniel Dantas

In case you want to restore the original behavior of a method you had previously added to a spy, you can use the mockRestore method.

如果您想恢复之前添加到 spy 的方法的原始行为,可以使用 mockRestore 方法。

Check out the example bellow:

看看下面的例子:

class MyClass {
    get myBooleanMethod(): boolean {
        return true;
    }
}

const myObject = new MyClass();
const mockMyBooleanMethod = jest.spyOn(myObject, 'myBooleanMethod', 'get');
// mock myBooleanMethod to return false
mockMyBooleanMethod.mockReturnValue(false);
// restore myBooleanMethod to its original behavior
mockMyBooleanMethod.mockRestore();