LinuxCommandLibrary

npm-test

Run automated tests for a Node.js project

TLDR

View documentation for the original command

$ tldr npm run
copy

SYNOPSIS

npm test [-- ]

DESCRIPTION

The `npm test` command executes the test script defined in your project's `package.json` file. This script typically runs a testing framework (like Jest, Mocha, or Tape) to verify the correctness of your project's code. The specific commands executed depend on the value of the `test` field in `package.json`. If no test script is specified, `npm test` will exit with code 0, meaning successful. It reads from the root directory of the npm package, where `package.json` is located. The output will give you insight into whether your latest changes broke some functionality, or whether they behave as desired. This helps ensure code quality and maintainability. The behavior of `npm test` is heavily reliant on the specific testing framework and configuration used in your project.

The command supports configuration through environment variables and npm configuration settings.

CAVEATS

The meaning of "success" depends on the configured testing framework; `npm test` only checks for a non-zero exit code. If the `test` script isn't properly configured or the testing framework isn't correctly handling errors, the test suite may fail without `npm test` reporting an error.

CONFIGURATION

The `test` script is defined in the `package.json` file under the `scripts` section. For example:
package.json:
{ "scripts": { "test": "jest" } }
This would run the Jest testing framework when you execute `npm test`.

PASSING ARGUMENTS TO THE TEST SCRIPT

You can pass arguments to the test script using the `--` separator. For example:
npm test -- --watch
This would pass the `--watch` argument to the test script defined in your `package.json`. The interpretation of these arguments depends entirely on the specific test script and testing framework used.

EXIT CODES

A non-zero exit code from the test script indicates a failure. npm treats this as an indication of test failure.

HISTORY

The `npm test` command has been a core part of npm since its early days. It provides a standard way for developers to execute tests associated with their projects. Its existence promotes consistency and automation within the JavaScript ecosystem, making testing more accessible and discoverable.

SEE ALSO

npm run(1), npm install(1), npm package.json(5)

Copied to clipboard