-
Notifications
You must be signed in to change notification settings - Fork 220
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
Some of my webpack app assets (images, workers) are not being loaded which leads to broken tests #498
Comments
Can you post your karma-webpack config? |
@codymikol it is shared between the app itself and karma and it seems fairly generic despite a bunch of loaders:
|
Sorry, I mostly wanted to see your karma configuration. I'm wondering if the image files are not included in your files list, this would mean that they can't be associated to the webpack build. |
@codymikol oh no worries, at this point I am happy with any advice or direction to research as the issue is a blocker for our team. So here is the karma.conf.js:
where Our
I wonder if the problem is that we use this approach? The |
@codymikol I used http://karma-runner.github.io/latest/config/files.html as a reference and included my assets into
So now I can access my images with URL like http://localhost:9876/base/app/assets/images/logo-black-trademark.svg No more 404 for these URLs, which is great! But my problem is still not solved. When I debug the tests I see that URLs like are being used to load my assets, but I need this URL http://localhost:9876/base/app/assets/images/logo-black-trademark.svg I am not sure if I can overcome this with karma
|
Trying all the wild ideas like generating the path to tmpdir ahead of time and passing it to karma-webpack. So I can use the path to configure proxies
but it still does not work. I don't think proxies should be used like this?... |
Oh man, I tried the wildest assumption and it worked out, which is great and also bad because it looks so dirty. So taking my latest approach by owning the webpack
it warns me in the console but it works!
@codymikol this seems like something could be dealt with at the default behavior of this repo. So I guess my bug request is valid. Would it be possible to ensure that by default karma |
The way karma-webpack currently works is that it maps the bundled files created by the webpack build process back to the files specified in the At this time I don't believe there is a way to modify the original list of files (specified in What I'd like to work on for a v6 of this plugin is to make the initial webpack step be the provider of the Hopefully I'll have some time this week and I can see if there is some simple workaround for making assets work that won't require pointing back to the temporary build. |
I'm running into this as well and it's pretty much completely derailing a general upgrade to Webpack v5. Is there anything I can do to assist? In our case, we have to fetch certain files from the Karma server and we are getting 404's back. I had thought that it was maybe related to removing webpack-dev-middleware but I wasn't sure. |
@jljorgenson18 have you tried this approach? #498 (comment) |
@eugenet8k, yep it did not work. |
@jljorgenson18 well, you gotta share your config files (karma.conf.js, and probably webpack). That's the essential minimum to guess what's going on. |
O scratch that @eugenet8k , I did just try your solution and it DID work. I had a misconfiguration where the publicPath was temporarily off. This is actually working. |
Quick note on this: this currently breaks all usage of wasm + webpack v5 + karma. Wasm files get included bundled automatically by webpack nowadays, and get copied to the output directory (just like the images discussed here) but karma refuses to serve them with a 404, so the import reliably fails & no tests can run. The workaround above (pass the webpack output path into to My project is actually a testing library often used with karma & webpack downstream, so I'm going to have to pass this workaround onwards to all my users too. It'd be amazing if the karma-webpack output files could all be added to |
Wow, thank you for your work on this! I ran into this same issue upgrading to Webpack 5 using Karma, because my web worker started 404'ing randomly. For others reading this down the road, to do what @eugenet8k did in this post, I needed to import const os = require('os');
const path = require('path');
const output = {
path: path.join(os.tmpdir(), '_karma_webpack_') + Math.floor(Math.random() * 1000000),
} I think Also, I needed to put the module.exports = function(config) {
config.set({
// other config stuff
webpack: Object.assign({}, webpackConfig, {
output,
optimization: {
runtimeChunk: false, // if you have this to get source maps working, DELETE IT, or you will get 404's again
}
// other webpack config stuff
})
});
}; |
@eugenet8k Thank you for sharing your solution. It helped me. function KarmaWebpackOutputFramework(config) {
// This controller is instantiated and set during the preprocessor phase.
const controller = config.__karmaWebpackController;
// only if webpack has instantiated its controller
if (!controller) {
console.warn(
"Webpack has not instantiated controller yet.\n" +
"Check if you have enabled webpack preprocessor and framework before this framework"
)
return
}
config.files.push({
pattern: `${controller.outputPath}/**/*`,
included: false,
served: true,
watched: false
})
}
const KarmaWebpackOutputPlugin = {
'framework:webpack-output': ['factory', KarmaWebpackOutputFramework],
};
config.plugins.push(KarmaWebpackOutputPlugin);
config.frameworks.push("webpack-output"); |
Expected Behavior
I have a number of SVG, PDF, JS workers, and other files in my webpack app. Obviously, it works all good while running the app normally, but when I get to run my mocha test with karma-webpack some of my tests fail due to failures with loading those mentioned asset-like files
Code
So when I run via command line I see errors like:
I run it using browser to see what's going on in Dev Tools, and see the same results:
So when I got curious to see what's in the
_karma_webpack_117318
folder, where I assumed it keeps my webpack assets. I found it in my macOS and indeed files are there:So these files
commons.js
andruntime.js
are loaded just fine, but not my files. This makes me think that when the browser doesGET http://localhost:9876/absolute/var/folders/qm/dx__qpt10rnd8rg2st400l200000gp/T/_karma_webpack_117318/runtime.js
it does not directly point to the disk folder. I made a test, by adding few lines of code intoruntime.js
to see if it will show up in the browser. It didn't. So I guess there is another memory cache or storage is used to serve the browser.Maybe the state of
_karma_webpack_117318
folder was cached before webpack finished the build, so my assets weren't cached in time and hence aren't served to browser?The general question is... Is this a karma-webpack bug? Or did I miss some config?
The text was updated successfully, but these errors were encountered: