vitest
vitest copied to clipboard
vi.mock does not seem to mock ESM imports when running in a docker container.
Describe the bug
On Node 16 with a package type of "module" I am trying to mock and ESM import. Locally (Mac OS), this works as intended. When attempting it in a docker container however, the module does not seem to be mocked.
I have tried both the alpine and ubuntu based node containers with no luck. Both my local as well as docker are running the same version of node.
Reproduction
this GitHub repo is a minimal reproduction of the bug. npm test
locally works. While inside the container fails. See the Github Actions to reproduce.
System Info
System:
OS: Linux 5.10 Alpine Linux
CPU: (6) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
Memory: 6.69 GB / 7.78 GB
Container: Yes
Shell: 1.34.1 - /bin/ash
Binaries:
Node: 16.14.2 - /usr/local/bin/node
Yarn: 1.22.18 - /usr/local/bin/yarn
npm: 8.5.0 - /usr/local/bin/npm
Used Package Manager
npm
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
This issue stems from a bad configuration of your Dockerfile. I am not very proficient in Docker, but one thing I have encountered several times is that certain things won't work as expected if your workdir
is not in /usr/<someuser>
.
Here's a working Docker config:
FROM node:16.14.2-alpine
ADD . /usr/src/app
WORKDIR /usr/src/app
RUN npm ci
RUN npm test
Yes, you have a problem with your path. Please, either follow @aMediocreDad's advice or pass correct --root
option.
This appears to be a bug with Vitest itself and not with Docker. I've been using Docker for over 6 years and have never had to use a WORKDIR under /usr/something. I've almost always used /app. Docker does not have an opinion or recommendation on the working directory path. When I use /usr/app everything works as I would expect. However, when I use /app Vitest executes just fine, and nearly all my tests pass, except those that are using module mocks. The vi.mock doesn't even work in this case and the original import is what gets used instead.
This appears to be a bug with Vitest itself and not with Docker. I've been using Docker for over 6 years and have never had to use a WORKDIR under /usr/something. I've almost always used /app. Docker does not have an opinion or recommendation on the working directory path. When I use /usr/app everything works as I would expect. However, when I use /app Vitest executes just fine, and nearly all my tests pass, except those that are using module mocks. The vi.mock doesn't even work in this case and the original import is what gets used instead.
I second this, as I just experienced this issue as I switched over from jest to vitest. I can confirm that the above "fix" does work, but this shouldn't be a requirement to make vitest run properly in Docker.
this is annoying, please fix it
this is annoying, please fix it
You can help by providing reproduction.
Please, can you try v0.16.0
?
I've raised #1645, and I've not hit the same issue with other ESM imports. I'm wondering if this could be related?
I have a repro in that issue.