jest: "resetAllMocks" does not reset all mocks

๐Ÿ› 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

About this issue

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

Most upvoted comments

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?

@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).

After playing with this topic for a bit, it seems like calling jestMock.clearAllMocks() will work on those mocks.

It seems like the file is required multiple times (within jest-runtime, jest-env-jsdom and jest-env-node) and the require cache is (probably on purpose) not always respected.

Iโ€™m not sure how to continue, possibly by attaching the mock state to global?

Apologies to @maumercado, I didnโ€™t mean to steal this from you, hope this info could help you solve it.

@johannes-scharlach Iโ€™m not sure I follow - can you post a sample of what you tested?

@maumercado feel free to take a look as well!