GitHub Actions only /tmp/unit-tests-2f311f40.sh: 1: jest: not found

Viewed 28

I have a pipeline in GitHub Actions and it is suggesting that it cannot find Jest despite it working locally.

If I run npm run unit-tests locally it works fine, but in the CI I get the following error:

> jest --group=unit --coverage --verbose

/tmp/unit-tests-2f311f40.sh: 1: jest: not found
name: staging-pipeline

on:
  push:
    tags:
    - 'v*-staging'

env:
  CI: true
  NODE_ENV: production

jobs:
  unit-test:
    name: unit-test
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@master
    - name: Unit Tests 
      run: |
        npm install
        npm run unit-tests
{
  "name": "app",
  "version": "1.0.0",
  "description": "app",
  "main": "",
  "scripts": {
    "ts-node": "ts-node",
    "test": "jest",
    "unit-tests": "jest --group=unit --coverage --verbose",
    "prisma-generate": "npx prisma generate && npm install @prisma/client"
  },
  "dependencies": {
    "@prisma/client": "^4.3.1",
    "axios": "^0.27.2",
    "axios-retry": "^3.3.1",
    "dotenv": "^16.0.2",
    "mongodb": "^4.9.1",
    "qs": "^6.11.0",
    "winston": "^3.8.1",
    "zod": "^3.18.0"
  },
  "devDependencies": {
    "@types/aws-lambda": "^8.10.102",
    "@types/jest": "^29.0.0",
    "@types/node": "^18.7.15",
    "@types/qs": "^6.9.7",
    "@typescript-eslint/eslint-plugin": "^5.36.1",
    "@typescript-eslint/parser": "^5.36.1",
    "esbuild": "^0.15.7",
    "eslint": "^8.23.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-prettier": "^4.2.1",
    "jest": "^28.1.3",
    "jest-mock-extended": "^2.0.7",
    "jest-runner-groups": "^2.2.0",
    "json-schema-to-ts": "^2.5.5",
    "prettier": "2.7.1",
    "prisma": "^4.3.1",
    "ts-jest": "^28.0.8",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.1.0",
    "typescript": "^4.8.2"
  },
  "author": "",
  "license": "MIT"
}
2 Answers

The reason is you installed jest globally on your local machine so Jest command is available in your local machine but not the case in Github action environment(container), so you have to way to fix it :

1.install jest globally in in Github action environment.

  jobs:
  unit-test:
    name: unit-test
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@master
    - name: Unit Tests 
      run: |
        npm install
        npm install -g jest
        npm run unit-tests

2.or in your package.json file change the npm run unit-test to ./node_modules/.bin/jest --group=unit --coverage --verbos to read I recommend the second way.

You can see in my CI i have the following variable

  NODE_ENV: production

This was causing npm to only install the dependencies and not the devDependencies. equivalant of doing npm install --prod once i removed this env variable, it was able to install jest and use it as normal.

Related