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

cannot mock spyOn namespace import in browser mode #6099

Closed
6 tasks done
kwojcik opened this issue Jul 11, 2024 · 2 comments · Fixed by #6289
Closed
6 tasks done

cannot mock spyOn namespace import in browser mode #6099

kwojcik opened this issue Jul 11, 2024 · 2 comments · Fixed by #6289
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@kwojcik
Copy link

kwojcik commented Jul 11, 2024

Describe the bug

If you use spyOn on a namespace import you get this error TypeError: Cannot redefine property:. This seems to work fine in jsdom environment, but not browser mode.

Workaround that still gives you the behavior of spyOn if you need a "default call original" behavior:

import { expect, it, vi } from "vitest";
import { one } from "./basic.js";

vi.mock("./basic.js", async (importActual) => {
  const actual = await importActual<typeof import("./basic.js")>();
  return {
    ...actual,
    one: vi.fn().mockImplementation(actual.one),
  };
});
it("test", () => {
  vi.mocked(one).mockReturnValue(2);
  expect(one()).toBe(2);
});

Reproduction

import { expect, it, vi } from "vitest";
import * as thing from "./basic.js";

it("test", () => {
  vi.spyOn(thing, "one").mockReturnValue(2);
  expect(thing.one()).toBe(2);
});

basic.js

export function one() {
  return 1;
}
 FAIL  test/basic.test.tsx > test
TypeError: Cannot redefine property: one

Failure screenshot:
  - test/__screenshots__/basic.test.tsx/test-1.png

 ❯ test/basic.test.tsx:5:5
      3|
      4| it("test", () => {
      5|   vi.spyOn(thing, "one").mockReturnValue(2);
       |     ^
      6|   expect(thing.one()).toBe(2);
      7| });

System Info

System:
    OS: macOS 13.6.7
    CPU: (12) arm64 Apple M2 Max
    Memory: 4.92 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.18.2 - ~/.asdf/installs/nodejs/18.18.2/bin/node
    Yarn: 1.22.21 - /opt/homebrew/bin/yarn
    npm: 9.8.1 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 6.32.8 - /opt/homebrew/bin/pnpm
    Watchman: 2024.04.01.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 126.0.6478.127
    Firefox Nightly: 127.0
    Safari: 16.6
  npmPackages:
    @vitest/browser: 2.0.2 => 2.0.2
    @vitest/ui: 2.0.2 => 2.0.2
    vite: latest => 5.3.2
    vitest: 2.0.2 => 2.0.2

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

sheremet-va commented Jul 15, 2024

Vitest Browser Mode previously had a mode (called slowHijackESM) when this behaviour was possible, but it made the runner slower and introduced subtle bugs that were hard to find. We effectively had to maintain a separate ESM environment. In the release version, we removed this flag in favor of using vi.mock.

Just calling vi.mock doesn't replicate the behavior though because it overrides the implementation, but maybe we can add a flag to wrap the methods instead of overriding them:

vi.mock("./basic.js", { spy: true })

@sheremet-va sheremet-va added p2-to-be-discussed Enhancement under consideration (priority) and removed pending triage labels Jul 15, 2024
@sheremet-va sheremet-va moved this to P2 - 4 in Team Board Jul 15, 2024
@kwojcik
Copy link
Author

kwojcik commented Jul 16, 2024

I'm not necessarily requesting this feature (since there is a workaround), just encountered it while moving to browser mode. If you decide against this then just having a nice error message that says it's not supported and suggestion of what to do instead would be great.

@sheremet-va sheremet-va moved this from P2 - 4 to Approved in Team Board Jul 18, 2024
@sheremet-va sheremet-va added p2-nice-to-have Not breaking anything but nice to have (priority) and removed p2-to-be-discussed Enhancement under consideration (priority) labels Jul 22, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
Archived in project
2 participants