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

unable to require('when') (cujojs/when) in test #216

Closed
honzatrtik opened this issue Jan 16, 2015 · 6 comments
Closed

unable to require('when') (cujojs/when) in test #216

honzatrtik opened this issue Jan 16, 2015 · 6 comments

Comments

@honzatrtik
Copy link

Hello!
My test file is as simple as

var when = require('when');

but still throws error even if i disable mocking using jest.dontMock('when')

/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/when/when.js: Cannot read property 'resolve' of undefined
    at /Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/when/when.js:35:28
    at /Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/when/when.js:229:96
    at /Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/when/when.js:11:1
    at /Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/when/when.js:229:3
    at Object.runContentWithLocalBindings (/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/src/lib/utils.js:357:17)
    at Loader._execModule (/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:245:9)
    at Loader.requireModule (/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:900:12)
    at Loader.requireModuleOrMock (/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:921:17)
    at /Users/honzatrtik/Workspace/bandzone3/src/www/js2/src/components/gig-list/__tests__/filter-genre-group.js:13:12
    at Object.runContentWithLocalBindings (/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/src/lib/utils.js:357:17)
    at Loader._execModule (/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:245:9)
    at Loader.requireModule (/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:900:12)
    at jasmineTestRunner (/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/src/jasmineTestRunner/jasmineTestRunner.js:239:16)
    at /Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/src/TestRunner.js:371:12
    at _fulfilled (/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/node_modules/q/q.js:798:54)
    at self.promiseDispatch.done (/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/node_modules/q/q.js:827:30)
    at Promise.promise.promiseDispatch (/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/node_modules/q/q.js:760:13)
    at /Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/node_modules/q/q.js:574:44
    at flush (/Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/node_modules/q/q.js:108:17)
    at /Users/honzatrtik/Workspace/bandzone3/src/www/js2/node_modules/jest-cli/src/lib/FakeTimers.js:259:7
    at process._tickCallback (node.js:419:13)

Is there any workaround?

@wwalser
Copy link
Contributor

wwalser commented Jan 18, 2015

https://github.com/cujojs/when/blob/master/when.js#L22

I'm pretty sure jest.dontMock('when') will still mock everything between lines 13 and 22. In this case I think the solution is to use config.unmockedModulePathPatterns. See: https://facebook.github.io/jest/docs/api.html#config-unmockedmodulepathpatterns-array-string

@benzen
Copy link

benzen commented Feb 14, 2015

I get into the same trap, thanks to @wwalser it's was easy to get out.
Still, it seems to me that even if the module that my moduleUnderTest depends are mocked out.
The module they depend on are still required (i mean node's required) and then mocked out.

Maybe it will be clearer as a draw:
A -> B -> C

The module i'm testing is A. When testing it, i'm expecting B to be mocked and not to require C for real.
But it seams to me that what is happening is this:

A -> mock(B) -> B -> mock(C) -> C

Is this because of the automatic construction of the mock ?

Also, it seems to me that the doc pointed by @wwalser has some syntaxe issue, almost always and almost all the time

@wwalser
Copy link
Contributor

wwalser commented Feb 14, 2015

It sounds like you've already caught on to what is happening, your illustration is close to accurate. Actually the real requires of b and c happen before the mock can be constructed.

They are required into a sandboxed context and inspected in order to create a mocked version. It's expected behavior.

In every case that I've seen this is only a problem when simply requiring a module causes side effects which I tend to treat as an antipattern in my own code.

@browniefed
Copy link
Contributor

I've added when to the unmockedModulePatterns as well as other variations of regular expressions to attempt to not mock out lines 13-22 however nothing seems to be working. Is there a documented unmockedModulePatterns for this somewhere?

@cpojer
Copy link
Member

cpojer commented Mar 3, 2016

This will be fixed in 0.9.0. You can try it with jest-cli@next already. Otherwise I recommend unmocking it using unmockedModulePathPatterns.

@github-actions
Copy link

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.
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 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants