question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"resetAllMocks" does not reset all mocks

See original GitHub issue

🐛 Bug Report

resetAllMocks does not reset mocks created with generateFromMetadata method

To Reproduce

Here is a utility method that I’ve created to create class mocks:

import * as jestMock from 'jest-mock'

export interface IClassType<T> {
  new (...args: any[]): T
}

export function mock<T>(classType: IClassType<T>): any {
  const mockType = jestMock.generateFromMetadata(jestMock.getMetadata(classType))
  return new mockType()
}

However when I use jest.resetAllMocks() mocks created by this function are not reset.

Here is a code example:

import { mock } from '../../../mock-utils'

import { Test } from './Test'

export class Test {
  public testMethod(param: number): boolean {
    return false
  }
}

describe('test mock', () => {
  const testMock = mock(Test)

  it('should reset mocks', () => {
    testMock.testMethod(123)
    testMock.testMethod.mockReset() // <--- works as expected
    expect(testMock.testMethod).not.toHaveBeenCalled() // <---- passes

    testMock.testMethod(123)
    jest.resetAllMocks() // <--- does not reset the mock
    expect(testMock.testMethod).not.toHaveBeenCalled() // <--- fails
  })
})

Expected behavior

The above test should pass.

Link to repl or repo (highly encouraged)

N/A

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS High Sierra 10.13.1
    CPU: x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
  Binaries:
    Node: 10.11.0 - /usr/local/bin/node
    Yarn: 1.10.1 - /usr/local/bin/yarn
    npm: 6.4.1 - ~/node_modules/.bin/npm
  npmPackages:
    @types/jest: ^23.1.0 => 23.3.2
    jest: ^22.0.0 => 22.4.4

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:20
  • Comments:26 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
johannes-scharlachcommented, Nov 24, 2018

So what worked was the following

const jestMock = require('jest-mock');

describe('test mock', () => {
  function mock(classType) {
    const mockType = jestMock.generateFromMetadata(
      jestMock.getMetadata(classType),
    );
    return new mockType();
  }

  class Test {
    testMethod(param) {
      return false;
    }
  }
  const testMock = mock(Test);
  const otherMock = jest.fn();
  it('should reset mocks', () => {
    testMock.testMethod(123);
    testMock.testMethod.mockClear(); // <--- works as expected
    expect(testMock.testMethod).not.toHaveBeenCalled(); // <---- passes

    testMock.testMethod(123);
    jestMock.clearAllMocks(); // <--- does reset the mock
    expect(testMock.testMethod).not.toHaveBeenCalled(); // <--- passes
  });
});

So the this._mockState seems to be different between jest.clearAllMocks() and jestMock.clearAllMocks.

One possible solution here would be to use global._mockState instead of this._mockState, making it definitely the same.

The other thing I found out was that the constructor of the ModuleMockerClass is invoked 3 times when I run this for 1 test file: Once by jest-environment-node, by jest-environment-jsdom and by jest-runtime. (I found out about that by logging a stack trace in the constructor of ModuleMockerClass.). Maybe there is a better way to fix this, given that info?

2reactions
finnigantimecommented, Feb 11, 2020

@caitecoll this workaround, mentioned on #7136, worked for me: https://github.com/facebook/jest/issues/7136#issuecomment-565976599

I think this ^ should be the default jest behavior. Leaking state between tests is an anti-pattern because it means test start to rely on running in a certain order (they rely on the side effects of previous tests). This is a problem because:

  1. A test may succeed when run in sequence but fail when run by itself (with -t).
  2. Tests cannot safely be moved around (order changed) without breaking.
  3. Tests cannot safely be split into batches and run in parallel.

IMO, clearing state between tests should be the default for these reasons and because the vast majority of projects do not require the performance benefits of not having to rebuild state before each test (and those projects that do can opt-into preserving state with config).

If it’s very hard to change these defaults due to back-compat, then at least this deserves thorough documentation and a section on how to set up this config (rather than having to do an extensive grep through issues and stack overflow to find it).

Read more comments on GitHub >

github_iconTop Results From Across the Web

jest.clearAllMocks(); does not remove mock implementation ...
The easiest solution I saw was to reset modules and re-require them before each test. This way resetAllMocks didn't wipe out all the...
Read more >
Why is my resetAllMocks not working in jest - Stack Overflow
I was experiencing the same problem until I placed jest.resetAllMocks(); inside afterEach like so: afterEach(() => { jest.
Read more >
How to automatically reset mocks and restore spies in Jest
Configuring Jest to automatically reset / restore mocks and spies. As it seemed, it turned out Jest can be configured to do an...
Read more >
jest.clearAllMocks vs jest.resetAllMocks ... - DEV Community ‍ ‍
clearAllMocks. Clear all mock usage data such as mock.calls , mock.instances , mock.contexts and mock.results but not their implementation.
Read more >
Jest mock and spy — mockClear vs mockReset vs mockRestore
When we will be updating one tests all other might start failing. If something like this is happening we most probably don't have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found