github-action icon indicating copy to clipboard operation
github-action copied to clipboard

Enable support for Cypress tests that rely on workspace packages

Open mathieubruguier opened this issue 9 months ago • 1 comments

The GitHub Action fails to run Cypress tests in a pnpm monorepo workspace with the following error:

Error: Webpack Compilation Error
Module not found: Error: Can't resolve '@repo/mock/rules' in /home/runner/work/monorepo/monorepo/packages/e2e/cypress/constants'

Environment

  • cypress-io/github-action: v6
  • Cypress: v14.2.0
  • Package manager: pnpm
  • Project structure: Monorepo with workspace packages
  • OS: Ubuntu 22.04 (GitHub Action runner)

Project Structure

/monorepo
├── .github
│ └── workflows
│ └── web-e2e-deployed-environments.yml # GitHub Actions workflow file
├── packages
│ ├── mock # Mock package with KYC rules and scenarios
│ │ ├── package.json # Contains exports configuration
│ │ ├── src
│ │ │ └── handlers
│ │ │ └── kyc
│ │ │ ├── rules.ts # Exports RULES and SSNS used in tests
│ │ │ └── scenarios.ts # KYC mock scenarios
│ │ └── dist # Built JavaScript files
│ │
│ └── e2e # E2E test package using Cypress
│ │ ├── package.json
│ │ ├── cypress.config.ts
│ │ ├── cypress
│ │ ├── constants
│ │ │ └── kyc-mock-constants.ts # Imports from @repo/mock/rules
│ └── ...

The import causing the issue

From packages/e2e/cypress/constants/kyc-mock-constants.ts:

// Import using the exports field from package.json
import { RULES, SSNS } from '@repo/mock/rules';

From packages/mock/package.json:

"exports": {
  "./rules": {
    "types": "./src/handlers/kyc/rules.ts",
    "default": "./dist/handlers/kyc/rules.js"
  }
}

Steps to reproduce

  1. Have a pnpm monorepo with workspace packages (using "workspace:*" in package.json)
  2. Create a Cypress test that imports from a workspace package (e.g., import { RULES } from '@repo/mock/rules')
  3. Run the Cypress tests using the GitHub Action

Current workflow

# Install Cypress dependencies. Since we're in a monorepo, tell Cypress to look at the root package.json/lockfile
# Don't run the tests yet, we'll do that later in the proper directory
- name: Install Cypress dependencies
  uses: cypress-io/github-action@v6
  with:
    runTests: false

- name: Build mock package
  run: pnpm build --filter @repo/mock

- name: Run Cypress tests
  uses: cypress-io/github-action@v6
  env:
    # Environment variables here
  with:
    install: false
    working-directory: packages/e2e
    browser: chrome
    record: true
    parallel: true
    tag: "Staging"

Expected behavior

The Cypress tests should be able to resolve and import the workspace package modules.

Actual behavior

Cypress webpack preprocessor fails to resolve the imports, resulting in the above error.

Additional information

  • The issue seems to be related to how the GitHub Action configures Cypress with pnpm workspaces
  • The mock package is built successfully before running the tests
  • The same tests run fine locally with pnpm cy:run

Expected behavior

The Cypress tests should be able to resolve and import the workspace package modules.

Actual behavior

Cypress webpack preprocessor fails to resolve the imports, resulting in the above error.

Possible solutions

  1. Add native support for pnpm workspaces in the github-action
  2. Provide built-in webpack configuration options for monorepos in the action
  3. Document a recommended approach for handling pnpm workspaces with Cypress

Any guidance on how to fix this issue would be greatly appreciated!

mathieubruguier avatar Mar 20 '25 19:03 mathieubruguier

@mathieubruguier

I'm not sure how we can help you here. It sounds very much as if there is a problem with the webpack configuration, however that is not something that this repo deals with at all.

Regarding your suggestions:

Add native support for pnpm workspaces in the github-action

  • There is an open feature request for this https://github.com/cypress-io/github-action/issues/1144. Nobody has come forward so far with an offer to implement this request.

Provide built-in webpack configuration options for monorepos in the action

  • The action has no direct relationship to any webpack configuration setup, so this suggestion doesn't fit into the current architecture.

Document a recommended approach for handling pnpm workspaces with Cypress

  • The README > pnpm workspaces gives some simple guidance on working around this restriction and it looks like you are following this guidance already.

If you don't get any suggestions to resolve your issue here, you can also try the Cypress technical community on Discord Discord chat (click on button)

If you could provide a public repo demonstrating the issue, that would help anybody trying to assist. You might also mention which version of webpack, Node.js and pnpm you are using.

MikeMcC399 avatar Mar 21 '25 06:03 MikeMcC399