cypress: webpack v5 error when module contains dynamic import: "Automatic publicPath is not supported in this browser"
Current behavior
When using webpack v5 and a test contains a dynamic import (or imports a module that contains a dynamic import) the test will fail with the following error: Automatic publicPath is not supported in this browser.
Running: example.test.js (1 of 1)
1) An uncaught error was detected outside of a test
0 passing (214ms)
1 failing
1) An uncaught error was detected outside of a test:
Error: The following error originated from your test code, not from Cypress.
> Automatic publicPath is not supported in this browser
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test.
We dynamically generated a new test to display this failure.
at eval (http://localhost:61013/__cypress/tests?p=cypress/integration/example.test.js:155:34)
at eval (http://localhost:61013/__cypress/tests?p=cypress/integration/example.test.js:158:13)
at eval (http://localhost:61013/__cypress/tests?p=cypress/integration/example.test.js:261:12)
at eval (<anonymous>)
Desired behavior
No error.
Test code to reproduce
Full reduced test case: https://github.com/OliverJAsh/cypress-webpack-5-public-path-error
This might seem like a contrived example because I have reduced it down from a much larger use case.
cypress/integration/lazy.js:
export const a = 1;
cypress/integration/example.test.js:
() => import('./lazy');
describe("foo", () => {
it("works", () => {});
});
cypress/plugins/index.js:
const webpackPreprocessor = require("@cypress/webpack-preprocessor");
module.exports = (on, config) => {
on(
"file:preprocessor",
webpackPreprocessor({
webpackOptions: {},
})
);
};
cypress.json:
{}
package.json:
{
"dependencies": {
"@cypress/webpack-preprocessor": "^5.9.1",
"cypress": "^8.5.0",
"webpack": "^5.58.1"
}
}
Cypress Version
8.5.0
Other
I was able to workaround the error by setting publicPath: '' inside of here:
I’m not really sure I understand why this issue happens nor how this fixes the issue, but FWIW this fix was recommended here: https://stackoverflow.com/questions/64294706/webpack5-automatic-publicpath-is-not-supported-in-this-browser?rq=1.
The issue does not reproduce when using webpack v4.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 15 (4 by maintainers)
Commits related to this issue
- Disable Cypress integration tests for now Cypress tests are not working after upgrading to webpack 5 Reenable after bug is fixed by Cypress. See https://github.com/cypress-io/cypress/issues/18435 — committed to FixMyBerlin/fixmy.frontend by ohrie 2 years ago
- Release 2022-02-xx (#966) * Remove querystring dependencies (#925) * Remove query-string dependecy * Remove qs dependency and replace with URLSearchParams * Remove obsolet implicit boolean c... — committed to FixMyBerlin/fixmy.frontend by ohrie 2 years ago
- Release 2022-02-15 (#974) * Remove querystring dependencies (#925) * Remove query-string dependecy * Remove qs dependency and replace with URLSearchParams * Remove obsolet implicit boolean c... — committed to FixMyBerlin/fixmy.frontend by ohrie 2 years ago
- Try publicPath: '' workaround This workaround is mentioned in a few places: - https://stackoverflow.com/a/64715069 - https://github.com/cypress-io/cypress/issues/18435#issue-1022498688 — committed to department-of-veterans-affairs/component-library by bkjohnson 2 years ago
- Fix react build public path (#423) * Try publicPath: '' workaround This workaround is mentioned in a few places: - https://stackoverflow.com/a/64715069 - https://github.com/cypress-io/cypress/... — committed to department-of-veterans-affairs/component-library by bkjohnson 2 years ago
Is there any update on this? Seems like it’s breaking even when I’m not using
mini-css-extract-plugin.@Circle6 copying this over from another issue. Add the following code into your
plugins/index.jsfileMy complete
plugins/index.jsconfig file looks like this (Angular + Nx):I’ve tried running through all the given solutions here and i’m still seeing this issue. I’m even explicitly setting the publicPath and it’s still throwing “Automatic publicPath is not supported in this browser”.
Any progress on fixing this?
I think there are multiple ways that you can end up triggering webpack’s automatic
publicPathdetection, which then fails with the not-very-helpful error message from the issue title. Its definitely not justmini-css-extract-plugin.Instead of trying to override the webpack configuration for all webpack core/plugins that have a
publicPathsetting, I have the following vile hack to trick webpack’s automatic detection into not throwing an error. Putinto your
cypress/support/index.ts(or whereversupportFilefromcypress.jsonpoints to)Well since I wrote my comment an hour ago, I found a longer term workaround until a more permanent solution is provided that doesn’t involve manually modifying the webpack-preprocessor. I modified the inline webpack config for my
file:preprocessorby addingmini-css-extract-plugin.The
defineProperty(webpackConfig, 'output')hack worked for me. My implementation was different enough from the prior example that I’ll include it here to help others.Here’s a generic
plugins/index.tsthat should work with an external webpack config file:Is there a reason why setting
publicPath: ''in https://github.com/cypress-io/cypress/blob/master/npm/webpack-preprocessor/index.ts#L204 has never been done, or is it simply because nobody bothered to make a PR ? This is the only workaround that works for me.Thanks for the report and detailed investigation, webpack errors are always hard to track down.
The change to @cypress/webpack-preprocessor looks fairly straightforward, if anyone wants to submit a PR we’d be happy to review and get it in. Otherwise, I’ve added this to our list to discuss, but can’t promise when the team will get around to a fix, especially since @thril has provided a working / less hacky solution.