Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/testSnapshot.ts
13389 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import { SnapshotClient } from '@vitest/snapshot';
6
import { NodeSnapshotEnvironment } from '@vitest/snapshot/environment';
7
import * as assert from 'assert';
8
import { createServiceIdentifier } from '../../src/util/common/services';
9
import { Lazy } from '../../src/util/vs/base/common/lazy';
10
11
export const ITestSnapshots = createServiceIdentifier<ITestSnapshots>('ITestSnapshots');
12
13
export interface ITestSnapshots {
14
readonly _serviceBrand: undefined;
15
16
matches(value: unknown, message?: string): Promise<void>;
17
}
18
19
export class TestSnapshotsImpl implements ITestSnapshots {
20
declare readonly _serviceBrand: undefined;
21
22
private readonly client = new Lazy(async () => {
23
const client = new SnapshotClient({
24
isEqual: (received, expected) => {
25
try {
26
assert.deepStrictEqual(received, expected);
27
return true;
28
} catch {
29
return false;
30
}
31
}
32
});
33
34
const name = this.runNumber !== undefined ? `${this.testName}-${this.runNumber}` : this.testName;
35
await client.startCurrentRun(this.filePath, name, {
36
updateSnapshot: 'new',
37
snapshotEnvironment: new NodeSnapshotEnvironment(),
38
});
39
40
return client;
41
});
42
43
constructor(
44
private readonly filePath: string,
45
private readonly testName: string,
46
private readonly runNumber?: number,
47
) { }
48
49
50
public async matches(value: unknown, message?: string): Promise<void> {
51
(await this.client.value).assert({
52
received: value,
53
isInline: false,
54
message,
55
});
56
}
57
58
public async dispose() {
59
const client = await this.client.rawValue;
60
const r = await client?.finishCurrentRun();
61
if (r?.unmatched) {
62
throw new Error(`${r.unmatched} snapshot(s) failed to match`);
63
}
64
}
65
}
66
67