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

Import order breaks tests when using vi.mock #6496

Closed
6 tasks done
gabz75 opened this issue Sep 13, 2024 · 2 comments
Closed
6 tasks done

Import order breaks tests when using vi.mock #6496

gabz75 opened this issue Sep 13, 2024 · 2 comments

Comments

@gabz75
Copy link

gabz75 commented Sep 13, 2024

Describe the bug

The stackblitz below illustrates the issue.

Let's say I have a function main that calls a function foobar. When testing main, I would like to mock foobar and have the mock implementation in a separate file.

In my test file, if I import the file with the mock implementation BEFORE the file where the function main is, everything works fine.

However if I import the file with the mock implementation AFTER the file where the function main is,vitest complains with the following hoisting error:

Screenshot 2024-09-13 at 11 10 10 AM

Notes: this example might be silly for functions but this pattern comes in handy in different context when trying to mock components and organize code in several files.

Reproduction

https://stackblitz.com/edit/vitejs-vite-q8pc8e?file=main.test.js&view=editor

Simply run the command npm exec vitest to see the issue.

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^5.4.3 => 5.4.4 
    vitest: ^2.1.0 => 2.1.1

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

vi.mock is executed before your imports so you don't have access to them: https://vitest.dev/api/vi.html#vi-mock

If you need to import something during mocking, use import inside the factory:

vi.mock('./deps', async () => {
  const { foobar } = await import( './mock-deps')
  return { foobar }
})

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Sep 13, 2024
@gabz75
Copy link
Author

gabz75 commented Sep 16, 2024

vi.mock is executed before your imports so you don't have access to them: https://vitest.dev/api/vi.html#vi-mock

If you need to import something during mocking, use import inside the factory:

vi.mock('./deps', async () => {
  const { foobar } = await import( './mock-deps')
  return { foobar }
})

Thanks @sheremet-va, I appreciate your quick response. Is there a reason why some import works? For example, the following works:

import html from 'svelte-htm';

vi.mock('./BookmarkIcon.svelte', () => ({
  default: html`<div data-testid="mock-icon" />`,
}));

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

2 participants