|
2 | 2 | * @vitest-environment jsdom
|
3 | 3 | */
|
4 | 4 |
|
5 |
| -import type { Mock, MockedFunction, MockedObject } from 'vitest' |
| 5 | +import type { Mock, MockInstance, MockedFunction, MockedObject } from 'vitest' |
6 | 6 | import { describe, expect, expectTypeOf, test, vi } from 'vitest'
|
7 | 7 | import { getWorkerState } from '../../../packages/vitest/src/utils'
|
8 | 8 |
|
@@ -118,6 +118,40 @@ describe('testing vi utils', () => {
|
118 | 118 | expect(someFn4).not.toBeCalled()
|
119 | 119 | })
|
120 | 120 |
|
| 121 | + test(`vi.spyOn for function overload types`, () => { |
| 122 | + class MyElement { |
| 123 | + scrollTo(options?: ScrollToOptions): void |
| 124 | + scrollTo(x: number, y: number): void |
| 125 | + scrollTo() {} |
| 126 | + } |
| 127 | + |
| 128 | + // verify `spyOn` is assignable to `MockInstance` with overload |
| 129 | + const spy: MockInstance<MyElement['scrollTo']> = vi.spyOn( |
| 130 | + MyElement.prototype, |
| 131 | + 'scrollTo', |
| 132 | + ) |
| 133 | + |
| 134 | + // however `Parameters` only picks up the last overload |
| 135 | + // due to typescript limitation |
| 136 | + expectTypeOf(spy.mock.calls).toEqualTypeOf< |
| 137 | + [x: number, y: number][] |
| 138 | + >() |
| 139 | + }) |
| 140 | + |
| 141 | + test(`mock.contexts types`, () => { |
| 142 | + class TestClass { |
| 143 | + f(this: TestClass) {} |
| 144 | + g() {} |
| 145 | + } |
| 146 | + |
| 147 | + const fSpy = vi.spyOn(TestClass.prototype, 'f') |
| 148 | + const gSpy = vi.spyOn(TestClass.prototype, 'g') |
| 149 | + |
| 150 | + // contexts inferred only when `this` is explicitly annotated |
| 151 | + expectTypeOf(fSpy.mock.contexts).toEqualTypeOf<TestClass[]>() |
| 152 | + expectTypeOf(gSpy.mock.contexts).toEqualTypeOf<unknown[]>() |
| 153 | + }) |
| 154 | + |
121 | 155 | test('can change config', () => {
|
122 | 156 | const state = getWorkerState()
|
123 | 157 | expect(state.config.hookTimeout).toBe(10000)
|
|
0 commit comments