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

Cypress component tests fail to run when using @react-data-table-component -- ^7.5.2 #22678

Closed
manishagupta016 opened this issue Jul 5, 2022 · 6 comments
Labels
CT Issue related to component testing type: bug

Comments

@manishagupta016
Copy link

manishagupta016 commented Jul 5, 2022

Current behavior

Running Cypress component tests (Cypress ^10.3.0) results in the following error message for all the tests:

"Cannot read properties of undefined (reading 'REACT_APP_SC_ATTR')"

at <unknown> (http://localhost:3000/__cypress/src/node_modules/.vite/deps/react-data-table-component.js?v=22eeef94:649:56)

image

Desired behavior

The cypress component tests should run correctly

Test code to reproduce

 "@cypress/react": "^6.0.0",
 "cypress": "^10.3.0",
 "vite": "^2.9.9",
"react": "^18.1.0",
"react-data-table-component": "^7.5.2",
 "styled-components": "^5.3.5",

Cypress Version

^10.3.0

Other

cypress config

 supportFile: "cypress/support/component.js",
        specPattern: "src/**/*.test.{js,jsx}",
        devServer  : {
            framework: "react",
            bundler  : "vite"
        },
        viewportHeight: 600,
        viewportWidth : 800,
        video         : false,

No response

@cypress-bot cypress-bot bot added the stage: investigating Someone from Cypress is looking into this label Jul 11, 2022
@astone123 astone123 added the CT Issue related to component testing label Jul 13, 2022
@astone123
Copy link
Contributor

This appears to be specific to Vite. Everything worked fine in my project using Webpack. I'm still looking into the issue

@astone123
Copy link
Contributor

Was able to reproduce this issue using React 17 and Vite with the dependencies mentioned. It looks like the actual error might be happening in Styled Components here.

@cypress-bot cypress-bot bot added stage: routed to ct and removed stage: investigating Someone from Cypress is looking into this stage: routed to ct labels Jul 13, 2022
@astone123 astone123 removed their assignment Jul 13, 2022
@lmiller1990
Copy link
Contributor

lmiller1990 commented Aug 4, 2022

Possibly related: styled-components/styled-components#3744

Seems this is a more general Vite problem than something specific to Cypress? I did get styled-components working in a fresh React 18 + Vite app, though. Hm.

@lmiller1990
Copy link
Contributor

lmiller1990 commented Aug 4, 2022

Update: I went into node_modules/styled-components/dist/styled-components.browser.esm.js and did console.log(typeof process) which gives object - this is not what you'd expect, in a regular Vite app, it's undefined. Something, somewhere, is defining a process object.

image

I wonder if https://www.npmjs.com/package/@emotion/is-prop-valid is getting pulled in somewhere, and defining process. I looked at the that code base, and it does not define process, but that could be our source maps just getting confused, or something.

@lmiller1990
Copy link
Contributor

lmiller1990 commented Aug 4, 2022

I couldn't find out what the problem is exactly, but I have a work around for now: https://github.com/cypress-io/cypress/compare/styled-components?expand=1

In your vite.config.js, you can defensively rewrite process.env to {} to ensure this error doesn't occur using @rollup/plugin-replace. Example commit.

import { defineConfig } from 'vite'
import replace from '@rollup/plugin-replace'
import reactPlugin from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [
    replace({
     values: {
        'process.env': `(process.env || {})`,
      }, 
    }),
    reactPlugin({
      jsxRuntime: 'classic',
    }),
  ],
})

I'm not sure if this has any side effects - there's a lot of ways to configure styled-components, but this works, at least with the minimal example I tested with:

image

@lmiller1990
Copy link
Contributor

I think this was related to Vite. Vite 3 is out, I wonder if this is fixed?

Happy to reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CT Issue related to component testing type: bug
Projects
None yet
Development

No branches or pull requests

5 participants