I have a very simple config in order to run e2e tests with Cypress using Github Actions in a Nextjs app. When it reaches the npm start command, although it seems to work since it gives the correct output: > Ready on http://localhost:3000, the step stays in pending state without ever advancing to the next step.
Any suggestions on how to fix this?
Following github actions config (.github/workflows/nodejs.yml):
name: Node CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [8.x, 10.x, 12.x]
steps:
- uses: actions/checkout#v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install, build, and test
run: |
npm ci
npm run build --if-present
npm start
npx wait-on http://localhost:3000
env:
CI: true
- name: Run Cypress
run: |
npx cypress run
env:
CI: true
Using the control operator & works for me. Try the following.
- name: npm install, build, and test
run: |
npm ci
npm run build --if-present
npm start & npx wait-on http://localhost:3000
env:
CI: true
From man bash
If a command is terminated by the control operator &, the shell executes the command in
the background in a subshell. The shell does not wait for the command to finish, and the
return status is 0. These are referred to as asynchronous commands. Commands separated
by a ; are executed sequentially; the shell waits for each command to terminate in turn.
The return status is the exit status of the last command executed.
Related
I am using yarn workspaces (yarn version 1.22.19) and I would like to run tests for all workspaces, without stopping even if tests fail for one of the workspaces.
This is so I can collect all failing tests across all workspaces in one run. I'm running the tests on a github action.
I am running the following command:
yarn workspaces run test --passWithNoTests
All workspaces have a test script in the package.json that runs the tests with Jest.
Jest returns an exit code of 1 when tests fail. This causes the yarn workspaces run command to fail and stop. I would like it to continue and fail only after running tests for all workspaces.
How can I make the yarn workspaces run continue even if tests fail for one of the workspaces, yet still have it fail at the end?
Edit:
I am running bash.
Using workarounds like set -e or || true might help swallow the error, but I do want the command to fail ultimately, I just want it to fail after running all tests.
For example:
Say I have 3 workspaces - workspace a, workspace b and workspace c. All of them have the following script in their package.json:
test: "jest"
Say tests pass for workspace a and workspace c, but fail for workspace b. My desired result is that running yarn workspaces run test will run tests for all workspaces (and not stop after tests fail for workspace b) but for it to fail after running all tests.
Here is my github workflow. It just installs dependencies and runs the test script which runs the command yarn workspaces run test --passWithNoTests.
name: Run All Tests
on:
pull_request:
branches: ['develop']
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout#v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Run tests
run: yarn test
For future reference, this is what I ended up doing:
Add a test:ci script to all package.json file with the following defintion:
"test:ci": "jest --ci --reporters=jest-junit --reporters=default --passWithNoTests || true"
This makes it so the command passes even if tests fails.
Use a jest-junit test reporter to output an xml with test results.
In the action, run yarn workspaces run test which runs tests for all workspaces (packages).
Use the dorny/test-reporter#v1 to collect all test result xml files into a nice view.
Set fail-on-error: 'true' for the action dorny/test-reporter#v1 which will make the step fail if any test failed.
Here's the full github workflow:
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
name: Build & Test
on:
pull_request:
branches: ['develop']
# cancel any previous runs that are still in progress if a new commit is pushed
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
steps:
- uses: actions/checkout#v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Run tests
run: yarn test:ci
- name: Unlink all symbolic links # so we don't go over the same file twice
if: success() || failure() # run this step even if previous step failed
run: find node_modules -type l -exec unlink {} \;
- name: Test Report
uses: dorny/test-reporter#v1
if: success() || failure() # run this step even if previous step failed
with:
name: Jest Test Results # Name of the check run which will be created
path: '**/jest-junit.xml'
reporter: jest-junit # Format of test results
list-suites: 'failed'
list-tests: 'failed'
fail-on-error: 'true'
This achieves everything I was looking for - running all tests for all workspaces, even if some fail, while still failing the workflow.
I have set up a cron job for my cypress tests however I want to run it on a specific test alone.
This is my cron job:
name: Cypress Tests
on: [push]
jobs:
cypress-run:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
# Install NPM dependencies, cache them correctly
# and run all Cypress tests
- name: Cypress run
uses: cypress-io/github-action#v4.x.x # use the explicit version number
with:
build: npm run build
start: npm start
I just want to run this one spec file:
Is there a way to run this spec file alone?
Try this out!
Add new command in script section of package.json. Use file or folder here.
"scripts": {
"cypress:spec-run": "cypress run --browser chrome --spec test.spec.js"
},
Run the commond in respective crone job
npm run cypress:spec-run
I would like to use Github Actions for CI and run tests before the branch can be merged.
I have a single repository that has both my server and frontend within it (Nest & Angular). I am using Cypress/Jest for my tests.
I need my backend server running for my frontend cypress tests to pass.
Currently GH Actions doesn't move onto the next step because the backend process is running - but that's what I need to happen...
How should I set this up so that I can use GH Actions for CI?
name: test
on: [push]
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OTHER_SECRETS: ${{ secrets.otherSecrets }}
jobs:
cypress-run:
runs-on: macos-11
steps:
# start cypress w/github action: https://github.com/cypress-io/github-action
- name: Setup Node.js environment
uses: actions/setup-node#v2.5.0
with:
node-version: '16.13.0'
- name: Checkout
uses: 'actions/checkout#v2'
- name: "Start Backend"
run: |
cd server &&
npm install &&
npm run build &&
npm run start:prod
- name: "Start Frontend"
run: |
npm install &&
npm run build &&
npm run start
- name: Cypress run
uses: cypress-io/github-action#v2
with:
record: true
browser: chrome
- name: "Run Jest Tests"
run: |
cd server &&
npm run test
#note: I have tried appending the "&& sleep 10 && curl http://localhost:port -i" option to the npm commands - and it hasn't worked for me.
#note2: It's my first time w/ GH Actions, so maybe I'm missing something obvious!!
#note: I have tried appending the "&& sleep 10 && curl http://localhost:port -i" option to the npm commands - and it hasn't worked for me.
There is a slight error here, && will wait for the previous command to complete and only run the next one if it is successful & would run the previous command in the background and then will move on to running the next one. Therefore as nothing stops your server, && won't work.
I am not sure it is the cleanest way but the following should work, I have used an equivalent to run a UI in one of my projects.
- name: "Start Backend"
run: |
cd server &&
npm install &&
npm run build &&
npm run start:prod &
sleep 5 &&
curl http://localhost:port -I
- name: "Start Frontend"
run: |
npm install &&
npm run build &&
npm run start &
sleep 5 &&
curl http://localhost:port -I
I had the same problem, server running but never moving to the next step of running the Cypress tests.
Thanks didwefixit, using just one & worked to start the server and then run Cypress test script worked:
jobs:
build:
env:
CI: true
strategy:
matrix:
node-version: [14.x, 16.x]
runs-on: [ ubuntu-latest ]
steps:
- uses: actions/checkout#v2
- name: Use Node.js version ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install --prefix client
- run: npm install --prefix server
- run: npm install
- run: npm run build --prefix client
- run: npm run start --prefix server & npm run test
script in client package.json:
"build": "BUILD_PATH=../server/public react-scripts build"
script in server package.json:
"start": "node src/server.js"
script in root package.json:
"test": "npx cypress run"
This is my actions
# This is a basic workflow to help you get started with Actions
name: CI/CD Akper Bina Insan - Live
on:
push:
branches: [ master ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
- name: Install Yarn
run: npm install -g yarn
- name: Build UI - Akper Bina Insan
working-directory: ./ui
run: yarn && yarn build && yarn start
- name: Build Backend Service - Akper Bina Insan
working-directory: ./be
run: yarn && yarn build && yarn prod
After it finished with the first run, it don't go continue with the second run. Even though the server is ready. I waited for 5 minutes then I stopped since I don't want to waste time.
How can I make it run for the second one?
GitHub actions are a tool for CI/CD and not for hosting (running) your application.
In the given workflows, you build an then run your UI application. The run command is a blocking process - e.g. your workflow will remain blocked because you have started your UI application. You should not do that in workflows.
Use GitHub Action for build and test, but not for hosting.
Adding an ending & to the node command solved this for my use-case
run: yarn && yarn build && yarn start &
or
run: |
yarn
yarn build
yarn start &
Using & in the end of a command, the shell executes the command in the background in a subshell, therefore running the first yarn start won't be blocking your next step in the workflow.
I have made simple hello world Desktop Application using electron framework.
and it's working locally fine but it's failing GitHub Action CI.
link to action
link to yaml
name: Node.js CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
- name: getting node js
run: sudo npm install npm#latest -g
- name: setting electron globally
run: npm i electron -g
- name: running application.
run: electron .
- name: all done just checks passed.
run: echo ci done successfully.
I'm getting following error in GitHub action console help me out which command is missing or anything that fixies this issue. I have tried npm init also.
run electron .
The futex facility returned an unexpected error code./opt/hostedtoolcache/node/10.23.0/x64/lib/node_modules/electron/dist/electron exited with signal SIGABRT
Error: Process completed with exit code 1.
please avoid spelling mistakes.
thanks in advance.