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.

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

See original GitHub issue

🐛 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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:7

github_iconTop GitHub Comments

2reactions
AlanFostercommented, Jun 12, 2018

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:

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();
1reaction
sandinosasocommented, Jun 11, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest's expect(value).toBeInstanceOf(Class) fails for expect(util ...
For now, the simplest workaround is to simply test expect(fn().constructor.name).toBe('Promise') , fully understanding that is much more limited ...
Read more >
Expect - Jest
The expect function is used every time you want to test a value. You will rarely call expect by itself. Instead, you will...
Read more >
Expect · Jest
The expect function is used every time you want to test a value. ... Thus, when pass is false, message should return the...
Read more >
Jest Cheatsheet 1(type) - kelly woo - Medium
Let's make a cheat sheet for jest. ... const bool = false;expect(num). ... toBeInstanceOf(Object); // errorconst numInst = new Number();
Read more >
Node.js v19.3.0 Documentation
Asserts that the function fn does not throw an error. Using assert.doesNotThrow() is actually not useful because there is no benefit in catching...
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