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

Intl.RelativeTimeFormat and Intl.ListFormat can't be spied upon #6104

Open
6 tasks done
amannn opened this issue Jul 12, 2024 · 4 comments · May be fixed by #6160
Open
6 tasks done

Intl.RelativeTimeFormat and Intl.ListFormat can't be spied upon #6104

amannn opened this issue Jul 12, 2024 · 4 comments · May be fixed by #6160
Labels
breaking change p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@amannn
Copy link

amannn commented Jul 12, 2024

Describe the bug

When using vi.spyOn(Intl, 'RelativeTimeFormat') or vi.spyOn(Intl, 'ListFormat'), the constructor can no longer be called due to:

Constructor Intl.RelativeTimeFormat requires 'new'

Note that Intl.DateTimeFormat works flawlessly.

Reproduction

StackBlitz

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (6) 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:
    @vitest/ui: latest => 2.0.2 
    vite: latest => 5.3.3 
    vitest: latest => 2.0.2 

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

Respecting new in a constructor would be a breaking change as it will change the value of mock.instances. However, you can already use tinyspy directly:

import { spyOn } from 'tinyspy'
const spy = spyOn(Intl, 'RelativeTimeFormat')

@sheremet-va sheremet-va added p2-edge-case Bug, but has workaround or limited in scope (priority) and removed pending triage labels Jul 12, 2024
@amannn
Copy link
Author

amannn commented Jul 12, 2024

Many thanks for the super quick response! That seems to work, thanks for sharing a workaround.

Any idea why this works for Intl.DateTimeFormat and Intl.NumberFormat then?

@sheremet-va
Copy link
Member

Any idea why this works for Intl.DateTimeFormat and Intl.NumberFormat then?

Because they are not implemented as classes internally.

@amannn
Copy link
Author

amannn commented Jul 12, 2024

Oh right, that's interesting:

Screenshot 2024-07-12 at 12 42 26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
Status: Approved
Development

Successfully merging a pull request may close this issue.

2 participants