I am using NX 14.5.1 and cypress 10.2.0. When I run cypress component testing for "libs/ui" always got "Process not defined" error. In the component call ".env" like this:
import consola from 'consola'
export const logger = consola.create({
level: process.env.NX_ENV_NAME === 'production' ? 0 : 5
})
This is my "cypress.config.ts":
import { defineConfig } from 'cypress';
import { nxComponentTestingPreset } from '@nrwl/react/plugins/component-testing';
export default defineConfig({
component: {
...nxComponentTestingPreset(__dirname)
}
})
And the error is like this: process is not defined
ReferenceError
The following error originated from your test code, not from Cypress.
> process is not defined
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.
I think Cypress doesn't recognize my ".env". How do I pass my ".env" when I run component testing?
I think the basic problem is the app is server-side-rendered. The server compiles in Node where
processis available, but the component test runs in the browser whereprocessisn't valid.This is the way you might tackle it:
In
cypress.config.jsload the process.env you need into Cypress.envIn the component, check where the component is being run from.
This is messy but removes the restriction on running SSR code in a browser environment.