[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)
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:
- Created 5 years ago
- Reactions:11
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I am currently working around this issue by wrapping my mock in a normal function that delegates the call to the target jest mock:
For instance in the above example posted by sandinosaso that may look like:
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:
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