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

[Vitest 3] "Intl.Collator is not a constructor" when using fake timers #7142

Closed
6 tasks done
spaceemotion opened this issue Dec 28, 2024 · 3 comments · Fixed by #7097
Closed
6 tasks done

[Vitest 3] "Intl.Collator is not a constructor" when using fake timers #7142

spaceemotion opened this issue Dec 28, 2024 · 3 comments · Fixed by #7097
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@spaceemotion
Copy link

spaceemotion commented Dec 28, 2024

Describe the bug

I am trying to upgrade to the latest Vitest 3 Beta. Our project does not use Vite for the build-pipeline (only a simple esbuild config, in fact), and we didn't need to do anything so far to get our tests running (we don't even have a vitest config).

However, after the upgrade, we suddenly see the following error in the console:

Intl.Collator is not a constructor

TypeError: Intl.Collator is not a constructor
 ❯ src/services/xxxxx.ts:96:24
     94| };
     95|
     96| const nameComparator = new Intl.Collator('en', {
       |                        ^
     97|   sensitivity: 'base',
     98|   numeric: true,

I thought it might have to do with the Vite 6 module resolution changes, but even tinkering with that one broke more than i liked. The project runs on Node v22, and Vitest v2 has worked without an issue so far.

The error also happens when I run vitest in isolation mode.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-ptkpew1o?file=test%2Fbasic.test.ts

Once the async import gets resolved, Vitest doesn't know what Intl. is.

System Info

System:
  OS: Linux 5.15 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
  CPU: (16) x64 AMD Ryzen 9 7950X 16-Core Processor
  Memory: 25.82 GB / 35.24 GB
  Container: Yes
  Shell: 3.6.1 - /usr/bin/fish
Binaries:
  Node: 22.2.0 - /mnt/wslg/runtime-dir/fnm_multishells/40485_1735384937185/bin/node
  npm: 10.7.0 - /mnt/wslg/runtime-dir/fnm_multishells/40485_1735384937185/bin/npm
  pnpm: 8.15.5 - /mnt/wslg/runtime-dir/fnm_multishells/40485_1735384937185/bin/pnpm
  bun: 1.1.8 - ~/.bun/bin/bun
npmPackages:
  vitest: 3.0.0-beta.3 => 3.0.0-beta.3

Used Package Manager

pnpm

Validations

@spaceemotion spaceemotion changed the title [Vitest 3] Intl.Collator is not a constructor [Vitest 3] Intl.Collator is not a constructor when using async import() in test Dec 28, 2024
@spaceemotion spaceemotion changed the title [Vitest 3] Intl.Collator is not a constructor when using async import() in test [Vitest 3] "Intl.Collator is not a constructor" when using async import() in test Dec 28, 2024
@hi-ogawa hi-ogawa added the p3-minor-bug An edge case that only affects very specific usage (priority) label Dec 29, 2024
@hi-ogawa
Copy link
Contributor

hi-ogawa commented Dec 29, 2024

This looks like due to fake timers. Here is a simplified repro https://stackblitz.com/edit/vitest-dev-vitest-96ajsug5?file=test%2Fbasic.test.ts

In v3, we changed the behavior of toFake to fake everything globally available #6288, which also includes Intl. There is a bug which breaks some Intl.xxx in @sinonjs/fake-timers and it's been fixed on upstream by sinonjs/fake-timers#479, but we are still using fairly old version 11.1.0 (latest is 14.0.0), which is before the fix.

There's a PR #7097 but probably we haven't reviewed yet.

In the mean time, you can explicitly set toFake to be same as v2 like

import { it, vi } from 'vitest';

it('repro', async () => {
  console.log('[before]', Intl.Collator);
  vi.useFakeTimers({
    // or write this in `test.fakeTimers.toFake` config
    toFake: [
      'setTimeout',
      'clearTimeout',
      'setInterval',
      'clearInterval',
      'setImmediate',
      'clearImmediate',
      'Date',
    ],
  });
  console.log('[after]', Intl.Collator);
});

@spaceemotion spaceemotion changed the title [Vitest 3] "Intl.Collator is not a constructor" when using async import() in test [Vitest 3] "Intl.Collator is not a constructor" when using fake timers Dec 29, 2024
@spaceemotion
Copy link
Author

Thanks! that did the trick. I updated the title for this issue accordingly.

@hi-ogawa
Copy link
Contributor

@spaceemotion feel free to try it out pre-release of the PR #7097 (comment) I confirmed the repro is fixed with this https://stackblitz.com/edit/vitest-dev-vitest-52wgjnc6?file=package.json

@github-actions github-actions bot locked and limited conversation to collaborators Jan 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants