简体   繁体   中英

How to reset or clear a spy in Jest?

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?

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
  })
})

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 you can use it with beforeEach ) like so:

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

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

Jest spies have the same API as mocks. The documentation for mocks is here and specifies a method mockClear which:

Resets all information stored in the mockFn.mock.calls and mockFn.mock.instances arrays.

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

(emphasis my own)

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

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
  })
})

Here is an example in CodeSandbox .

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.

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();

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

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

See the docshere .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM