Skip to content
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

Problems with migrating to 5.0 version due to issues with webpack configuration #8373

Closed
azakordonets opened this issue Aug 21, 2020 · 11 comments

Comments

@azakordonets
Copy link

Current behavior:

We are trying to migrate to Cypress 5.0 and having some issues with webpack compilation. Here's what we did:

  1. Installed webpack-preprocessor
  2. Added it to our plugins/index.js
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)
// eslint-disable-next-line

// promisified fs module
const fs = require('fs-extra')
const path = require('path')

const webpackPreprocessor = require('@cypress/webpack-preprocessor')
const defaults = webpackPreprocessor.defaultOptions

function getConfigurationByFile(file) {
    const pathToConfigFile = path.resolve('cypress', 'config', `${file}.json`)

    return fs.readJson(pathToConfigFile)
}

function islogableError(event) {
    if (event.url === undefined) {
        return true
    }
    return !(
        event.url.indexOf('google-analytics.com') > 0 ||
        event.url.indexOf('new10.blueconic.net') > 0 ||
        event.url.indexOf('player.vimeo.com') > 0
    )
}

// plugins file
module.exports = (on, config) => {
    // `on` is used to hook into various events Cypress emits
    // `config` is the resolved Cypress config
    require('cypress-terminal-report').installPlugin(on)
    require('cypress-log-to-output').install(on, (type, event) => {
        // return true or false from this plugin to control if the event is logged
        // `type` is either `console` or `browser`
        // if `type` is `browser`, `event` is an object of the type `LogEntry`:
        //  https://chromedevtools.github.io/devtools-protocol/tot/Log#type-LogEntry
        // if `type` is `console`, `event` is an object of the type passed to `Runtime.consoleAPICalled`:
        //  https://chromedevtools.github.io/devtools-protocol/tot/Runtime#event-consoleAPICalled

        return (event.level === 'error' || event.type === 'error') && islogableError(event)
    })
    delete defaults.webpackOptions.module.rules[0].use[0].options.presets
    on('file:preprocessor', webpackPreprocessor(defaults))

    const environment = process.env.ENVIRONMENT || config.env.ENVIRONMENT || 'acceptance'
    console.log(`plugins/index.js: Loading local config from cypress/config/${environment}.json...`)
    return getConfigurationByFile(environment)
}

When we run `$(yarn bin)/cypress open and run any of our tests, the browser crashes and in the debug log we see

 Error: Can't resolve 'fs' in '/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/src/outputProcessor'

error. Full log attached:

➜ DEBUG=cypress:webpack $(yarn bin)/cypress open
  cypress:webpack typescript not found +0ms
  cypress:webpack get default options +0ms
  cypress:webpack load default options +0ms
  cypress:webpack user options: { webpackOptions: { mode: 'development', module: { rules: [Array] } }, watchOptions: {} } +50ms
plugins/index.js: Loading local config from cypress/config/dev.json...
[cypress-log-to-output] Attempting to connect to Chrome Debugging Protocol
[cypress-log-to-output] Connected to Chrome Debugging Protocol
GET /__/ 200 4.262 ms - -
GET /__cypress/runner/cypress_runner.css 200 3.217 ms - -
GET /__cypress/runner/cypress_runner.js 200 1.249 ms - -
GET /__cypress/iframes/integration/regular/approveAndBeyond/bv-complex-multiUBO-with-Spouse-e2e.js 200 2.644 ms - -
GET /__cypress/runner/fonts/fa-solid-900.woff2 200 1.181 ms - 76120
  cypress:webpack get /Users/andrew/Work/New10/Testing/cypress/cypress/integration/regular/approveAndBeyond/bv-complex-multiUBO-with-Spouse-e2e.js +1m
  cypress:webpack load default options +0ms
  cypress:webpack setting devtool to inline-source-map +1ms
  cypress:webpack webpackOptions: { mode: 'development', module: { rules: [ [Object] ] }, entry: [ '/Users/andrew/Work/New10/Testing/cypress/cypress/integration/regular/approveAndBeyond/bv-complex-multiUBO-with-Spouse-e2e.js' ], output: { path: '/Users/andrew/Library/Application Support/Cypress/cy/production/projects/cypress-6a748727a98c889f97baeeb385d13b8d/bundles/cypress/integration/regular/approveAndBeyond', filename: 'bv-complex-multiUBO-with-Spouse-e2e.js' }, devtool: 'inline-source-map' } +0ms
  cypress:webpack watchOptions: {} +0ms
  cypress:webpack input: /Users/andrew/Work/New10/Testing/cypress/cypress/integration/regular/approveAndBeyond/bv-complex-multiUBO-with-Spouse-e2e.js +0ms
  cypress:webpack output: /Users/andrew/Library/Application Support/Cypress/cy/production/projects/cypress-6a748727a98c889f97baeeb385d13b8d/bundles/cypress/integration/regular/approveAndBeyond/bv-complex-multiUBO-with-Spouse-e2e.js +3ms
  cypress:webpack watching +100ms
  cypress:webpack compile /Users/andrew/Work/New10/Testing/cypress/cypress/integration/regular/approveAndBeyond/bv-complex-multiUBO-with-Spouse-e2e.js +2ms
  cypress:webpack get /Users/andrew/Work/New10/Testing/cypress/cypress/support/index.js +13ms
  cypress:webpack load default options +0ms
  cypress:webpack setting devtool to inline-source-map +0ms
  cypress:webpack webpackOptions: { mode: 'development', module: { rules: [ [Object] ] }, entry: [ '/Users/andrew/Work/New10/Testing/cypress/cypress/support/index.js' ], output: { path: '/Users/andrew/Library/Application Support/Cypress/cy/production/projects/cypress-6a748727a98c889f97baeeb385d13b8d/bundles/cypress/support', filename: 'index.js' }, devtool: 'inline-source-map' } +1ms
  cypress:webpack watchOptions: {} +0ms
  cypress:webpack input: /Users/andrew/Work/New10/Testing/cypress/cypress/support/index.js +0ms
  cypress:webpack output: /Users/andrew/Library/Application Support/Cypress/cy/production/projects/cypress-6a748727a98c889f97baeeb385d13b8d/bundles/cypress/support/index.js +0ms
  cypress:webpack watching +3ms
  cypress:webpack compile /Users/andrew/Work/New10/Testing/cypress/cypress/support/index.js +1ms
  cypress:webpack get /Users/andrew/Work/New10/Testing/cypress/cypress/integration/regular/approveAndBeyond/bv-complex-multiUBO-with-Spouse-e2e.js +2ms
  cypress:webpack already have bundle for /Users/andrew/Work/New10/Testing/cypress/cypress/integration/regular/approveAndBeyond/bv-complex-multiUBO-with-Spouse-e2e.js +0ms
  cypress:webpack stats had error(s) +2s
  cypress:webpack errored bundling /Users/andrew/Library/Application Support/Cypress/cy/production/projects/cypress-6a748727a98c889f97baeeb385d13b8d/bundles/cypress/support/index.js Webpack Compilation Error
./node_modules/cypress-terminal-report/src/outputProcessor/BaseOutputProcessor.js
Module not found: Error: Can't resolve 'fs' in '/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/src/outputProcessor'
resolve 'fs' in '/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/src/outputProcessor'
  Parsed request is a module
  using description file: /Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/package.json (relative path: ./src/outputProcessor)
    Field 'browser' doesn't contain a valid alias configuration
    Looked for and couldn't find the file at the following paths:
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/src/outputProcessor/node_modules]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/src/node_modules]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/node_modules]
[/Users/andrew/Work/New10/Testing/node_modules]
[/Users/andrew/Work/New10/node_modules]
[/Users/andrew/Work/node_modules]
[/Users/andrew/node_modules]
[/Users/node_modules]
[/node_modules]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/node_modules/fs]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/fs]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/node_modules/fs.wasm]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/fs.wasm]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/node_modules/fs.mjs]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/fs.mjs]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/node_modules/fs.js]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/fs.js]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/node_modules/fs.json]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/fs.json]
 @ ./node_modules/cypress-terminal-report/src/outputProcessor/BaseOutputProcessor.js 1:11-24
 @ ./node_modules/cypress-terminal-report/src/outputProcessor/CustomOutputProcessor.js
 @ ./node_modules/cypress-terminal-report/src/installLogsPrinter.js
 @ ./node_modules/cypress-terminal-report/index.js
 @ ./cypress/support/index.js +2ms
  cypress:webpack - compile finished for /Users/andrew/Work/New10/Testing/cypress/cypress/support/index.js, initial? true +31ms
Webpack Compilation Error
./node_modules/cypress-terminal-report/src/outputProcessor/BaseOutputProcessor.js
Module not found: Error: Can't resolve 'fs' in '/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/src/outputProcessor'
resolve 'fs' in '/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/src/outputProcessor'
  Parsed request is a module
  using description file: /Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/package.json (relative path: ./src/outputProcessor)
    Field 'browser' doesn't contain a valid alias configuration
    Looked for and couldn't find the file at the following paths:
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/src/outputProcessor/node_modules]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/src/node_modules]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/node_modules]
[/Users/andrew/Work/New10/Testing/node_modules]
[/Users/andrew/Work/New10/node_modules]
[/Users/andrew/Work/node_modules]
[/Users/andrew/node_modules]
[/Users/node_modules]
[/node_modules]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/node_modules/fs]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/fs]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/node_modules/fs.wasm]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/fs.wasm]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/node_modules/fs.mjs]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/fs.mjs]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/node_modules/fs.js]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/fs.js]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/cypress-terminal-report/node_modules/fs.json]
[/Users/andrew/Work/New10/Testing/cypress/node_modules/fs.json]
 @ ./node_modules/cypress-terminal-report/src/outputProcessor/BaseOutputProcessor.js 1:11-24
 @ ./node_modules/cypress-terminal-report/src/outputProcessor/CustomOutputProcessor.js
 @ ./node_modules/cypress-terminal-report/src/installLogsPrinter.js
 @ ./node_modules/cypress-terminal-report/index.js
 @ ./cypress/support/index.js
GET /__cypress/tests?p=cypress/support/index.js 200 2395.327 ms - -
GET /__cypress/tests?p=cypress/integration/regular/approveAndBeyond/bv-complex-multiUBO-with-Spouse-e2e.js - - ms - -
The automation client disconnected. Cannot continue running tests.

However, in the cypress itself we see this :

Screenshot 2020-08-21 at 11 02 27

Desired behavior:

From what i understand, we need to setup webpack config in some way, but i can't figure out in which exactly way. Your help would be much appreciated

Versions

Operating system : Mac OS Catalina 10.15.6
Cypress version: 5.0

Our dependencies :

"devDependencies": {
    "@babel/core": "^7.11.4",
    "@babel/preset-env": "^7.11.0",
    "@cypress/webpack-preprocessor": "^5.4.4",
    "babel-eslint": "^7.1.1",
    "babel-loader": "^8.1.0",
    "cypress": "5.0.0",
    "cypress-failed-log": "2.7.0",
    "cypress-promise": "^1.0.2",
    "eslint": "^3.14.0",
    "eslint-plugin-prettier": "^2.7.0",
    "faker": "^4.1.0",
    "flatted": "^2.0.0",
    "fs-extra": "^7.0.1",
    "husky": "^0.14.3",
    "lint-staged": "^4.0.2",
    "mocha": "^5.0.0",
    "mochawesome": "^3.1.2",
    "prettier": "^1.18.2",
    "prettier-eslint": "^6.4.2",
    "webpack": "^4.44.1"
  },
  "dependencies": {
    "@new10com/test-state-generator": "1.0.1239-2020-06-15-12-10-20",
    "assert": "^1.5.0",
    "aws-sdk": "^2.554.0",
    "axios": "^0.18.1",
    "axios-delay": "^1.0.0-rc6",
    "chai-almost": "^1.0.1",
    "cypress-localstorage-commands": "1.2.1",
    "cypress-log-to-output": "^1.1.1",
    "cypress-terminal-report": "1.4.2",
    "form-data": "^3.0.0",
    "jspdf": "^1.5.3",
    "lodash": "^4.17.11",
    "mkdirp": "^0.5.1",
    "mochawesome-merge": "^1.0.7",
    "moment": "^2.27.0",
    "qs": "^6.9.3",
    "query-string": "^5.0.0",
    "react-is": "^16.8.6",
    "request": "^2.81.0",
    "telerivet": "^1.2.1",
    "yargs": "^8.0.2"
  }```
@jennifer-shehane
Copy link
Member

The first error looks like it's being thrown from cypress-terminal-report.

Can you narrow down your example? Try removing some of the plugins to see if the errors still exist? Want to narrow down if it's an issue with one of the plugins or with Cypress.

@jennifer-shehane jennifer-shehane added the stage: needs information Not enough info to reproduce the issue label Aug 21, 2020
@azakordonets
Copy link
Author

Well, originally at the beginning we had a different package complaining on the same reason. We have an api client that we have generated and added to our test framework. When we run tests, we get :

Error: Webpack Compilation Error
./node_modules/@new10com/test-state-generator/src/index.js
Module not found: Error: Can't resolve 'ApiClient' in '/Users/andrew/Work/New10/Testing/cypress/node_modules/@new10com/test-state-generator/src'
resolve 'ApiClient' in '/Users/andrew/Work/New10/Testing/cypress/node_modules/@new10com/test-state-generator/src'
  Parsed request is a module
  using description file: /Users/andrew/Work/New10/Testing/cypress/node_modules/@new10com/test-state-generator/package.json (relative path: ./src)
    resolve as module
      /Users/andrew/Work/New10/Testing/cypress/node_modules/@new10com/test-state-generator/src/node_modules doesn't exist or is not a directory
      /Users/andrew/Work/New10/Testing/cypress/node_modules/@new10com/test-state-generator/node_modules doesn't exist or is not a directory
      /Users/andrew/Work/New10/Testing/cypress/node_modules/@new10com/node_modules doesn't exist or is not a directory
      /Users/andrew/Work/New10/Testing/cypress/node_modules/node_modules doesn't exist or is not a directory
      /Users/andrew/Work/New10/Testing/node_modules doesn't exist or is not a directory
      /Users/andrew/Work/New10/node_modules doesn't exist or is not a directory
      /Users/andrew/Work/node_modules doesn't exist or is not a directory
      /Users/andrew/node_modules doesn't exist or is not a directory
      /Users/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory
      looking for modules in /Users/andrew/Work/New10/Testing/cypress/node_modules
        using description file: /Users/andrew/Work/New10/Testing/cypress/package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: /Users/andrew/Work/New10/Testing/cypress/package.json (relative path: ./node_modules/ApiClient)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/andrew/Work/New10/Testing/cypress/node_modules/ApiClient doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/andrew/Work/New10/Testing/cypress/node_modules/ApiClient.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              /Users/andrew/Work/New10/Testing/cypress/node_modules/ApiClient.json doesn't exist
            .jsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/andrew/Work/New10/Testing/cypress/node_modules/ApiClient.jsx doesn't exist
            .coffee
              Field 'browser' doesn't contain a valid alias configuration
              /Users/andrew/Work/New10/Testing/cypress/node_modules/ApiClient.coffee doesn't exist
            as directory
              /Users/andrew/Work/New10/Testing/cypress/node_modules/ApiClient doesn't exist

Error. I checked the changelog of cypress 5.0 and saw this :

Cypress no longer supports file paths with a question mark ? in them. We now use the webpack preprocessor by default and it does not support files with question marks. Addressed in #7982.

I checked out project and googled a bit my error and saw that this is related to Webpack configuration. Since we didn't have Webpack before, i have added it like i specified in the description of the issues. After i applied this change i found that the same issue is still appearing, but now different package is complaining on this ( probably this package was first in order to get loaded, so Webpack failed earlier). To me it looks like there should be some specific webpack configuration added in order to properly load all the modules, but i'm strugling with this. So i was hoping that you could help.

@jennifer-shehane
Copy link
Member

We'll need a smaller reproducible example - to track down exactly why the error is occuring in order to help. Can you provide what you're running now?

@azakordonets
Copy link
Author

What i'm running now is quite big and complex, but i will try to come up with something smaller.

@azakordonets
Copy link
Author

Ok, @jennifer-shehane i managed to reproduce the issue:

  1. Please pull my branch - https://github.com/azakordonets/cypress-test-tiny/tree/webpack-compiler-error-issue
  2. Run yarn cypress:open
  3. Run the test.

@bobmacneal
Copy link

bobmacneal commented Aug 26, 2020

I'm seeing a similar issue here. Rolling back to v4.11.0 until this is resolved.

@azakordonets
Copy link
Author

@jennifer-shehane any chance you can check the example i have provided?

@chrisbreiding
Copy link
Contributor

chrisbreiding commented Sep 8, 2020

@azakordonets I checked out the example and it seems like it can be fixed by changing the spec to the following:

import SwaggerPetstore from 'swagger-petstore'

/// <reference types="cypress" />
describe('page', () => {
  it('works', () => {
    const pet = new SwaggerPetstore.PetApi();
    cy.log(pet)
    cy.visit('https://example.cypress.io')
  })
})

@azakordonets
Copy link
Author

@chrisbreiding thanks for the tip! It did actually solved the problem, but not all the problems i'm having unfortunately. At first with PetStore i was trying to simulate the easiest and simplest example to reproduce the error. However, in our project we have another generated api client, that has the same structure and setup as Petstore api client, but it doesn't want to work and I can't figure out why. I have pushed a new setup for the issue into https://github.com/azakordonets/cypress-test-tiny/tree/webpack-compiler-error-issue branch. I'm banging my head over and i can't figure out what is the difference and why Webpack is complaining about this. Maybe you could shed the light on it ?

@chrisbreiding
Copy link
Contributor

Looks like swagger-generated code and webpack don't play well together. They have a workaround in their documentation here.

Here's how to implement it with Cypress.

Install the necessary dependencies:

npm i -D @cypress/webpack-preprocessor @cypress/webpack-batteries-included-preprocessor

Add the following to cypress/plugins/index.js:

const wp = require('@cypress/webpack-batteries-included-preprocessor')

module.exports = (on, config) => {
  const options = wp.defaultOptions

  options.webpackOptions.module.rules.push({
    parser: { amd: false },
  })

  on('file:preprocessor', wp(options))
}

I'll have to research whether or not it makes sense to bake this into Cypress or whether it will end up breaking projects that rely on webpack supporting AMD.

@azakordonets
Copy link
Author

Omg! @chrisbreiding you are the wizzard :) I still find Webpack a dark box of magic, but the fix you suggested did that trick and i don't see this Webpack compilation error anymore. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants