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

Tests failing when run the test first time after installation #2845

Closed
6 tasks done
kazupon opened this issue Feb 9, 2023 · 5 comments · Fixed by #2857
Closed
6 tasks done

Tests failing when run the test first time after installation #2845

kazupon opened this issue Feb 9, 2023 · 5 comments · Fixed by #2857

Comments

@kazupon
Copy link

kazupon commented Feb 9, 2023

Describe the bug

I've been using vitest to develop nuxt modules so far.
I have changed the code in this PR without changing the code around dependencies and imports. However, when I run the tests, the tests are now failing in CI with the following error code:

TypeError: generateJSON is not a function
 ❯ generateVueI18nOptions src/gen.ts:183:11
    181|       for (const [locale, localeMessages] of Object.entries(value)) {
    182|         genCode += `${JSON.stringify(locale)}:${
    183|           generateJSON(JSON.stringify(localeMessages), { type: 'bare', env: dev ? 'development' : 'production' }).code
       |           ^
    184|         },`
    185|       }
 ❯ src/gen.ts:90:37
 ❯ Module.generateLoaderOptions src/gen.ts:82:41
 ❯ test/gen.test.ts:91:16

It seems that the code I imported is undefined.

I noticed this issue in CI.

Additional info:

Reproduction

Unfortunately, I could not reproduce with minimal codes, but We can reproduce realworld code.

repo: https://github.com/nuxt-modules/i18n

reproduction step:

  1. git clone git@github.com:nuxt-modules/i18n.git
  2. git checkout fix/1834
  3. pnpm install
  4. pnpm test:unit

So, you can face issue.

By the way, if we do pnpm test:unit again, the test works correctly.

System Info

System:
    OS: macOS 13.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 86.55 MB / 64.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.19.0 - ~/.nvm/versions/node/v16.19.0/bin/node
  Browsers:
    Chrome: 109.0.5414.119
    Firefox: 109.0
    Safari: 16.2
  npmPackages:
    vitest: ^0.26.2 => 0.26.2

Used Package Manager

pnpm

Validations

@AriPerkkio
Copy link
Member

AriPerkkio commented Feb 9, 2023

@kazupon as quick work-around for now you can add following in custom_route.test.ts:

afterAll(() => {
  vi.restoreAllMocks()
})

For some reason the restoreMocks: true in configuration is not working. I did some debugging but did not find the root cause. Using this command it's easy to see when tests start to work properly on first run: rm -rf node_modules/.vitest && pnpm test:unit.

@kazupon
Copy link
Author

kazupon commented Feb 10, 2023

@AriPerkkio
Thanks!

Using this command it's easy to see when tests start to work properly on first run: rm -rf node_modules/.vitest && pnpm test:unit.

Yeah!, I have noticed this issue being caused by .vitest too.
I will work around this issue with your ideas and continue for development.

@kazupon
Copy link
Author

kazupon commented Feb 10, 2023

I’ve kept this issue at repro/vitest-issue branch.
I hope this will help to find out the cause of the issue. :)

https://github.com/nuxt-modules/i18n/tree/repro/vitest-issue

@AriPerkkio
Copy link
Member

Minimal reproduction: https://stackblitz.com/edit/vitest-dev-vitest-xeb6cf

@sheremet-va
Copy link
Member

sheremet-va commented Feb 13, 2023

It is a bug that we don't restore mocks between different files. This only affects builtin modules. #2857 should address this.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants