Github Actions with Jest Suddenly Stopped - jestjs

I registered test-coverage ci in github actions
name: TEST-COVERAGE
on:
push:
branches:
- main
- dev
pull_request:
branches:
- main
- dev
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
steps:
- name: Checkout source code
uses: actions/checkout#v3
- name: Set up Nest.js
uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
- name: Set up Node_modules
run: |
cd ./client
rm -rf node_modules
yarn install --immutable --immutable-cache --check-cache
cd ..
cd ./server/gateway
rm -rf node_modules
npm ci
cd ../services/core
rm -rf node_modules
npm ci
cd ../auth
rm -rf node_modules
npm ci
cd ../ticket
rm -rf node_modules
npm ci
- name: Run Test Coverage
run: |
rm -rf node_modules
npm ci
npm run coveralls
- name: Coveralls
uses: coverallsapp/github-action#master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
I passed all the test cases with jest, and while executing it, a situation occurred where the Github action suddenly stopped. Even locally, the test code passes and does not emit an error message, but it stops without any output, so it cannot be solved.
Error: Process completed with exit code 1.
Below is my Current Directory
client
server
gateway
auth
core
ticket
At first, it seemed to be a Github action memory leak, so when I changed the npm install command to npm ci, it ran fine. However, as we added and expanded new features, npm ci encountered the same problem.
https://github.com/boostcampwm-2022/web03-FanUP/actions/runs/3521371482/jobs/5903147717
This is github actions result
"scripts": {
"test": "jest --detectOpenHandles --forceExit",
"coverage": "jest --detectOpenHandles --coverage --forceExit",
"coveralls": "jest --coverage --detectOpenHandles --forceExit && coveralls < ./coverage/lcov.info"
},

Related

How can I use yarn workspaces to run tests for all workspaces, even if one fails?

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.

How to restore dist and node_modules if the build fails because actions/checkout#v3 deletes everything from the directory? (Github Actions selfhosted)

I wonder if there is a way for actions/checkout#v3 to skip specific folders for build restoration.
For example, if my build fails to run, actions/checkout#v3 deletes dist and node_modules, consequently crashes the whole application, thus causing a bigger problem than just the npm run build failure.
Is there any method or strategy to deal with this? a cache? restore the files? how can I deal with this?
Translated with www.DeepL.com/Translator (free version)
my node.js.yml
name: Node.js CI
on:
push:
branches: [ 'main' ]
pull_request:
branches: [ 'main' ]
jobs:
build:
runs-on: self-hosted
strategy:
matrix:
node-version: [16.x]
steps:
- name: Move a last build (cached version)
run: |
mkdir ../build_cache
mv build ../build_cache
- uses: actions/checkout#v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v3
with:
clean: false
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci -f
- run: npm install -f
- run: npm run build
- name: Release nome build of new version build
run: mv dist build
- run: pm2 stop fastify-backend
- run: pm2 delete fastify-backend
- run: pm2 start ./build/main/bundle.js --name fastify-backend"
- if: failure()
run: |
mv ./build_cache/build ./
- run: pm2 restart fastify-backend
I tried a copy and paste file strategy, but copying and pasting node_modules would take too much time, I would like a simpler solution to deal with this...

GitHub Actions to build artifact and deploy

I am using a github action pipeline and am trying to run an "npm run build" to generate an artifact that will then be used for deployment and production. Right now, my application is loaded from the local /build folder containing the built production application. This folder is populated after i make my changes and manually run "npm run build." I would instead like my pipeline to build the artifact and then deploy the application from artifact in Heroku. Here is my pipeline code:
# 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://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Node.js CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
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'
# - run: npm ci
# - run: npm run build --if-present
- run: cd react-frontend && npm install
- run: cd react-frontend && npm run build
# - run: npm test --if-present ```
No artifact is being built at present

Github Actions - Run server & frontend, then execute tests

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"

Github actions not working after npm start

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.

Resources