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

isMockedFunction returns wrong value after object is spied on twice #6419

Open
6 tasks done
kwojcik opened this issue Aug 28, 2024 · 1 comment · May be fixed by #6464
Open
6 tasks done

isMockedFunction returns wrong value after object is spied on twice #6419

kwojcik opened this issue Aug 28, 2024 · 1 comment · May be fixed by #6464
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) upstream

Comments

@kwojcik
Copy link

kwojcik commented Aug 28, 2024

Describe the bug

Spy on an object twice and call restoreAllMocks, then isMockedFunction still thinks that it is a mock function.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-bhbdof?file=test%2Fbasic.test.ts

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

const fooService = { foo: () => "foo" };
describe("fooService", () => {
  describe("foo", () => {
    it("is not a mock functon after 1 spy", () => {
      vi.spyOn(fooService, "foo").mockImplementation(() => "bar");
      vi.restoreAllMocks();
      expect(vi.isMockFunction(fooService.foo)).toBe(false);
    });
    it("it not a mock function after 2 spys", () => {
      vi.spyOn(fooService, "foo").mockImplementation(() => "bar");
      vi.spyOn(fooService, "foo").mockImplementation(() => "bar");
      vi.restoreAllMocks();
      expect(vi.isMockFunction(fooService.foo)).toBe(false);
    });
  });
});
 ❯ test/fooService.test.ts (2)
   ❯ fooService (2)
     ❯ foo (2)
       ✓ is not a mock functon after 1 spy
       × it not a mock function after 2 spys

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  test/fooService.test.ts > fooService > foo > it not a mock function after 2 spys
AssertionError: expected true to be false // Object.is equality

- Expected
+ Received

- false
+ true

 ❯ test/fooService.test.ts:15:49
     13|       vi.spyOn(fooService, "foo").mockImplementation(() => "bar");
     14|       vi.restoreAllMocks();
     15|       expect(vi.isMockFunction(fooService.foo)).toBe(false);
       |                                                 ^
     16|     });
     17|   });

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: latest => 2.0.5 
    vite: latest => 5.4.2 
    vitest: latest => 2.0.5

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

I think it's a bug with https://github.com/tinylibs/tinyspy. It restores the previous property decorator, not the original one. Maybe if the property is already spied, we need to return the spy instead of reapplying the spy.

@sheremet-va sheremet-va added upstream p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Aug 29, 2024
@sheremet-va sheremet-va linked a pull request Sep 9, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) upstream
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants