Closed nicojs closed 1 month ago
I don't think importing from vitest
(which also imports from @vitest/browser
by extension) should have side effects at all, correct?
Excellent, thanks, @sheremet-va, for fixing it so fast 👐 I can confirm that it works (although I have no clue why, looking at the code 😅).
Excellent, thanks, @sheremet-va, for fixing it so fast 👐 I can confirm that it works (although I have no clue why, looking at the code 😅).
Rollup sees that it can't remove the file content because there is a side effect of calling a method on a globalThis
. We are now saying to Rollup that it's OK to remove the file even if it detects side effects (basically, we override its detection)
Describe the bug
When running vitest inside a mocha test suite, the error "Cannot read properties of undefined (reading 'navigator')" is thrown from a global
afterEach
handler registered by@vitest/browser
.For example:
I've investigated this somewhat, and vitest includes some reset logic for mocking in its index.js bundle. However, this should not be included in the server-side code as it assumes running in the browser (as it tries to read from
globalThis.window.navigator
).I cannot find this code inside the vitest monorepo, so I think it is a dependency that gets bundled in via rollup.
Code that causes the issue
```js const ClipboardStubControl = Symbol('Manage ClipboardSub'); function isClipboardStub(clipboard) { return !!(clipboard === null || clipboard === void 0 ? void 0 : clipboard[ClipboardStubControl]); } function resetClipboardStubOnView(window) { if (isClipboardStub(window.navigator.clipboard)) { window.navigator.clipboard[ClipboardStubControl].resetClipboardStub(); } } function detachClipboardStubFromView(window) { if (isClipboardStub(window.navigator.clipboard)) { window.navigator.clipboard[ClipboardStubControl].detachClipboardStub(); } } const g = globalThis; /* istanbul ignore else */ if (typeof g.afterEach === 'function') { g.afterEach(()=>resetClipboardStubOnView(globalThis.window)); } /* istanbul ignore else */ if (typeof g.afterAll === 'function') { g.afterAll(()=>detachClipboardStubFromView(globalThis.window)); } ```Reproduction
I've created a simple reproduction repo for this: vitest-issue.zip
The basics is that I've created a simple mocha test that starts and stops vitest. In the vitest config I've configured browser mode.
System Info
Used Package Manager
npm
Validations