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

After upgrading to 0.31.1: TypeError on spies on get methods of a global when restoring mocks #3448

Closed
6 tasks done
rmehner opened this issue May 25, 2023 · 4 comments
Closed
6 tasks done
Labels

Comments

@rmehner
Copy link
Contributor

rmehner commented May 25, 2023

Describe the bug

When you spy on a getter on global and restore it after every test with restoreAllMocks, there will be TypeErrors because global is deemed undefined (TypeError: Cannot read properties of undefined)

The solution is to either downgrade to 0.31.0 or to remove the call to restoreAllMocks, which obviously might have other side effects, depending on the test suite. I suspect #3341 to be the culprit, but didn't have the time to investigate further yet.

Thank you for reading this and for vitest in general, has been a blast using it so far :)

Reproduction

Github Repo: https://github.com/rmehner/vitest-issue-3448

import { afterEach, expect, it, vi } from "vitest";

let globalSpy = vi.spyOn(global, "global", "get");

afterEach(() => {
  // commenting this line, or downgrading to 0.31.0 fixes the problem
  vi.restoreAllMocks();
});

it("returns NOT FOUND when variable has not been set", () => {
  globalSpy.mockImplementation(() => ({ GLOBAL_VAR: undefined }));
  expect(global.GLOBAL_VAR).toEqual(undefined);
});

it("returns the value of variable if it has been set", () => {
  globalSpy.mockImplementation(() => ({ GLOBAL_VAR: "👋" }));
  expect(global.GLOBAL_VAR).toEqual("👋");
});

System Info

System:
    OS: macOS 13.4
    CPU: (12) arm64 Apple M2 Max
    Memory: 51.15 GB / 96.00 GB
    Shell: 5.9 - /opt/homebrew/bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.asdf/installs/nodejs/18.16.0/bin/node
    Yarn: 1.22.19 - ~/.asdf/installs/nodejs/18.16.0/bin/yarn
    npm: 9.5.1 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Chrome: 113.0.5672.126
    Edge: 113.0.1774.35
    Firefox: 113.0.1
    Safari: 16.5
  npmPackages:
    vitest: 0.31.1 => 0.31.1

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

This looks like a terrible pattern. Why won't you just use vi.stubGlobal or vi.spyOn(global, 'GLOBAL_VAR', 'get').mockImplementation(() => undefined)?

@rmehner
Copy link
Contributor Author

rmehner commented May 26, 2023

Hi @sheremet-va,

vi.stubGlobal is something we didn't use, since vi.unstubAllGlobals wasn't a thing when we started using vitest.

Honestly have no idea why we didn't go for the second option you mentioned, might have been confusion on our side :)

Either way, changed the code accordingly and things work as expected. Thank you for the pointers!

I'm not entirely sure what you want to do with this bug report, since it worked before and it's not working now, so one could argue this is a regression, but I'm fine with it being wrong usage on our side.

@sheremet-va
Copy link
Member

It is still a bug, so issue should be opened.

@sheremet-va
Copy link
Member

Should be fixed by #3315 (the fix itself is updating tinyspy to 2.1.1)

@github-actions github-actions bot locked and limited conversation to collaborators Jan 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants