Issue: tinylibs/tinyspy#32
- Install
pnpm
pnpm i
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"
}
},
Terminal
pnpm install && pnpm run test:ui
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);
});
});
export class MyClass {
constructor(readonly name: string) {}
}
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;
}