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

Auto-mocking classes doesn't work in browser mode #6563

Closed
6 tasks done
scottohara opened this issue Sep 25, 2024 · 1 comment
Closed
6 tasks done

Auto-mocking classes doesn't work in browser mode #6563

scottohara opened this issue Sep 25, 2024 · 1 comment

Comments

@scottohara
Copy link

Describe the bug

Apologies if this is by design, but I couldn't find anywhere that confirms this.

When mocking a module that exports a Class, the expectation is that without passing a factory, that all exports (including the exported Class are automocked).

This seems to work as expected with --environment=jsdom, but when used in --browser=chrome it fails with:

TypeError: Class constructor <name of class> cannot be invoked without 'new'

The error in browser mode can be avoided by supplying a factory to the mock function, e.g. instead of:

vi.mock("./foo.js");

we can pass:

vi.mock("./foo.js", () => {
  return {
    Foo: vi.fn()
  };
});

But it is unclear why this additional boilerplate is only needed in browser mode?

Reproduction

https://github.com/scottohara/vitest-browser-automock-class

System Info

System:
    OS: macOS 15.0
    CPU: (8) arm64 Apple M1
    Memory: 88.84 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/.asdf/installs/nodejs/20.11.1/bin/node
    Yarn: 1.22.19 - ~/.asdf/installs/nodejs/20.11.1/bin/yarn
    npm: 10.5.0 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 9.9.0 - ~/.asdf/installs/nodejs/20.11.1/bin/pnpm
  Browsers:
    Chrome: 129.0.6668.59
    Chrome Canary: 131.0.6737.0
    Safari: 18.0
  npmPackages:
    @vitest/browser: 2.1.1 => 2.1.1 
    vitest: 2.1.1 => 2.1.1

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

Spying on classes doesn't work at all: #6160

Closing this as a duplicate of #6104

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Sep 25, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Oct 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants