-
-
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
How to test with Jest when I'm using webpack #334
How to test with Jest when I'm using webpack #334
Comments
I have a related issue, in my case it is finding the file because I am using a relative path: But getting |
Not much of a solution, but you can require your css in main.js or app.js where you don't have tests (or refactor these to not have business logic to test). |
Yes, this is what I'm doing right now, I put the css at the very top level, to make my test pass. I wonder if anyone out there actually solve that issue in different way. |
Don't you need all the loaders for Sass in your webpack config? I didn't On Tuesday, April 21, 2015, Roy notifications@github.com wrote:
Joshua K Stoutenburg |
I have all the loaders in my webpack config, but when I'm running jest CLI, it isn't going through webpack. But let me see if I can instrument jest within webpack. But if you have successfully did that before, I would love to see it, thanks. |
Here is how I do var ReactTools = require('react-tools'); module.exports = { Hope it can help. |
Thanks zhengl, I also searched for further solution and someone has a similar issue, what he does is after the transform, he remove the line that is related to css, so the JEST won't see the css. http://stackoverflow.com/questions/28870296/how-to-use-jest-with-webpack Seems like it solve my problem by removing the thing I don't need to test, but is there a better way than this ? |
Actually, the S.O. approach seems like a fine solution to me - the actual CSS isn't really needed for testing. I want to test that the class names are or are-not in the rendered HTML, but it doesn't matter if the actual CSS declarations are present for tests. I updated my preprocessor to look like this:
|
+1 same here. I also ended up like the StackOverflow approach. Here is my final var coffee = require('coffee-react');
var transform = require('coffee-react-transform');
module.exports = {
process: function(src, path) {
if (coffee.helpers.isCoffee(path)) {
return coffee.compile(transform(src), {
'bare': true
});
} else if (path.match(/.scss/)) {
// Ignoring require'd SASS files
return '';
}
return src;
}
}; |
I'm using the 'usable' aspect, which made this even tricker. I'm still trying to figure out a better option, but here's how I accomplished it if anyone is interested: The component
The preprocessor from React Starter Kit
The jest code
|
Babel + Less + Webpack + Jest I write the new preprocessor. var babelJest = require('babel-jest');
module.exports = {
process: function(src, filename) {
return babelJest.process(src, filename)
.replace(/require\(\'[^\']+\.less\'\);/gm, '');
}
}; It is working for me. |
I had some mixed results with the solution on stack overflow due to the babel-jest output containing spaces after "require(". I enhanced the regular expression to handle those scenarios and also capture css, scss, and less. I published a package to npm called webpack-babel-jest in case it can help anybody else out. |
For the resolving directories part of the problem, here's a workaround which can resolve webpack paths in the preprocess phase: https://www.npmjs.com/package/jest-webpack-alias |
This works for me. var babelJest = require('babel-jest');
module.exports = {
process: function(src, filename) {
if (filename.match(/\.[css|less|scss]/)) {
return '';
}
return babelJest.process(src, filename);
},
}; |
I would greatly appreciate if someone would enlighten me, on the purpose of integrating jest and webpack. I've setup the configs ...etc as this article describes to, but I'm unclear as to what I'm suppose to expect. Is the integration solely for resolving requires, or when you run |
I got the same problem. |
I've done a little investigation into this. I think ColCh/jest-webpack has the cleanest approach. The biggest issue I can see however is that webpack now runs asynchronously, (i.e. Webpack's compiler takes a callback) whereas Jest expects any preprocessor to be synchronous because |
Hey @chrislloyd. I think with babel6 and the Jest+Babel auto-integration, there is little need to use webpack for processing. However, webpack might be doing some things that Jest does not and I'd like to collect a list of things that are missing from Jest that make it not work so well with your particular projects. Here is a list of things that would help:
My guess is that with |
@chrislloyd, maybe we are getting a bit off topic but you can use deasync as a workaround for the synchronous preprocessor / asynchronous webpack problem. Something like this works for me:
Disclaimer: Remember kids, you should never ever force synchronous execution on asynchronous implementations unless you understand the consequences. |
I'm experiencing the same problem. @cpojer Correct me if I'm wrong, but I can't see how it will be possible to get jest to play nicely with webpack without using webpack for processing. Webpack allows you to define any arbitrary loaders and resolve extensions which means the behaviour of require calls or imports will vary greatly dependant on the specific config. |
@sampeka: can you share a typical webpack configuration with all the special things require does in webpack? Then we can consider improving support for it. I think besides the consolidation around babel (and |
@cpojer any webpack config with loaders that lets you
which can result in app level code like:
|
@chrislloyd did @benweizhu's idea from above work for you - could return empty object for css modules, empty string for images? |
@psimyn that's what we're currently using. I thought it was initially less ideal as it couples our jest harness to our webpack config. However, thinking more about it, if the guard was inverted, it might actually be a decent solution as jest fundamentally can only mock JS.
|
After all this time we have finally implemented more support for webpack. I'm sorry for the long wait. I think we support all the features for module resolution well and testing a project with Jest when using webpack should work well, so we can close this issue at last. cc @jquense who offered to write some documentation for the website and who implemented this feature into jest-resolve. |
👏 I will get on it! |
Another example I've just run into working on a big project is shiming modules: https://webpack.github.io/docs/shimming-modules.html So my webpack config looks like: loaders: [
// Shims
{ test: /jssha256\-[\w\.]+js/, loader: 'exports?array_to_string&SHA256_hash&HMAC_SHA256_init&HMAC_SHA256_write&HMAC_SHA256_finalize' },
{ test: /modernizr\-custom\.js$/, loader: 'imports?this=>window!exports?window.Modernizr' },
{ test: /easepack\-[\w\.]+js$/, loader: 'imports?window=>{}!exports?Power1&Power2&Power3' }, etc. and then somewhere deep in the source code I have: import jssha256 from 'jssha256'; So webpack is handling wrapping those modules with I've spent quite some time on it already trying compiling using webpack: https://gist.github.com/ninjapanzer/70b55f90edcb69c74e52 but it doesn't seem to work for me yet. Any idea on a better approach? |
@okonet I'm facing the same issue when attempting to have Jest work with modules that webpack is shimming (i.e. using the imports-loader and exports-loader). Did you find a resolution to this issue? |
@dcalhoun this is what I did: https://gist.github.com/okonet/9b718e743a8e89560cd2003633765c73 |
@dcalhoun as a workaround for
Btw how should I import/mock correctly imports-loader? Do I really need to create webpack (I use webpack 2) config for jest?
Also asked about it here webpack-contrib/imports-loader#54 |
if anybody hits this issue in 2021 with webpack/peerDeps/and Jest, you can use
|
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. |
I'm using webpack and react, everything works fine until I do the unit test with Jest.
In one of my js files I have
And it said
Cannot find module 'style/pages/example_index.scss'
The following is my webpack config
The following is my configuration in package.json
The following is my folder structure
tests / ( host all my test )
src / js / ( host all my js )
The text was updated successfully, but these errors were encountered: