Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node in Docker: npm test and exit

I want to test my node docker image with "npm run test" as a command overwrite when running my container.

My Dockerfile is:

FROM node:alpine
WORKDIR /app
COPY ./package.json ./
RUN npm install
COPY ./ ./
CMD ["npm", "run", "start"]

The "npm run test" command should be run in my container and exit to the terminal (locally and Travis CI) but the test run is stuck at "Ran all test suites." waiting for input.

My docker run command is:

docker run myimage npm run test -- --coverage

I also tried with:

docker run myimage npm run test -- --forceExit

But none of them exits when the test have run (neither locally or in Travis CI).

My App.test.js file is the standard test:

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);
});

What should I do to automatically exit the test when it is finished?

like image 592
barto90 Avatar asked Apr 30 '19 18:04

barto90


People also ask

How do you stop and exit docker?

If you want to stop and exit the container, and are in an interactive, responsive shell - press ctrl+d to exit the session. You could as well type the exit command. TL;DR: press ctrl+c then ctrl+d - that means, keep the ctrl key pressed, type a c, and let go of ctrl. Then the same with ctrl and d.


1 Answers

Found that the docker run command should contain -e CI=true to exit immediately:

docker run -e CI=true myimage npm run test

From React documentation about "CI=true":

The test command will force Test to run in CI-mode, and tests will only run once instead of launching the watcher.

like image 86
barto90 Avatar answered Oct 12 '22 12:10

barto90