You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using Vitest Browser, it's important to note that thread blocking dialogs like `alert` or `confirm` cannot be used natively. This is because they block the web page, which means Vitest cannot continue communicating with the page, causing the execution to hang.
585
585
586
586
In such situations, Vitest provides default mocks with default returned values for these APIs. This ensures that if the user accidentally uses synchronous popup web APIs, the execution would not hang. However, it's still recommended for the user to mock these web APIs for better experience. Read more in [Mocking](/guide/mocking).
587
+
588
+
### Spying on Module Exports
589
+
590
+
Browser Mode uses the browser's native ESM support to serve modules. The module namespace object is sealed and can't be reconfigured, unlike in Node.js tests where Vitest can patch the Module Runner. This means you can't call `vi.spyOn` on an imported object:
591
+
592
+
```ts
593
+
import { vi } from'vitest'
594
+
import*asmodulefrom'./module.js'
595
+
596
+
vi.spyOn(module, 'method') // ❌ throws an error
597
+
```
598
+
599
+
To bypass this limitation, Vitest supports `{ spy: true }` option in `vi.mock('./module.js')`. This will automatically spy on every export in the module without replacing them with fake ones.
it('spying on an esm module prints an error',()=>{
5
+
consterror: Error=(()=>{
6
+
try{
7
+
vi.spyOn(module,'calculator')
8
+
expect.unreachable()
9
+
}
10
+
catch(err){
11
+
returnerr
12
+
}
13
+
})()
14
+
expect(error.name).toBe('TypeError')
15
+
expect(error.message).toMatchInlineSnapshot(`"Cannot spy on export "calculator". Module namespace is not configurable in ESM. See: https://vitest.dev/guide/browser/#limitations"`)
0 commit comments