vitest-dev / vitest

Next generation testing framework powered by Vite.

Home Page:https://vitest.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.doMock won't mock if called from an imported module

dearlordylord opened this issue · comments

Describe the bug

If we have a module A and a module B

Where module A has devDependencies of module C and module B

and, if these conditions are true:

  • vi.doMock for module C is called inside module B
  • we're running vitest run in module A
  • and try to import module C after vi.doMock of module B is called

then, callback of vi.doMock never fires, with no error, and import module C returns a real module C

now, if we remove module C from devDependencies of module A, we have vi.doMock called and import module C returns the mocked value

this discrepancy is what makes me think it's a bug or undefined behaviour: I'd expect either both cases fail, or, hopefully, both cases success

Reproduction

https://github.com/Firfi/vittest-base - serves as "module A"

https://github.com/Firfi/vittest-dependency - serves as "module B"

uuid (https://www.npmjs.com/package/uuid) - serves as "module C" (but it's true with every module I tested with)

yarn install in both and link them together with yarn link (in readme) to see the effect

I have the same effect with a published module, therefore I rule out yarn link issues

System Info

  System:
    OS: macOS 13.2.1
    CPU: (8) arm64 Apple M1
    Memory: 111.55 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.n/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 9.5.1 - ~/.n/bin/npm
  Browsers:
    Chrome: 112.0.5615.137
    Edge: 112.0.1722.68
    Firefox: 109.0.1
    Safari: 16.3

Used Package Manager

yarn

Validations

All file resolution is done by Vite - here I assume this is how your package manager resolves it. Also, you provided zero information about your system configuration. What version of yarn? What Node version? Why should I care that you use osx? (I will put a label for reproduction, but I need system info). We provide you with a command to run because we need this information.

Hello @Firfi. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

Thank you for your response @sheremet-va . I overlooked the command npx envinfo --system --npmPackages '{vitest,@vitest/*,vite,@vitejs/*}' --binaries --browsers indeed! It was in too fine print for how late it is on my side of the globe 😅

I updated the system information, assuming this is what you are trying to tell me.

Thank you again for such a swift response.