Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix toHaveBeenCalledBefore/toHaveBeenCalledAfter type definition #292

Merged
merged 1 commit into from Jul 19, 2022

Conversation

aheuermann
Copy link
Contributor

@aheuermann aheuermann commented Aug 11, 2020

What

I get a compiler error when passing a jest.MockedFunction type to toHaveBeenCalledAfter()/toHaveBeenCalledBefore().

Why

Code showing the issue.

import { func } from './some-func';
import { SomeClass } from './some-class';

const mockFunc = func as jest.MockedFunction<typeof func>;
const mockClass = SomeClass as jest.MockedClass<typeof SomeClass>;

jest.mock('./some-func');
jest.mock('./some-class');

// all of the following statements generate a compiler error
expect(jest.fn()).toHaveBeenCalledBefore(mockFunc);
expect(jest.fn()).toHaveBeenCalledBefore(mockClass);
expect(jest.fn()).toHaveBeenCalledAfter(mockFunc);
expect(jest.fn()).toHaveBeenCalledAfter(mockClass);

I get this compiler error.

 Argument of type 'MockedFunction<() => void>' is not assignable to parameter of type 'Mock<any, any>'.
  The types returned by 'mockClear().mockClear()' are incompatible between these types.
    Type 'MockedFunction<() => void>' is not assignable to type 'Mock<any, any>'.
      Type 'MockInstance<void, [()?]> & (() => void)' provides no match for the signature 'new (...args: any): any'.

12. expect(jest.fn()).toHaveBeenCalledAfter(mockFunc);

Changing these to jest.MockInstance resolves the issue and should work for all use cases as both jest.MockedFunction and jest.MockedClass extend jest.MockInstance.

Notes

Housekeeping

  • Unit tests
  • Documentation is up to date
  • No additional lint warnings
  • Typescript definitions are added/updated where relevant

jacksteamdev added a commit to jacksteamdev/DefinitelyTyped that referenced this pull request Jan 19, 2021
This change allows `MockedFunction` to be used where `Mock` can be used. I don't think this fundamentally changes `MockedFunction`, just makes it more robust. This would resolve issues where an extended expect method takes a `Mock` type, but the mocking library uses `MockedFunction`.

Related issues:
extend-chrome/jest-chrome#9
jest-community/jest-extended#292
@jacksteamdev
Copy link

Until this gets merged, you can fix this in your own project by adding the following to a declaration file (modules.d.ts or maybe jest-extended.d.ts) to your own project.

/// <reference types="jest" />

declare namespace jest {
  interface Matchers {
    /**
     * Use `.toHaveBeenCalledBefore` when checking if a `MockInstance` was called before another `MockInstance`.
     *
     * Note: Required Jest version >=23
     *
     * @param {MockInstance} mock
     */
    toHaveBeenCalledBefore(mock: jest.MockInstance): R

    /**
     * Use `.toHaveBeenCalledAfter` when checking if a `MockInstance` was called after another `MockInstance`.
     *
     * Note: Required Jest version >=23
     *
     * @param {MockInstance} mock
     */
    toHaveBeenCalledAfter(mock: jest.MockInstance): R
  }

  interface Expect {
    /**
     * Use `.toHaveBeenCalledBefore` when checking if a `MockInstance` was called before another `MockInstance`.
     *
     * Note: Required Jest version >=23
     *
     * @param {MockInstance} mock
     */
    toHaveBeenCalledBefore(mock: jest.MockInstance): R

    /**
     * Use `.toHaveBeenCalledAfter` when checking if a `MockInstance` was called after another `MockInstance`.
     *
     * Note: Required Jest version >=23
     *
     * @param {MockInstance} mock
     */
    toHaveBeenCalledAfter(mock: jest.MockInstance): R
  }
}

types/index.d.ts Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Jul 19, 2022

Codecov Report

Merging #292 (be4689c) into main (9d5d236) will not change coverage.
The diff coverage is n/a.

@@            Coverage Diff            @@
##              main      #292   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           71        71           
  Lines          582       582           
  Branches       148       148           
=========================================
  Hits           582       582           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9d5d236...be4689c. Read the comment docs.

@keeganwitt keeganwitt merged commit be7bc8e into jest-community:main Jul 19, 2022
10 checks passed
@keeganwitt keeganwitt mentioned this pull request Jul 20, 2022
@SimenB
Copy link
Member

SimenB commented Aug 4, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants