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

Mocking Modules breaks instanceof on Classes #2824

Closed
6 tasks done
Jason3S opened this issue Feb 7, 2023 · 1 comment
Closed
6 tasks done

Mocking Modules breaks instanceof on Classes #2824

Jason3S opened this issue Feb 7, 2023 · 1 comment

Comments

@Jason3S
Copy link

Jason3S commented Feb 7, 2023

Describe the bug

I think there is a combination of things going on here.

Mocking of modules with classes works with tinyspy 1.0.2 but breaks with tinyspy 1.1.0.

See: tinylibs/tinyspy#32

Looks like the prototype chain isn't getting preserved.

import { afterEach, describe, expect, test, vi } from 'vitest';

import { MyClass } from './MyClass.js';
import { create, isMyClass } from './MyClassUtil.js';

vi.mock('./MyClass.js');

const mockedMyClass = vi.mocked(MyClass);

// class MockImplementationMyClass {
//     constructor(readonly name: string) {}
// }

// mockedMyClass.mockImplementation((name) => new MockImplementationMyClass(name));

describe('MyClass', () => {
  afterEach(() => {
    mockedMyClass.mockClear();
  });

  test.each`
    name          | expected
    ${'Atlantic'} | ${'Atlantic'}
    ${'Pacific'}  | ${'Pacific'}
  `('new MyClass $name', ({ name, expected }) => {
    const instance = create(name);
    expect(isMyClass(instance)).toBe(true);  // Breaks here
    expect(instance).toBeInstanceOf(MyClass); // And here
    expect(mockedMyClass).toHaveBeenCalledTimes(1);
    expect(mockedMyClass).toHaveBeenCalledWith(expected);
  });
});

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-dhhorq?file=README.md

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: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    @vitest/ui: latest => 0.28.4 
    vite: ^4.1.1 => 4.1.1 
    vitest: ^0.26.3 => 0.26.3

Used Package Manager

pnpm

Validations

@sheremet-va
Copy link
Member

This is fixed in tinylibs/tinyspy#32

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

No branches or pull requests

2 participants