Skip to content
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

Removes error when mocking mongoose library root object #8040

Merged
merged 11 commits into from Mar 4, 2019

Conversation

hluedeke
Copy link
Contributor

@hluedeke hluedeke commented Mar 4, 2019

Summary

Allows mongoose library to be mocked using jest.mock('mongoose') without the error:

TypeError: Cannot create property 'constructor' on number '1'

Test plan

Used linked forked copy of jest with change with codebase that imported 'mongoose' and the tests ran without errors.

@hluedeke
Copy link
Contributor Author

hluedeke commented Mar 4, 2019

Resolves #3073

@SimenB
Copy link
Member

SimenB commented Mar 4, 2019

Thanks! Would be great to add a test for this. If all existing tests pass, maybe an integration test that has mongoose in it?


I merged in master to fix the conflict

@thymikee
Copy link
Collaborator

thymikee commented Mar 4, 2019

Does this makes us closer to fix the awful recommendation not to use Jest on their docs maybe? https://mongoosejs.com/docs/jest.html

@SimenB
Copy link
Member

SimenB commented Mar 4, 2019

Wow, harsh :P Hadn't seens the article. Would have been nice if they reached out first...

Meh, not gonna spend any energy on it

@mjesun
Copy link
Contributor

mjesun commented Mar 4, 2019

I'm super curious to know which truthy value that is not an object breaks when assigning to it .constructor :)

@hluedeke
Copy link
Contributor Author

hluedeke commented Mar 4, 2019

@mjesun #3073 (comment)

@SimenB
Copy link
Member

SimenB commented Mar 4, 2019

Yeah, just a property called prototype.

This reproduces

const jestMock = require('jest-mock');
const metadata = jestMock.getMetadata({prototype: 1});

jestMock.generateFromMetadata(metadata);

throws:

/Users/simen/repos/jest/packages/jest-mock/build/index.js:684
      mock.prototype.constructor = mock;
                                 ^

TypeError: Cannot create property 'constructor' on number '1'
    at ModuleMockerClass._generateMock (/Users/simen/repos/jest/packages/jest-mock/build/index.js:684:34)
    at ModuleMockerClass.generateFromMetadata (/Users/simen/repos/jest/packages/jest-mock/build/index.js:699:23)
    at Object.<anonymous> (/Users/simen/repos/jest/ugh.js:4:18)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)

@SimenB
Copy link
Member

SimenB commented Mar 4, 2019

I pushed that as a test :)

@hluedeke
Copy link
Contributor Author

hluedeke commented Mar 4, 2019

Beat me to it! 😜

@SimenB SimenB requested a review from mjesun March 4, 2019 17:43
@mjesun
Copy link
Contributor

mjesun commented Mar 4, 2019

Interestingly, it does not work only on use strict mode:

// Fails:
'use strict'; var x = 1; x.constructor = () => {};

// Works:
var x = 1; x.constructor = () => {};

I guess I'm too old-school by now 😄

@SimenB SimenB merged commit c15b7a5 into jestjs:master Mar 4, 2019
@SimenB
Copy link
Member

SimenB commented Mar 4, 2019

Thanks @hluedeke!

@saveman71
Copy link

Hey guys so this comment won't bring anything useful for the conversation, but I just wanted to let you know that because of this we finally can upgrade from mongoose v4.7.0, I've just ran our test suite successfully, and I couldn't be happier today.

Thanks a lot everyone involved, and thank you @hluedeke !!!

You really, really made my day. And kuddos for the fast release. I can't stress enough how it was for me to land on this old issue in our repo, land on this, see "Closed", then see "3 days ago" then go on the PR and see it's merged, and see it's released. Pure joy.

Cheers 🎉

@smably
Copy link

smably commented Mar 7, 2019

Agreed, this was an A+ open source experience! Huge thanks to both @hluedeke and the maintainers for such a quick and friendly fix. Y'all are amazing!

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants