Skip to content

Commit

Permalink
test: add pnpm workspaces example (#1140)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMcC399 authored Mar 7, 2024
1 parent 3864088 commit 4090c65
Show file tree
Hide file tree
Showing 14 changed files with 1,808 additions and 0 deletions.
127 changes: 127 additions & 0 deletions .github/workflows/example-start-and-pnpm-workspaces.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
name: example-start-and-pnpm-workspaces
on:
push:
branches:
- 'master'
pull_request:
workflow_dispatch:

jobs:
# The example has pnpm workspaces in its "root" folder
# examples/start-and-pnpm-workspaces

single-ws:
# This job installs pnpm,
# installs all dependencies,
# caches the pnpm store,
# caches the Cypress binary cache,
# then runs Cypress tests in the single workspace
# of the subfolder "packages/workspace-1".
runs-on: ubuntu-22.04
name: Single workspace
steps:
- name: Checkout repository
uses: actions/checkout@v4

# pnpm is not installed by default on GitHub runners
- name: Install pnpm
run: npm install -g pnpm@8

# locate the pnpm store directory where pnpm dependencies are installed
- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
# Cypress github-action does not cache pnpm dependencies by default.
# Cache the dependencies.
- name: Setup pnpm cache
uses: actions/cache@v4
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('examples/basic-pnpm/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
# with Cypress GitHub Action.
# Calling the Cypress GitHub Action causes all dependencies from
# the root of the pnpm workspace examples/start-and-pnpm-workspaces
# to be installed
# AND it automatically caches the Cypress binary.
#
# If you copy this workflow to another repository replace the following with
# uses: cypress-io/github-action@v6
# The notation ./ is a special usage only used here.
# It causes the version of the action code from whatever branch it is launched in to be used.
# Do not try to use the ./ notation this outside of this repository!
uses: ./ # approximately equivalent to using cypress-io/github-action@v6
with:
working-directory: examples/start-and-pnpm-workspaces
runTests: false

- name: Cypress test Single
# Run Cypress in examples/start-and-pnpm-workspaces/packages/workspace-1 only
uses: ./ # equivalent to using cypress-io/github-action@v6
with:
# Do not attempt to install dependencies in the workspace using the action.
# There is no pnpm-lock.yaml file in a workspace for
# Cypress GitHub Action to use.
# We already installed dependencies when we called the action previously.
install: false
working-directory: examples/start-and-pnpm-workspaces/packages/workspace-1
build: pnpm run build
start: pnpm start
wait-on: 'http://localhost:5000'

multiple-ws:
# This job installs pnpm,
# installs all dependencies,
# caches the pnpm store,
# caches the Cypress binary cache,
# then runs Cypress tests in each of the workspaces.
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
cypress:
- working_directory: examples/start-and-pnpm-workspaces/packages/workspace-1
- working_directory: examples/start-and-pnpm-workspaces/packages/workspace-2
name: Multiple workspaces
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install pnpm
run: npm install -g pnpm@8

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: Setup pnpm cache
uses: actions/cache@v4
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('examples/basic-pnpm/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
uses: ./ # approximately equivalent to using cypress-io/github-action@v6
with:
working-directory: examples/start-and-pnpm-workspaces
runTests: false

- name: Cypress test Multiple
# Run Cypress in
# examples/start-and-pnpm-workspaces/packages/workspace-1 and
# examples/start-and-pnpm-workspaces/packages/workspace-2
uses: ./ # equivalent to using cypress-io/github-action@v6
with:
install: false
working-directory: ${{ matrix.cypress.working_directory }}
build: pnpm run build
start: pnpm start
wait-on: 'http://localhost:5000'
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- Use different [working-directory](#working-directory)
- Use [subfolders](#subfolders)
- Use [pnpm](#pnpm)
- Use [pnpm workspaces](#pnpm-workspaces)
- Use [Yarn Classic](#yarn-classic)
- Use [Yarn Modern](#yarn-modern)
- Use [Yarn Plug'n'Play](#yarn-plugnplay)
Expand Down Expand Up @@ -1136,6 +1137,30 @@ jobs:

[![pnpm example](https://github.com/cypress-io/github-action/workflows/example-basic-pnpm/badge.svg?branch=master)](.github/workflows/example-basic-pnpm.yml)

### pnpm workspaces

If you are using [pnpm workspaces](https://pnpm.io/workspaces) you need to install dependencies and run Cypress tests in a workspace in separate steps. The snippet below shows this principle.

```yml
...
- name: Install dependencies
uses: cypress-io/github-action@v6
with:
working-directory: examples/start-and-pnpm-workspaces
runTests: false
- name: Cypress test
uses: cypress-io/github-action@v6
with:
install: false
working-directory: examples/start-and-pnpm-workspaces/packages/workspace-1
...
```

[![pnpm workspaces example](https://github.com/cypress-io/github-action/workflows/example-start-and-pnpm-workspaces/badge.svg?branch=master)](.github/workflows/example-start-and-pnpm-workspaces.yml)

See the example project [start-and-pnpm-workspaces](examples/start-and-pnpm-workspaces/) and the [example-start-and-pnpm-workspaces.yml](.github/workflows/example-start-and-pnpm-workspaces.yml) workflow for a full working example including pnpm caching.

### Yarn Classic

If a `yarn.lock` file is found, the action uses the [Yarn 1 (Classic)](https://classic.yarnpkg.com/) command `yarn --frozen-lockfile` by default to install dependencies.
Expand Down
6 changes: 6 additions & 0 deletions examples/start-and-pnpm-workspaces/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "start-and-pnpm-workspaces",
"version": "1.0.0",
"description": "example using pnpm with workspaces",
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { defineConfig } = require('cypress')

module.exports = defineConfig({
fixturesFolder: false,
e2e: {
setupNodeEvents(on, config) {},
supportFile: false,
baseUrl: 'http://localhost:5000',
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
it('loads the page', () => {
cy.visit('/')
cy.contains('This is a page, from workspace-1').should('be.visible')
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "workspace-1",
"version": "1.0.0",
"description": "e2e tests for a server with build and start steps",
"main": "index.js",
"scripts": {
"test": "cypress run",
"build": "echo building ... server ... done!",
"start": "serve -p 5000 public"
},
"private": true,
"devDependencies": {
"cypress": "13.6.6",
"serve": "14.2.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<body>
This is a page, from workspace-1
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { defineConfig } = require('cypress')

module.exports = defineConfig({
fixturesFolder: false,
e2e: {
setupNodeEvents(on, config) {},
supportFile: false,
baseUrl: 'http://localhost:5000',
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
it('loads the page', () => {
cy.visit('/')
cy.contains('This is a page, from workspace-2').should('be.visible')
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "workspace-2",
"version": "1.0.0",
"description": "e2e tests for a server with build and start steps",
"main": "index.js",
"scripts": {
"test": "cypress run",
"build": "echo building ... server ... done!",
"start": "serve -p 5000 public"
},
"private": true,
"devDependencies": {
"cypress": "13.6.6",
"serve": "14.2.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<body>
This is a page, from workspace-2
</body>
Loading

0 comments on commit 4090c65

Please sign in to comment.