Can I use WORKDIR in my Dockerfile with Github Actions? (Also, resolving Jest "No tests found")

  docker, dockerfile, github, github-actions, jestjs

Can I use WORKDIR in my Dockerfile with Github Actions?

I am switching from one CI provider to Github Actions and found that I had a step that runs docker run <temp_image> npm test -- --coverage and something seemed to be altering the way my Jest test were run, compared to my previous CI, and I would receive the error:

No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /app
  18 files checked.
  testMatch: /**/?(*.)+(spec|test).[jt]s?(x) - 1 match
  testPathIgnorePatterns: /.next/, /node_modules/, /testconfig/ - 16 matches
  testRegex:  - 0 matches
Pattern:  - 0 matches

That one testMatch would run correctly in my previous CI solution, using the same command.

Some with this problem were accidentally ignoring their test path : Jest No Tests found

I tried a bunch of different approaches — the main hunch being that my tests were being run in the incorrect directory. Using my shotgun approach I tried:

  • Specifying for testMatch and testPathIgnorePatterns
  • Removing testPathIgnorePatterns altogether
  • Specifying the –config path, also –no-cache, options for Jest
  • Specifying the -w working directory on docker run options
  • And a multi-command approach for docker run /bin/sh/ (cd /app && npm test)

Ultimately, I found this line in Github Actions Docs: Dockerfile Instructions And Overrides

GitHub sets the working directory path in the GITHUB_WORKSPACE environment variable. It’s recommended to not use the WORKDIR instruction in your Dockerfile.

Removing the WORKDIR instruction in my Dockerfile fixes my error

BUT, Is there a way around having to remove WORKDIR from my Dockerfile? It seems to maybe be a Docker best practice to use WORKDIR and I would prefer to follow Docker guidelines than Github Actions.

Thank you for your time!

Source: Docker Questions