Skip to content

Commit

Permalink
refactor: use Map for mockMap (#971)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 17, 2022
1 parent 8a25a78 commit 8a96eaf
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/vitest/src/integrations/jest-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ export function isMockFunction(fn: any): fn is EnhancedSpy {
export function spyOn<T, S extends Properties<Required<T>>>(
obj: T,
methodName: S,
accesType: 'get',
accessType: 'get',
): SpyInstance<[], T[S]>
export function spyOn<T, G extends Properties<Required<T>>>(
obj: T,
methodName: G,
accesType: 'set',
accessType: 'set',
): SpyInstance<[T[G]], void>
export function spyOn<T, M extends (Methods<Required<T>> | Classes<Required<T>>)>(
obj: T,
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/execute.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ViteNodeRunner } from 'vite-node/client'
import type { ModuleCache, ViteNodeRunnerOptions } from 'vite-node'
import { normalizePath } from 'vite'
import type { SuiteMocks } from '../types/mocker'
import type { MockMap } from '../types/mocker'
import { getWorkerState } from '../utils'
import { VitestMocker } from './mocker'

export interface ExecuteOptions extends ViteNodeRunnerOptions {
mockMap: SuiteMocks
mockMap: MockMap
}

export async function executeInViteNode(options: ExecuteOptions & { files: string[] }) {
Expand Down
15 changes: 9 additions & 6 deletions packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export class VitestMocker {

public getMocks() {
const suite = this.getSuiteFilepath()
const suiteMocks = this.mockMap[suite]
const globalMocks = this.mockMap.global
const suiteMocks = this.mockMap.get(suite)
const globalMocks = this.mockMap.get('global')

return {
...globalMocks,
Expand Down Expand Up @@ -190,17 +190,20 @@ export class VitestMocker {

const fsPath = this.normalizePath(path)

if (this.mockMap[suitefile]?.[fsPath])
delete this.mockMap[suitefile][fsPath]
const mock = this.mockMap.get(suitefile)
if (mock?.[fsPath])
delete mock[fsPath]
}

public mockPath(path: string, external: string | null, factory?: () => any) {
const suitefile = this.getSuiteFilepath()

const fsPath = this.normalizePath(path)

this.mockMap[suitefile] ??= {}
this.mockMap[suitefile][fsPath] = factory || this.resolveMockPath(path, external)
if (!this.mockMap.has(suitefile))
this.mockMap.set(suitefile, {})

this.mockMap.get(suitefile)![fsPath] = factory || this.resolveMockPath(path, external)
}

public async importActual<T>(id: string, importer: string): Promise<T> {
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/runtime/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ModuleCacheMap } from 'vite-node/client'
import type { ResolvedConfig, WorkerContext, WorkerRPC } from '../types'
import { distDir } from '../constants'
import { getWorkerState } from '../utils'
import type { MockMap } from '../types/mocker'
import { executeInViteNode } from './execute'
import { rpc } from './rpc'

Expand All @@ -13,7 +14,7 @@ let _viteNode: {
}

const moduleCache = new ModuleCacheMap()
const mockMap = {}
const mockMap: MockMap = new Map()

async function startViteNode(ctx: WorkerContext) {
if (_viteNode)
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/types/mocker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type SuiteMocks = Record<string, Record<string, string | null | (() => unknown)>>
export type MockMap = Map<string, Record<string, string | null | (() => unknown)>>

export interface PendingSuiteMock {
id: string
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/types/worker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MessagePort } from 'worker_threads'
import type { FetchFunction, ModuleCacheMap, RawSourceMap, ViteNodeResolveId } from 'vite-node'
import type { BirpcReturn } from 'birpc'
import type { SuiteMocks } from './mocker'
import type { MockMap } from './mocker'
import type { ResolvedConfig } from './config'
import type { File, TaskResultPack, Test } from './tasks'
import type { SnapshotResult } from './snapshot'
Expand Down Expand Up @@ -38,5 +38,5 @@ export interface WorkerGlobalState {
current?: Test
filepath?: string
moduleCache: ModuleCacheMap
mockMap: SuiteMocks
mockMap: MockMap
}

0 comments on commit 8a96eaf

Please sign in to comment.