Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running tests in a separate Docker container #1008

Open
Lehks opened this issue Feb 10, 2023 · 7 comments
Open

Running tests in a separate Docker container #1008

Lehks opened this issue Feb 10, 2023 · 7 comments

Comments

@Lehks
Copy link

Lehks commented Feb 10, 2023

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/[email protected] /app
    +-- @ek/[email protected]
    | `-- [email protected] deduped
    +-- [email protected]
    | `-- [email protected] deduped
    `-- [email protected]
  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

  • are you able to run jest test from the command line? yes
  • how do you run your tests from the command line? (for example: npm run test or node_modules/.bin/jest) docker exec -t nodejs-generator-test-unittest_app_1 npx jest --coverage --runInBand (the command that is also passed to vscode-jest)

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
Copy link
Collaborator

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
Copy link
Author

Lehks commented Feb 11, 2023

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
Copy link
Collaborator

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
Copy link
Author

Lehks commented Feb 12, 2023

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
Copy link

snowstorm0182 commented Feb 17, 2023

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
Copy link

calebpitan commented Feb 19, 2023

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
Copy link

ntindle commented Jun 7, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants