Skip to content

Commit

Permalink
Merge branch 'main' into feat/have-been-called-before-after
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes authored Jul 11, 2024
2 parents e6368b3 + e9f9adc commit d083215
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 119 deletions.
10 changes: 0 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@
"esbuild": "^0.22.0",
"eslint": "^9.6.0",
"fast-glob": "^3.3.2",
"lint-staged": "^15.2.7",
"magic-string": "^0.30.10",
"pathe": "^1.1.2",
"rimraf": "^5.0.7",
"rollup": "^4.18.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-esbuild": "^6.1.1",
"rollup-plugin-license": "^3.5.1",
"simple-git-hooks": "^2.11.1",
"tsx": "^4.16.0",
"typescript": "^5.5.2",
"vite": "^5.3.3",
Expand All @@ -87,13 +85,5 @@
"[email protected]": "patches/[email protected]",
"[email protected]": "patches/[email protected]"
}
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
},
"lint-staged": {
"*.{?([cm])[jt]s?(x),vue,md}": [
"eslint --cache --fix"
]
}
}
8 changes: 4 additions & 4 deletions packages/spy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,11 @@ export function spyOn<T, G extends Properties<Required<T>>>(
export function spyOn<T, M extends Classes<Required<T>> | Methods<Required<T>>>(
obj: T,
methodName: M
): Required<T>[M] extends
| { new (...args: infer A): infer R }
| ((...args: infer A) => infer R)
): Required<T>[M] extends { new (...args: infer A): infer R }
? MockInstance<(this: R, ...args: A) => R>
: never
: T[M] extends Procedure
? MockInstance<T[M]>
: never
export function spyOn<T, K extends keyof T>(
obj: T,
method: K,
Expand Down
106 changes: 2 additions & 104 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion test/core/test/vi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @vitest-environment jsdom
*/

import type { Mock, MockedFunction, MockedObject } from 'vitest'
import type { Mock, MockInstance, MockedFunction, MockedObject } from 'vitest'
import { describe, expect, expectTypeOf, test, vi } from 'vitest'
import { getWorkerState } from '../../../packages/vitest/src/utils'

Expand Down Expand Up @@ -118,6 +118,40 @@ describe('testing vi utils', () => {
expect(someFn4).not.toBeCalled()
})

test(`vi.spyOn for function overload types`, () => {
class MyElement {
scrollTo(options?: ScrollToOptions): void
scrollTo(x: number, y: number): void
scrollTo() {}
}

// verify `spyOn` is assignable to `MockInstance` with overload
const spy: MockInstance<MyElement['scrollTo']> = vi.spyOn(
MyElement.prototype,
'scrollTo',
)

// however `Parameters` only picks up the last overload
// due to typescript limitation
expectTypeOf(spy.mock.calls).toEqualTypeOf<
[x: number, y: number][]
>()
})

test(`mock.contexts types`, () => {
class TestClass {
f(this: TestClass) {}
g() {}
}

const fSpy = vi.spyOn(TestClass.prototype, 'f')
const gSpy = vi.spyOn(TestClass.prototype, 'g')

// contexts inferred only when `this` is explicitly annotated
expectTypeOf(fSpy.mock.contexts).toEqualTypeOf<TestClass[]>()
expectTypeOf(gSpy.mock.contexts).toEqualTypeOf<unknown[]>()
})

test('can change config', () => {
const state = getWorkerState()
expect(state.config.hookTimeout).toBe(10000)
Expand Down

0 comments on commit d083215

Please sign in to comment.