vitest icon indicating copy to clipboard operation
vitest copied to clipboard

vi.mock does not seem to mock ESM imports when running in a docker container.

Open prowe opened this issue 2 years ago • 8 comments

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

prowe avatar Apr 27 '22 01:04 prowe

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

aMediocreDad avatar Apr 27 '22 08:04 aMediocreDad

Yes, you have a problem with your path. Please, either follow @aMediocreDad's advice or pass correct --root option.

sheremet-va avatar Apr 28 '22 09:04 sheremet-va

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.

wrporter avatar Jun 01 '22 12:06 wrporter

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.

justinwaite avatar Jun 05 '22 05:06 justinwaite

this is annoying, please fix it

pingcheng avatar Jun 24 '22 06:06 pingcheng

this is annoying, please fix it

You can help by providing reproduction.

sheremet-va avatar Jun 24 '22 06:06 sheremet-va

Please, can you try v0.16.0?

sheremet-va avatar Jun 24 '22 13:06 sheremet-va

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.

mrmckeb avatar Jul 18 '22 14:07 mrmckeb