37
38
constructor(private readonly test: Mocha.Test | undefined) {
40
throw new Error('assertSnapshot can only be used in a test');
41
}
43
>
if (!test.file) {
44
throw new Error('currentTest.file is not set, please open an issue with the test you\'re trying to run');
45
}
47
>
const src = URI.joinPath(FileAccess.asFileUri(''), '../src');
48
>
const parts = test.file.split(/[/\\]/g);
49
>
50
>
this.namePrefix = sanitizeName(test.fullTitle()) + '.';
51
>
this.snapshotsDir = URI.joinPath(src, ...[...parts.slice(0, -1), '__snapshots__']);
52
>
}
53
54
public async assert(value: unknown, options?: ISnapshotOptions) {
55
>
const originalStack = new Error().stack!; // save to make the stack nicer on failure
snapshot.ts
56
>
const nameOrIndex = (options?.name ? sanitizeName(options.name) : this.nextIndex++);
57
>
const fileName = this.namePrefix + nameOrIndex + '.' + (options?.extension || 'snap');
58
>
this.usedNames.add(fileName);
59
>
60
>
const fpath = URI.joinPath(this.snapshotsDir, fileName).fsPath;
61
>
const actual = formatValue(value);
62
>
let expected: string;
63
>
try {
64
>
expected = await __readFileInTests(fpath);
65
>
} catch {
66
console.info(`Creating new snapshot in: ${fpath}`);
67
await __mkdirPInTests(this.snapshotsDir.fsPath);