Path: blob/main/extensions/copilot/test/simulation/testSnapshot.ts
13389 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import { SnapshotClient } from '@vitest/snapshot';5import { NodeSnapshotEnvironment } from '@vitest/snapshot/environment';6import * as assert from 'assert';7import { createServiceIdentifier } from '../../src/util/common/services';8import { Lazy } from '../../src/util/vs/base/common/lazy';910export const ITestSnapshots = createServiceIdentifier<ITestSnapshots>('ITestSnapshots');1112export interface ITestSnapshots {13readonly _serviceBrand: undefined;1415matches(value: unknown, message?: string): Promise<void>;16}1718export class TestSnapshotsImpl implements ITestSnapshots {19declare readonly _serviceBrand: undefined;2021private readonly client = new Lazy(async () => {22const client = new SnapshotClient({23isEqual: (received, expected) => {24try {25assert.deepStrictEqual(received, expected);26return true;27} catch {28return false;29}30}31});3233const name = this.runNumber !== undefined ? `${this.testName}-${this.runNumber}` : this.testName;34await client.startCurrentRun(this.filePath, name, {35updateSnapshot: 'new',36snapshotEnvironment: new NodeSnapshotEnvironment(),37});3839return client;40});4142constructor(43private readonly filePath: string,44private readonly testName: string,45private readonly runNumber?: number,46) { }474849public async matches(value: unknown, message?: string): Promise<void> {50(await this.client.value).assert({51received: value,52isInline: false,53message,54});55}5657public async dispose() {58const client = await this.client.rawValue;59const r = await client?.finishCurrentRun();60if (r?.unmatched) {61throw new Error(`${r.unmatched} snapshot(s) failed to match`);62}63}64}656667