Home > Net >  npm ERR! Missing script: "test" when using github actions
npm ERR! Missing script: "test" when using github actions

Time:07-26

I wrote a test for my javascript application from the Actions part of git. However, it does not work as expected. Here it is:

# This is a basic workflow to help you get started with Actions
name: Node.js CI Testing
 
# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
 
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
 
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
 
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js
      uses: actions/setup-node@v2
      with:
        node-version: "16.x"
 
    - name: Install dependencies
      run: npm install --legacy-peer-deps
 
    - name: Run test
     
      run: npm test

However, when I run it, I get the following errors:

Run npm test
npm ERR! Missing script: "test"
npm ERR! 
npm ERR! To see a list of scripts, run:
npm ERR!   npm run

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2022-07-25T23_14_24_295Z-debug-0.log
Error: Process completed with exit code 1.

I searched the internet for how to fix the problem and added the following line to my test file:

 - name: Run test
          working-directory: ./src
          run: npm test

. but without success. Here is my package.json file:

{
  "name": "hydrogen",
  "version": "1.0",
  "scripts": {
    "start": "vite",
    "dev": "vite",
    "build": "vite build",
    "serve": "vite preview"
  },
  "devDependencies": {
    "autoprefixer": "^10.4.2",
    "postcss": "^8.4.5",
    "tailwindcss": "^3.0.18",
    "vite": "^2.7.13",
    "vite-plugin-solid": "^2.2.5"
  },
  "dependencies": {
    "@tailwindcss/forms": "^0.4.0",
       ...
    "solid-js": "^1.3.3"
  }
}

CodePudding user response:

That is happening because when Github Actions is trying to run this part of the worflow:

  • name: Run test

    run: npm test

npm does not find a "test" script in your package.json:

"scripts": {
    "start": "vite",
    "dev": "vite",
    "build": "vite build",
    "serve": "vite preview"
  },

So if you don't have tests in your project you can get rid of "run: npm test" in your workflow or add a "test" script in your package.json:

 "scripts": {
        "start": "vite",
        "dev": "vite",
        "build": "vite build",
        "serve": "vite preview",
        "test": "echo \"Error: no test specified\" && exit 1"
      },
  • Related