-
Notifications
You must be signed in to change notification settings - Fork 75
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
--require not working as expected #1
Comments
There’s an issue with mocha-webpack where requiring the spec helper in the config file doesn’t make its changes visible to the actual specs. See zinserjan/mocha-webpack#1.
Yep, that's the reason. This issue is caused by webpack when node_modules are not excluded from bundling. Then you have 2 versions of your node_modules and webpack uses it's own bundled version...
This includes your specHelper to webpacks bundle and affects the used modules for the initial run. But when you change a file, webpack rebuilds the bundle and only those files which are affected by this change will be executed again. Excluding node_modules feels more like a workaround, but it's the simplest solution for this issue. Have a look at webpack-node-externals to exclude them. The bundle process is also faster with this plugin ;) For a real solution, we need to make sure that node & webpack shares their instances of modules - when needed. |
I originally tried using webpack-node-externals as you show in the README, but ran into other issues. I can't remember what they were off the top of my head, but I'll re-try that when I get a chance and report back. |
OK, I've had a chance to re-investigate, and the problem was unrelated to this module. We're using eslint-plugin-import, and when we use webpack-node-externals, the |
While the [issue I opened on mocha-webpack](zinserjan/mocha-webpack#1) is still outstanding, the author’s suggestion is to use `webpack-node-externals`. We tried that initially, but that results in numerous firings of the `import/no-unresolved` lint rule. After conferring with @lexun, we decided to go back to that approach and instead disable the linters when doing `npm run test`. They still run with `npm run build`, `npm run test:debug`, and `npm run dev`. If the above-mentioned issue gets resolved, then we can go back to running the linters in all environments.
@zinserjan Do you have any ideas on how to accomplish what you said above?
I'm happy to try to work on a PR for this, but I'm not sure where to start with it. If you could point me in a direction, I'll see what I can do. |
Thought a lot about this the last days and I'm still not sure how to solve it... The main problem is, that webpack creates it's own js bundle with all necessary modules. If we use I see the following solutions for this problem:
The more I think about the first solution the more I come to the result, that there will probably never a bullet-proof solution for this. In theory it would be enough to externalize all used dependencies in Second way feels more a like a hack than a solution. Third is probably the way to go. It just includes the code into webpack's bundle, so it just works for use cases like spec helpers. Shared instances are also possible - with a custom externals configuration. What do you think? |
My knowledge of how webpack works is pretty weak, but I think option 3 sounds best as well. What's not clear to me is whether the I don't have enough knowledge or understanding of webpack to know whether there will be other undesirable effects of doing this. I can try to come up with something along these lines when I get some open-source time, but if you see a clear path to implementing this, then go for it. |
Yes! That's the idea. Added a PR for this. But before I'll merge this, I wanna test it on windows and try it with a real project. |
I just tried out your PR branch on one of my projects, and it worked perfectly. Thanks for that! I don't have a Windows setup to test on, but consider it successful on a real project! |
Cool. Windows looks good, too. Released as 0.3.0 |
Updated all of my affected projects, and everything works great! Thanks so much for solving this issue! |
Thank you so much for this package! It is making my testing workflow so much better!
I'm running into an issue with
--require
. I have the followingmocha-webpack.opts
file:My
specHelper.js
file is being required properly, but its effects are not being made visible to my tests when they run.specHelper.js
looks like this:When I run my tests, I get the error:
TypeError: (0 , _chai.expect)(...).to.become is not a function
, which means thatchai-as-promised
is not available.When I previously ran my specs using mocha directly, the
--require
option worked as I expected.If I rename
specHelpler.js
tospecHelper-spec.js
, then it works as I expect; however, if I runmocha-webpack
with the--watch
option and then change a test, I get the same error message again because thespecHelper-spec.js
file wasn't reloaded by--watch
.At this point, the only workaround I can think of is to explicitly import my spec helper into all of my test files.
I've had a look at the code, and I don't see any obvious problems with it. Somehow it seems like the webpack-compiled code doesn't have access to the
chai
object that was configured by the spec helper.The text was updated successfully, but these errors were encountered: