| 9 | const { now } = Date |
| 10 | |
| 11 | export class ModuleMocker implements TestModuleMocker { |
| 12 | protected registry: MockerRegistry = new MockerRegistry() |
| 13 | |
| 14 | private queue = new Set<Promise<void>>() |
| 15 | private mockedIds = new Set<string>() |
| 16 | |
| 17 | constructor( |
| 18 | private interceptor: ModuleMockerInterceptor, |
| 19 | private rpc: ModuleMockerRPC, |
| 20 | private createMockInstance: CreateMockInstanceProcedure, |
| 21 | private config: ModuleMockerConfig, |
| 22 | ) {} |
| 23 | |
| 24 | public async prepare(): Promise<void> { |
| 25 | if (!this.queue.size) { |
| 26 | return |
| 27 | } |
| 28 | await Promise.all([...this.queue.values()]) |
| 29 | } |
| 30 | |
| 31 | public async resolveFactoryModule(id: string): Promise<Record<string | symbol, any>> { |
| 32 | const mock = this.registry.get(id) |
| 33 | if (!mock || mock.type !== 'manual') { |
| 34 | throw new Error(`Mock ${id} wasn't registered. This is probably a Vitest error. Please, open a new issue with reproduction.`) |
| 35 | } |
| 36 | const result = await mock.resolve() |
| 37 | return result |
| 38 | } |
| 39 | |
| 40 | public getFactoryModule(id: string): any { |
| 41 | const mock = this.registry.get(id) |
| 42 | if (!mock || mock.type !== 'manual') { |
| 43 | throw new Error(`Mock ${id} wasn't registered. This is probably a Vitest error. Please, open a new issue with reproduction.`) |
| 44 | } |
| 45 | if (!mock.cache) { |
| 46 | throw new Error(`Mock ${id} wasn't resolved. This is probably a Vitest error. Please, open a new issue with reproduction.`) |
| 47 | } |
| 48 | return mock.cache |
| 49 | } |
| 50 | |
| 51 | public async invalidate(): Promise<void> { |
| 52 | const ids = Array.from(this.mockedIds) |
| 53 | if (!ids.length) { |
| 54 | return |
| 55 | } |
| 56 | await this.rpc.invalidate(ids) |
| 57 | await this.interceptor.invalidate() |
| 58 | this.registry.clear() |
| 59 | } |
| 60 | |
| 61 | public async importActual<T>(id: string, importer: string): Promise<T> { |
| 62 | const resolved = await this.rpc.resolveId(id, importer) |
| 63 | if (resolved == null) { |
| 64 | throw new Error( |
| 65 | `[vitest] Cannot resolve "${id}" imported from "${importer}"`, |
| 66 | ) |
| 67 | } |
| 68 | const ext = extname(resolved.id) |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…