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

feat(vitest): add return type and promisable mockFactory #6139

Merged
6 changes: 5 additions & 1 deletion packages/vitest/src/integrations/vi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { assertTypes, createSimpleStackTrace } from '@vitest/utils'
import { parseSingleStack } from '../utils/source-map'
import type { VitestMocker } from '../runtime/mocker'
import type { ResolvedConfig, RuntimeConfig } from '../types'
import type { MockFactoryWithHelper } from '../types/mocker'
import type { MockFactoryWithHelper, PromiseMockFactoryWithHelper } from '../types/mocker'
import { getWorkerState } from '../utils/global'
import { resetModules, waitForImportsToResolve } from '../utils/modules'
import { isChildProcess } from '../utils/base'
Expand Down Expand Up @@ -194,6 +194,8 @@ export interface VitestUtils {
mock(path: string, factory?: MockFactoryWithHelper): void
// eslint-disable-next-line ts/method-signature-style
mock<T>(module: Promise<T>, factory?: MockFactoryWithHelper<T>): void
// eslint-disable-next-line ts/method-signature-style
mock<T>(module: Promise<T>, factory?: PromiseMockFactoryWithHelper<T>): void

/**
* Removes module from mocked registry. All calls to import will return the original module even if it was mocked before.
Expand All @@ -219,6 +221,8 @@ export interface VitestUtils {
doMock(path: string, factory?: MockFactoryWithHelper): void
// eslint-disable-next-line ts/method-signature-style
doMock<T>(module: Promise<T>, factory?: MockFactoryWithHelper<T>): void
// eslint-disable-next-line ts/method-signature-style
doMock<T>(module: Promise<T>, factory?: PromiseMockFactoryWithHelper<T>): void
/**
* Removes module from mocked registry. All subsequent calls to import will return original module.
*
Expand Down
7 changes: 5 additions & 2 deletions packages/vitest/src/types/mocker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type MockFactoryWithHelper<M = unknown> = (
importOriginal: <T extends M>() => Promise<T>
) => any
importOriginal: () => Promise<M>
) => Partial<Record<keyof M, any>>
export type PromiseMockFactoryWithHelper<M = unknown> = (
importOriginal: () => Promise<M>
) => Promise<Partial<Record<keyof M, any>>>
syi0808 marked this conversation as resolved.
Show resolved Hide resolved
export type MockFactory = () => any

export type MockMap = Map<string, Record<string, string | null | MockFactory>>
Expand Down
Loading