reactjsdockerdocker-execreact-testing

Triggering react tests inside docker containers


I have container with React application which is running from docker-compose. When I print logs for this container using

docker-compose logs -f

There is a message:

PASS  src/App.test.js
  ✓ renders learn react link (28 ms)
tests_1  |
Test Suites: 1 passed, 1 total
tests_1  | Tests:       1 passed, 1 total
tests_1  | Snapshots:   0 total
tests_1  | Time:        1.258 s
tests_1  | Ran all test suites related to changed files.
tests_1  |
tests_1  | Watch Usage
tests_1  |  › Press a to run all tests.
tests_1  |  › Press f to run only failed tests.
tests_1  |  › Press q to quit watch mode.
tests_1  |  › Press p to filter by a filename regex pattern.
tests_1  |  › Press t to filter by a test name regex pattern.
tests_1  |  › Press Enter to trigger a test run.

Container works with bind mounts. Changes in my project directory are loaded into container. When I add some new tests, how can I "Press Enter to trigger a test run" inside running container? If I can't press enter in container how can I refresh container to run new tests? I use React 17, WSL, Windows and environment variables like FAST_REFRESH or CHOKIDAR_USEPOLLING don't work.

Best regards


Solution

  • The issue is that the following command shows a stream of logs in the container. But it's not interactive (it's not a shell. It just shows the log).

    docker-compose logs -f
    

    If you really do a change to some of the test files in the host machine(in your machine) and save it, it will rerun the tests.

    If you run the following docker command in the root folder of your react app it will start an interactive (-it) shell for the started container where you can press Enter to rerun tests.

    docker run -it -w /app -v "$(pwd)":/app node:latest sh -c "npm run test"