( config: SerializedConfig, moduleRunner: TestModuleRunner, traces: Traces, )
| 30 | } |
| 31 | |
| 32 | export async function resolveTestRunner( |
| 33 | config: SerializedConfig, |
| 34 | moduleRunner: TestModuleRunner, |
| 35 | traces: Traces, |
| 36 | ): Promise<VitestRunner> { |
| 37 | const TestRunner = await getTestRunnerConstructor(config, moduleRunner) |
| 38 | const testRunner = new TestRunner(config) |
| 39 | |
| 40 | // inject private executor to every runner |
| 41 | Object.defineProperty(testRunner, 'moduleRunner', { |
| 42 | value: moduleRunner, |
| 43 | enumerable: false, |
| 44 | configurable: false, |
| 45 | }) |
| 46 | |
| 47 | if (!testRunner.config) { |
| 48 | testRunner.config = config |
| 49 | } |
| 50 | |
| 51 | if (!testRunner.importFile) { |
| 52 | throw new Error('Runner must implement "importFile" method.') |
| 53 | } |
| 54 | |
| 55 | if ('__setTraces' in testRunner) { |
| 56 | (testRunner.__setTraces as any)(traces) |
| 57 | } |
| 58 | |
| 59 | const [diffOptions] = await Promise.all([ |
| 60 | loadDiffConfig(config, moduleRunner), |
| 61 | loadSnapshotSerializers(config, moduleRunner), |
| 62 | ]) |
| 63 | testRunner.config.diffOptions = diffOptions |
| 64 | |
| 65 | // patch some methods, so custom runners don't need to call RPC |
| 66 | const originalOnTaskUpdate = testRunner.onTaskUpdate |
| 67 | testRunner.onTaskUpdate = async (task, events) => { |
| 68 | const p = rpc().onTaskUpdate(task, events) |
| 69 | await originalOnTaskUpdate?.call(testRunner, task, events) |
| 70 | return p |
| 71 | } |
| 72 | |
| 73 | // patch some methods, so custom runners don't need to call RPC |
| 74 | const originalOnTestAnnotate = testRunner.onTestAnnotate |
| 75 | testRunner.onTestAnnotate = async (test, annotation) => { |
| 76 | const p = rpc().onTaskArtifactRecord(test.id, { type: 'internal:annotation', location: annotation.location, annotation }) |
| 77 | const overriddenResult = await originalOnTestAnnotate?.call(testRunner, test, annotation) |
| 78 | const vitestResult = await p |
| 79 | return overriddenResult || vitestResult.annotation |
| 80 | } |
| 81 | |
| 82 | const originalOnTestArtifactRecord = testRunner.onTestArtifactRecord |
| 83 | testRunner.onTestArtifactRecord = async (test, artifact) => { |
| 84 | const p = rpc().onTaskArtifactRecord(test.id, artifact) |
| 85 | const overriddenResult = await originalOnTestArtifactRecord?.call(testRunner, test, artifact) |
| 86 | const vitestResult = await p |
| 87 | return overriddenResult as typeof artifact || vitestResult |
| 88 | } |
| 89 |
no test coverage detected