NPM test suite exit?

Hello All,

I am using react-app to test and chalk CI/CD pipeline using Travis CI and docker. I am not a developerā€¦so I am not doing any funky code editing, but some small edits. So test suite gets run locally>>>commit GIT and push code to repo>>>Kick in Travis run for initial testingā€¦the usual CI thing.

The issue I am facing here is, how do I exit test ran by NPM and come back to CLI? I tried this ā€“ --coverage with no luck -

vagrant@docubu:/vagrant/lab/react101$ docker run react-dev npm test -- --coverage

> react101@0.1.0 test /app
> react-scripts test "--coverage"

PASS src/App.test.js
  āœ“ renders without crashing (30ms)

------------------|----------|----------|----------|----------|-------------------|
File              |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
------------------|----------|----------|----------|----------|-------------------|
All files         |     2.44 |        0 |     6.25 |     2.44 |                   |
 App.js           |      100 |      100 |      100 |      100 |                   |
 index.js         |        0 |      100 |      100 |        0 |              7,12 |
 serviceWorker.js |        0 |        0 |        0 |        0 |... 23,130,131,132 |
------------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.284s
Ran all test suites.

For some reason, this does not fall back to a command line after tests get executed successfully. Tried running it int local directory on localhost without involving docker. Same. I have to press CTRL+Z to get back to a shell.

Unless it test exits, my CI tool will be stuck for 1st integration or test run.

Is there any other way of existing test suit on its own. I reckon ā€“ -- coverage just gives the summary of tests done and exit

Node details -

vagrant@docubu:/vagrant/lab/react101$ node -v
v12.2.0
vagrant@docubu:/vagrant/lab/react101$ npm -v
6.9.0

npm test is just running the ā€œtestā€ script (command) from the package.json. You can try running that command directly to check that the failure to exit is not related to using npm. From your output that might be:

react-scripts test "--coverage"

Tried running the command you told in local directory (without docker), where react app is created. The Command does not exist.

vagrant@docubu:/vagrant/lab/react101$ react-scripts test "--coverage"
-bash: react-scripts: command not found
vagrant@docubu:/vagrant/lab/react101$ sudo react-scripts test "--coverage"
sudo: react-scripts: command not found
vagrant@docubu:/vagrant/lab/react101$ react-scripts test
-bash: react-scripts: command not found

Here is how react101 FS looks. Also Package.json and test script.

vagrant@docubu:/vagrant/lab/react101$ ls -l
total 568
drwxrwxr-x 1 vagrant vagrant   4096 Jun  7 14:14 build
drwxrwxr-x 1 vagrant vagrant   4096 Jun 13 04:30 coverage
-rw-rw-r-- 1 vagrant vagrant    310 Jun 11 07:49 docker-compose.yml
-rw-rw-r-- 1 vagrant vagrant    204 Jun 10 03:42 Dockerfile
-rw-rw-r-- 1 vagrant vagrant    210 Jun 10 03:42 Dockerfile.dev
drwxrwxr-x 1 vagrant vagrant  36864 Jun  7 14:12 node_modules
-rw-r--r-- 1 vagrant vagrant    614 Jun  7 13:52 package.json
-rw-r--r-- 1 vagrant vagrant 505290 Jun  7 13:52 package-lock.json
drwxrwxr-x 1 vagrant vagrant   4096 Jun  8 10:30 public
-rw-r--r-- 1 vagrant vagrant   2881 Jun  7 13:52 README.md
drwxrwxr-x 1 vagrant vagrant   4096 Jun  8 11:01 src
vagrant@docubu:/vagrant/lab/react101$ cd src/
vagrant@docubu:/vagrant/lab/react101/src$ ls
App.css  App.js  App.test.js  index.css  index.js  logo.svg  serviceWorker.js
vagrant@docubu:/vagrant/lab/react101/src$ cat App.test.js 
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);
  ReactDOM.unmountComponentAtNode(div);
});
vagrant@docubu:/vagrant/lab/react101/src$ cat ../package.json 
{
  "name": "react101",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-scripts": "3.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
vagrant@docubu:/vagrant/lab/react101/src$

Eidt 1
Here are few react scripts -

vagrant@docubu:/vagrant/lab/react101$ find . -iname *react-scripts*
./node_modules/.bin/react-scripts
./node_modules/react-scripts
./node_modules/react-scripts/bin/react-scripts.js
1 Like

Ah, the PATH gets modified for running npm scripts to include commands from node_modules/.bin, so to call directly:

./node_modules/.bin/react-scripts test --coverage

These links are about create-react-app but may also apply to react-scripts test.

According to this page: https://facebook.github.io/create-react-app/docs/running-tests

By default npm test runs the watcher with interactive CLI. However, you can force it to run tests once and finish the process by setting an environment variable called CI .

And there are two approaches to run the tests a single time, by defining CI=true or by passing --watchAll=false:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.