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

Cannot mock chalk #471

Closed
amasad opened this issue Aug 21, 2015 · 4 comments
Closed

Cannot mock chalk #471

amasad opened this issue Aug 21, 2015 · 4 comments

Comments

@amasad
Copy link
Contributor

amasad commented Aug 21, 2015

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 mock

'use strict';
var ansiRegex = require('ansi-regex');
var re = new RegExp(ansiRegex().source); // remove the `g` flag
module.exports = re.test.bind(re);
@amasad
Copy link
Contributor Author

amasad commented Aug 23, 2015

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.

cc @DmitrySoshnikov @cpojer @jeffmo

@volkanunsal
Copy link

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.

@cpojer
Copy link
Member

cpojer commented Mar 3, 2016

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 ansi-regex to be mocked. This will be fixed in 0.9.0 as well, so the original issue doesn't apply any longer.

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.

@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

3 participants