Skip to content

Implement module mocking in browser mode #3046

@christian-bromann

Description

@christian-bromann

Describe the bug

The following test:

import { test, vi } from 'vitest'
import { v4 } from 'uuid'

vi.mock('uuid', () => ({
    v4: 'I am mocked'
}))

test('I live in the browser', () => {
    console.log('YES', window.navigator.userAgent, v4)
})

throws an error:

 FAIL  tests/browser.test.js [ tests/browser.test.js ]
TypeError: this._mocker.queueMock is not a function
 ❯ VitestUtils.mock http:/localhost:63315/node_modules/.vite/deps/chunk-DGJ3HNQR.js?v=22cddd7a:10193:18
 ❯ http:/localhost:63315/private/tmp/vitestbrowser/tests/browser.test.js?v=1679345265563:4:4

Reproduction

see above

System Info

System:
    OS: macOS 12.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 420.98 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 18.7.0 - ~/.nvm/versions/node/v18.7.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.7.0/bin/yarn
    npm: 8.15.0 - ~/.nvm/versions/node/v18.7.0/bin/npm
  Browsers:
    Brave Browser: 111.1.49.120
    Chrome: 111.0.5563.64
    Firefox: 109.0
    Firefox Nightly: 111.0a1
    Safari: 15.2
  npmPackages:
    @vitest/browser: ^0.29.7 => 0.29.7
    vitest: ^0.29.7 => 0.29.7

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfeat: browserIssues and PRs related to the browser runner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions