-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Jest mock module failed when mocking mongoose model object #3073
Comments
Does it happen on |
@thymikee yes, setting |
So it's probably connected with Jest running in VM context: #2549 |
Closing as a duplicate. |
Yeah, I agree. This still fails on the latest version of jest. I have no idea how or why, though 😀 The error comes from https://github.com/facebook/jest/blob/77d2e8e41ffa4a68dd7c1e3f81f5c4476c997c8a/packages/jest-mock/src/index.js#L620 |
+1 I have the same issue with built-in auto-mock..., but I just manually mock directly upon the method I want to stub. |
Same here 😞 |
I don't know much about Jest inner workings, but I can get my tests to run if I add a check to if (
metadata.type !== 'undefined' &&
metadata.type !== 'null' &&
mock.prototype && typeof mock.prototype === 'object'
) {
mock.prototype.constructor = mock;
} Would this break anything else? If not, then I could submit a PR if that would be helpful. EDIT EDIT |
…s interaction with mongoos
I have same issue. Does anyone have solution for it? |
I had a look into this issue and it seems that Mongoose creates an object with a key called It 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 |
Using jest.mock with module factory parameter works correctly:
For some cases that might be enough. |
@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 |
I didn't know this was possible, so thanks for the info! I'm updating this comment because the above doesn't actually address the problem. We are mocking 'mongoose' itself because if you don't mock the mongoose library, and you're using a singleton connection object, then mongoose will attempt to connect with every test suite run and will cause a jest timeout error. Mocking the whole mongoose library is the only way I've found to eliminate the connection attempt. |
What issue did you run into? I think all relevant docs are in in the contributing guide. Feel free to open up a PR and let CI run the tests though - even if they don't pass it might help someone else (or yourself) make progress on this issue 🙂 |
This is the current error I'm getting (even with the --fix option), though the test failures have been different as I've updated jest: I didn't want to go start "fixing" tests, since they aren't really related to my change, and I don't want to break something for someone else, so I've just been sitting on it. Thanks for the encouragement @SimenB to go ahead and PR anyway. I will go ahead and submit and see what happens! |
I don't know about the specifics of your code, in particular about singleton connection object, but given approach where module factory is used make tests from @harin 's repo pass and in general makes it possible to mock mongoose models using jest mocks. As far as I can see the initial issue was about using auto-mocked version of mongoose model which is just exported from another module. Sorry, but I can't see how any connection object is connected with given issue. See the repo please I guess @leoyli 's comment was about using the same approach. |
@hluedeke seems like the |
You are right, my apologies. It's an old thread that I commented on months ago - I shouldn't have made assumptions. I did not mean to demean in any way - was just trying to clear up that this issue still exists when importing 'mongoose'. Didn't mean to step on any toes! 😬 |
Fixed in #8040 |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Problem mocking a mongoose model Object with the following error.
repo with reproduction code
node - v6.9.3
yarn - v0.18.1
npm - 3.10.10
OS - macOS Sierra
The text was updated successfully, but these errors were encountered: