jest: Jest mock module failed when mocking mongoose model object
Problem mocking a mongoose model Object with the following error.
● Test suite failed to run
TypeError: Cannot create property 'constructor' on number '1'
at ModuleMockerClass._generateMock (node_modules/jest-mock/build/index.js:457:34)
at Array.forEach (native)
at Array.forEach (native)
at Array.forEach (native)
repo with reproduction code node - v6.9.3 yarn - v0.18.1 npm - 3.10.10 OS - macOS Sierra
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 14
- Comments: 22 (12 by maintainers)
Commits related to this issue
- Fixes issue #3073. Typechecks prototype before assignment. Fixes interaction with mongoos — committed to hluedeke/jest by deleted user 6 years ago
I have the same issue, how can fix it ? @cpojer @thymikee
I had a look into this issue and it seems that Mongoose creates an object with a key called
prototype
and sets it to 1: https://github.com/Automattic/mongoose/blob/master/lib/schema.js#L464It turns out that Jest doesn’t like this. I’ve created a minimal repro of the problem here: https://github.com/smably/jest-prototype-test
Adding another check for
typeof mock.prototype === 'object'
does seem to fix this issue (thanks @hluedeke!). If no objections, I’d also be happy to open up a PR with that fix.@hluedeke seems like the
--fix
is added tojest
and noteslint
… You can runyarn lint --fix
and it should be fixed@smably I’d be eternally grateful! I forked and updated it…but can’t get all the jest tests to pass 😒 (even with periodic pulls)…so I’ve been hesitant to submit it
+1 I have the same issue with built-in auto-mock…, but I just manually mock directly upon the method I want to stub.