How to add build script and tests in NodeJs project

Viewed 11751

I have created a basic todo app which has package.json file as:

{
  "name": "to-do-app",
  "version": "1.0.0",
  "description": "A basic to-do app created using JavaScript.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Sahil Silare",
  "license": "MIT",
  "dependencies": {
    "body-parser": "^1.19.0",
    "build": "^0.1.4",
    "ejs": "^2.7.1",
    "express": "^4.17.1",
    "npm-build": "0.0.1"
  },
  "devDependencies": {},
  "repository": {
    "type": "git",
    "url": "git+https://github.com/sahil9001/to-do-app.git"
  },
  "keywords": [
    "todo",
    "app"
  ],
  "bugs": {
    "url": "https://github.com/sahil9001/to-do-app/issues"
  },
  "homepage": "https://github.com/sahil9001/to-do-app#readme"
}

Whenever I run npm test it fails by saying no tests specified, how can I solve this issue? also whenever I try to use TRAVIS CI it fails to detect build script how can I create one?

2 Answers

Specify all required script in scripts property in package.json as below

{
      "name": "to-do-app",
      "version": "1.0.0",
      "description": "A basic to-do app created using JavaScript.",
      "main": "index.js",
      "scripts": {
        "test": "put test command here",  // example "test": "mocha test.js"
         "build" : "put build command here"
      },
      "author": "Sahil Silare",
      "license": "MIT",
      "dependencies": {
        "body-parser": "^1.19.0",
        "build": "^0.1.4",
        "ejs": "^2.7.1",
        "express": "^4.17.1",
        "npm-build": "0.0.1"
      },
      "devDependencies": {},
      "repository": {
        "type": "git",
        "url": "git+https://github.com/sahil9001/to-do-app.git"
      },
      "keywords": [
        "todo",
        "app"
      ],
      "bugs": {
        "url": "https://github.com/sahil9001/to-do-app/issues"
      },
      "homepage": "https://github.com/sahil9001/to-do-app#readme"
    }

You don't have any scripts command listed in scripts property and have only default. You have to create and place it as per the need. Refer below links for more details

https://www.freecodecamp.org/news/introduction-to-npm-scripts-1dbb2ae01633/

https://flaviocopes.com/package-json/

If you look under "scripts" you'll see your npm scripts such as the script you call when you run "npm test"

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },

The default value when you run NPM init is this simple echo saying no tests, you have to write your own test command(s) here.

Travis is failing to detect a build script, because you don't have a value for "build" in your scripts section, add something like:

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "my_script_to_build"
  },
Related