9

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?

barto90
  • 679
  • 1
  • 9
  • 27
  • Try `ENTRYPOINT ["npm", "run"]` and then a `CMD ["start"]` but override the cmd whenever you start the container with `docker run myimage test -- --coverage` and see if that changes anything. `CMD` specifies the default arguments to pass to `ENTRYPOINT`. I think the entrypoint for your container may be wrong and need to be overridden. – zero298 Apr 30 '19 at 18:43

3 Answers3

22

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.

barto90
  • 679
  • 1
  • 9
  • 27
3

I needed to add ENV CI=true to my Dockerfile:

ENV CI=true
RUN npm run test
RUN npm run build
tjgrist
  • 71
  • 7
0

I am running CI in travis and it is working for me now

sudo: required
services:
    -docker
before_install:
    - docker build -t sambhaba/dockerdevandprod -f Dockerfile.dev .  
script:
    - docker run -e CI=true sambhaba/dockerdevandprod npm run test -- --coverage
leopal
  • 4,711
  • 1
  • 25
  • 35
Sambhaba Pani
  • 77
  • 1
  • 5