Jest: [bug] jest.fn() instanceof Function === false

Created on 28 May 2018  ·  5Comments  ·  Source: facebook/jest

🐛 Bug Report

Mock functions (jest.fn()) violates instanceof Function test.

To Reproduce

Steps to reproduce the behavior:

describe('jest.fn', () => {
  expect(typeof jest.fn()).toBe('function') // OK
  expect(jest.fn() instanceof Function).toBe(true); // FAIL
});

Expected behavior

Mock function should not break instanceof Function test.

Link to repl or repo (highly encouraged)

repl.it demo

Run npx envinfo --preset jest

Paste the results here:

•100% ➜ npx envinfo --preset jest
npx: installed 1 in 7.84s

  System:
    OS: macOS High Sierra 10.13.4
    CPU: x64 Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz
  Binaries:
    Node: 8.10.0 - ~/.anyenv/envs/ndenv/versions/v8.10.0/bin/node
    Yarn: 1.6.0 - ~/.anyenv/envs/ndenv/versions/v8.10.0/bin/yarn
    npm: 5.6.0 - ~/.anyenv/envs/ndenv/versions/v8.10.0/bin/npm
  npmPackages:
    @types/jest: ^22.2.0 => 22.2.0
    jest: ^22.4.3 => 22.4.3

Most helpful comment

I am currently working around this issue by wrapping my mock in a normal function that delegates the call to the target jest mock:

const mock = jest.fn();

const mockWrapper = function (...args) {
  return mock(...args);
}
````

For instance in the above example posted by sandinosaso that may look like:

```javascript
const mockOnClick = jest.fn();
const component = mountWithTheme(
  // Wrap the mock in a function which directly calls the mock
  <Tag data={tagData} onClick={(...args) => mockOnClick(...args)} />
);
component.find('StyledTag').simulate('click');
expect(mockOnClick).toHaveBeenCalled();

All 5 comments

Running in to the same problem when trying to use jest.fn() as a prop to a third party react library that has invariant checks on its props

Same problem here. I was trying to figure out why my coverage was not good. I noticed that even I dispatched an action with a mockFunction it was not being called:

screen shot 2018-06-11 at 12 35 43 pm

What do you suggest is the best approach in the meantime this bug is resolved ?
I changed instanceof for typeof but I prefer to use the former because it check actual types rather than string. It is also faster.

Thank you

I am currently working around this issue by wrapping my mock in a normal function that delegates the call to the target jest mock:

const mock = jest.fn();

const mockWrapper = function (...args) {
  return mock(...args);
}
````

For instance in the above example posted by sandinosaso that may look like:

```javascript
const mockOnClick = jest.fn();
const component = mountWithTheme(
  // Wrap the mock in a function which directly calls the mock
  <Tag data={tagData} onClick={(...args) => mockOnClick(...args)} />
);
component.find('StyledTag').simulate('click');
expect(mockOnClick).toHaveBeenCalled();

expect(jest.fn()).toBeInstanceOf(jest.fn().constructor);

or better yet

expect(jest.fn()).toEqual(expect.any(Function));

This is a duplicate of #2549. Help solving that would be very much appreciated! It has a bounty of $499

Was this page helpful?
0 / 5 - 0 ratings