Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Jason3S/tinyspy-issue-32

Repository files navigation

1.1.0 breaks instanceOf tests

Issue: tinylibs/tinyspy#32

Getting Started

  1. Install pnpm
  2. pnpm i
  3. pnpm test

It should be possible to mock a class and have instanceof return the right value.

To change to a working version, change

   "pnpm": {
     "overrides": {
-      "tinyspy": "1.1.0"
+      "tinyspy": "1.0.2"
     }
   },

StackBlitz

StackBlitz

Terminal

pnpm install && pnpm run test:ui

Files

MyClass.test.ts

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);
    expect(instance).toBeInstanceOf(MyClass);
    expect(mockedMyClass).toHaveBeenCalledTimes(1);
    expect(mockedMyClass).toHaveBeenCalledWith(expected);
  });
});

MyClass.ts

export class MyClass {
  constructor(readonly name: string) {}
}

MyClassUtil.ts

import { MyClass } from './MyClass.js';

export function create(name: string): MyClass {
  return new MyClass(name);
}

export function isMyClass(val: unknown): val is MyClass {
  return val instanceof MyClass;
}

About

1.1.0 breaks instanceOf tests

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published