-
-
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
Cannot mock chalk #471
Comments
I'm actually running into this issue a lot. Modules are failing to mock because some other dependency that they have have to run as part of initialization. The problem becomes is that you're trying to call a mock which returns undefined, and then you're trying to access a property of undefined (see OP). Essentially, we're trying to mock the interface of a given node module, but we end up mocking everything underneath the interface as well. That realization lead to the idea that inline-requires will make things a lot faster (if it's not used in initialization then we don't need to mock it that early on). Inline-requires will actually mitigate part of this issue (if we don't have to require everything, there less of a chance that we hit a module that has this initialization mocking bug). What we need to do to support mocking these kind of modules is that we need not to mock it's dependencies, we just let everything execute untouched and finally mock the interface. |
I encountered this issue too. Really annoying. Because of this, I abandoned all hope of using Jest. I'd love to know how to use Jest if your code executes the dependency immediately and tries to use a property on that object, as above. |
The documentation for this is going to be vastly improved with Jest 0.9.0, so I'm going to close out this issue. Some modules simply cannot be mocked properly because of the meta-programming they do. I'm quite convinced in your example you either used npm3 or you deduped your npm2 dependencies, causing I agree with you that mocking is sometimes a pain. I'm hoping to improve both the mocker and the documentation further in the future, but I don't think there is anything actionable left to do in this issue. See #763 for the documentation updates. |
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. |
Modules doing any kind of initializations in their code are bound to fail to be mocked.
https://github.com/chalk/has-ansi/blob/77fd627fb18b9740518a7bb65e9c3a68b9e51cf2/index.js#L3
In the following
ansiRegex()
will return undefined because it's a mockThe text was updated successfully, but these errors were encountered: