Sh: 1: jest: not found

Hi

I’m using NestJS and would like to run the tests in my Gitlab pipeline, using Jest.

Problem: I get the error sh: 1: jest: not found when I use npm run test

My gitlab-ci.yml


    image: node
    
    stages:
      - nest_build
      - nest_test
      - ... my other stages
    
    cache:
      paths:
        - node_modules/
    
    default:
      tags:
        - deploy
    
    nest_build:
      stage: nest_build
      script: 
        - echo "Start building NestJS"
        - npm ci
        - npm run build
        - echo "Build successfully!"
      
    nest_test:
      stage: nest_test
      script:
        - echo "Testing NestJS"
        - npm run test
        - echo "Test successfully!"

Pipeline:

$ echo "Testing NestJS"
    Testing NestJS
    $ npm run test
    > skezi-api@0.0.1 test
    > jest
    sh: 1: jest: not found

My script test in package.json :

"test": "jest",

I tried running npm run test in nest_build and it works fine.

I have a feeling that the node_modules cache is not working?

Since npm run test works in the nest_build job too, this seems to be a caching issue. I’m not deeply familiar with how npm/yarn caches work - the docs mention to specify a target directory for npm ci to store the modules in a controlled directory outside of the home directory.

Another way to verifying this without changing the npm ci command - add a ls -la node_modules/* command after the echo "Build successfully!" script step. If it is empty, the path to node modules is wrong and needs to be corrected.