question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[7.6.0] Webpack Compilation Error

See original GitHub issue

Current behavior

After upgrading to Cypress 7.6.0 from 7.5.0, my tests can’t run anymore, failing to locate files I’m require-ing in my tests. I don’t use absolute path, but relative to the project root, so I don’t have to write require('../../../../my_dep') in my test files.

Desired behavior

I have custom babel.config.json, which helped to properly locate files.

{
    "presets": [
        [
            "@babel/preset-env",
            {
                "targets": {
                    "node": "current"
                }
            }
        ]
    ],
    "plugins": [
        [
            "module-resolver",
            {
                "root": [
                    "./"
                ],
                "alias": {
                    "framework": "./framework"
                }
            }
        ]
    ]
}

Test code to reproduce

  • Place a helper my_utils.js file in root of your project, that will return any code.
  • Have a test file placed in some sub-foldered structure, like ./tests/sub1/sub2/testfile.js, require-ing that helper file:
const utils = require('my_utils');
....
// any test code 

Try to run that test file.

Versions

Windows 10 Cypress 7.6.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
creagecommented, Jul 8, 2021

I did some investigation on this issue, and it looks like the way our test files are processed is changed in 7.6.0. All of js files are now going through the Webpack compiler (or were they doing that already before?), and there is no way to turn it off.

So, in order to make the setup I’ve presented above work again, I had to do these steps:

  1. Install @cypress/webpack-preprocessor
  2. Add custom Webpack options to cypress/plugins/index.js:
const webpackPreprocessor = require('@cypress/webpack-preprocessor');

module.exports = (on, config) => {

    const options = webpackPreprocessor.defaultOptions;

    Object.assign(options.webpackOptions, {
        resolve: {
            modules: [
                'node_modules',
                path.resolve(__dirname, '../../')
            ]
        }
    });

    on('file:preprocessor', webpackPreprocessor(options));
});

After these steps, my dependencies are back again, BUT I have to rewrite my tests to be ES6-compatible now!

Simply because

 // this code used to work before 7.6.0, since it was pure NodeJS processed
const dep = require('my_dep');

is not the same as

 // this is how code needs to be converted to work in 7.6.0, as now all of js files go through Webpack
import dep from 'my_dep';

in Webpack world.

We have ~1000 test files to refactor now, or just stick to 7.5.0.

0reactions
creagecommented, Aug 6, 2021

anyway, the easiest way to fix that was to add my “mappings” to the browser field in my package.json

"browser": {
    "framework/selectors": "./framework/selectors.js"
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get more detail than 'Webpack Compilation Error' with ...
Whenever there is an error inside one of my imported modules, all I get from Cypress is the message Error: Webpack Compilation Error...
Read more >
Changelog - Cypress Documentation
Compile errors are now surfaced in the command log during tests for Angular and Next projects. Fixes #23219. The error "Automatic publicPath is...
Read more >
webpack/webpack - Gitter
I'm running into an issue where webpack is placing some node_modules before the core-js modules and it ends up causing errors in IE11....
Read more >
@faker-js/faker - npm
Generate massive amounts of fake contextual data. Latest version: 7.6.0, last published: 3 months ago. Start using @faker-js/faker in your ...
Read more >
Firebase JavaScript SDK Release Notes - Google
Fixed some typings issues that caused compile errors for TypeScript users. ... an IndexedDB bug in Safari (https://bugs.webkit.org/show_bug.cgi?id=226547).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found