jest-community / vscode-jest

The optimal flow for Jest based testing in VS Code
MIT License
2.85k stars 295 forks source link

Running tests in a separate Docker container #1008

Open Lehks opened 1 year ago

Lehks commented 1 year ago

Environment

  1. vscode-jest version: v5.2.3
  2. node -v: 19.0.0
  3. npm -v or yarn --version: 8.19.2
  4. npm ls jest or npm ls react-scripts (if you haven’t ejected): @ek/nodejs-generator-test@0.0.0 /app +-- @ek/jest-matchers@1.0.1 | `-- jest@29.4.2 deduped +-- jest-extended@3.2.3 | `-- jest@29.4.2 deduped `-- jest@29.4.2
  5. your vscode-jest settings if customized:
    • jest.jestCommandLine? docker exec -t nodejs-generator-test-unittest_app_1 npx jest --coverage --runInBand
    • jest.autoRun? {}
    • anything else that you think might be relevant? no
  6. Operating system: Docker for Windows in WSL 2, using the image node:current-alpine

Prerequisite

Steps to Reproduce

I do not belive this the be necessary, see error description below.

Relevant Debug Info

None

Expected Behavior

The tests should run.

Actual Behavior

I get the error

Error: Could not resolve a module for a custom reporter.
  Module name: /home/my-home-dir/.vscode-server/extensions/orta.vscode-jest-5.2.3/out/reporter.js

Als mentioned in the title, all of my unittests are run inside of a separate container. This makes the error message quite clear: The extension is looking for a file inside of the container that is actually on the host. My question is wether there is a good possibility to provide this required file in the container. My preferred way would be by installing an addional NPM package, ore some other command that installs the required files inside of the container and can be executed in the Dockerfile. I have full access to the Dockerfile, so it can be changed in any way I like.

I could also mount the file into the container using Docker but this is impractical to the point that I would abstrain from using the extension, since mounting the file directly would depend on where VS Code (or VS Code Server) is installed on the host. That makes this solution rather suboptimal considering that a bunch of people at my company are supposed to use the setup, so VS Code installations vary quite a bit.


The fastest (and the most fun) way to resolve the issue is to submit a pull-request yourself. If you are interested, feel free to check out the contribution guide, we look forward to seeing your PR...

connectdotz commented 1 year ago

Interesting use case... we could consider the customization for the dependency, but we also need to read output from the test run (we instruct jest to dump output to the tmp dir), how will that work with Docker?

Lehks commented 1 year ago

Reading the output from stdout would probably be the easiest thing in this case, at least from the Docker perspective. But that would probably require too many changes on your side. But since the /tmp is always the same, i.e. independent from a user's installation, I could probably mount the tmp dir.

connectdotz commented 1 year ago

I should clarify that we use nodejs os.tmpdir(), not the hardcoded /tmp. It's easy to mount whatever that directory is for a specific env. It might be more complicated if your project is used across platforms or developers...

would it be easier if you mount the package folder directory to your developer host instead? Or run tests within the docker env instead of the development env?

Lehks commented 1 year ago

would it be easier if you mount the package folder directory to your developer host instead? Or run tests within the docker env instead of the development env?

I am not quite sure what you mean by that. What is the package folder, are you talking about /home/my-home-dir/.vscode-server/extensions/orta.vscode-jest-5.2.3? Also, what do you mean by the differentiation between the "Docker env" and the "Development env"? I am running the tests in a Docker environment.

snowstorm0182 commented 1 year ago

I've used this script as jest.jestCommandLine to run tests and transfer results For some reason I have both local and container paths in Test Explorer (local ones getting marked green/red).

#!/bin/bash
CURRENT_DIR=$(pwd)
APP_DIR="/opt/brand/project"
docker exec project_container_1 sh -c "mkdir -p /tmp/jest_tests; rm /tmp/jest_tests/*"
docker cp ~/.vscode/extensions/orta.vscode-jest-5.2.3/out/reporter.js project_container_1:/tmp
SUFFIX=$(echo "$@" | sed "s|$HOME/.vscode/extensions/orta.vscode-jest-5.2.3/out/|/tmp/|")
SUFFIX=$(echo $SUFFIX | sed "s|$CURRENT_DIR|$APP_DIR/frontend|g")
docker exec -w $APP_DIR/frontend project_container_1 sh -c "NODE_ENV=test node_modules/.bin/jest --no-cache $SUFFIX"
docker exec project_container_1 sh -c "mkdir -p /tmp/jest_tests; cp /tmp/jest_runner_projects_* /tmp/jest_tests/"
docker exec project_container_1 sh -c "sed -i 's|$APP_DIR/frontend|$CURRENT_DIR|g' /tmp/jest_tests/*.json"
docker cp project_container_1:/tmp/jest_tests/. /tmp/
echo "Finished docker wrapper"
calebpitan commented 1 year ago

I think it will be easier to mount this path from the host onto the container using volumes and allowing user's to pass a custom path to the reporter, which in this case will be the mount path on the container.

services:
  web-test:
    volumes:
      - '~/.vscode/extensions/orta.vscode-jest-5.2.3/out/reporter.js:/root/.vscode/extensions/orta.vscode-jest-5.2.3/out/reporter.js'
ntindle commented 1 year ago

That didn't work for me due to the full path being passed on mac which contains /Users