src/vs/base/test/common/snapshot.ts
187 LOC · 180 covered · 7 uncovered · 45 ranges · 686 concepts · 13 introducers · 306 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
snapshot.ts ×5
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Lazy } from '../../common/lazy.js';
import { FileAccess } from '../../common/network.js';
import { URI } from '../../common/uri.js';
declare const __readFileInTests: (path: string) => Promise<string>;
declare const __writeFileInTests: (path: string, contents: string) => Promise<void>;
declare const __readDirInTests: (path: string) => Promise<string[]>;
declare const __unlinkInTests: (path: string) => Promise<void>;
declare const __mkdirPInTests: (path: string) => Promise<void>;
// setup on import so assertSnapshot has the current context without explicit passing
let context: Lazy<SnapshotContext> | undefined;
const sanitizeName = (name: string) => name.replace(/[^a-z0-9_-]/gi, '_');
const normalizeCrlf = (str: string) => str.replace(/\r\n/g, '\n');
export interface ISnapshotOptions {
/** Name for snapshot file, rather than an incremented number */
name?: string;
/** Extension name of the snapshot file, defaults to `.snap` */
extension?: string;
}
/**
* This is exported only for tests against the snapshotting itself! Use
* {@link assertSnapshot} as a consumer!
*/
export class SnapshotContext {
private nextIndex = 0;
protected snapshotsDir: URI;
private readonly namePrefix: string;
private readonly usedNames = new Set();
constructor(private readonly test: Mocha.Test | undefined) {
throw new Error('assertSnapshot can only be used in a test');
}
if (!test.file) {
throw new Error('currentTest.file is not set, please open an issue with the test you\'re trying to run');
}
const src = URI.joinPath(FileAccess.asFileUri(''), '../src');
const parts = test.file.split(/[/\\]/g);
this.namePrefix = sanitizeName(test.fullTitle()) + '.';
this.snapshotsDir = URI.joinPath(src, ...[...parts.slice(0, -1), '__snapshots__']);
}
public async assert(value: unknown, options?: ISnapshotOptions) {
const originalStack = new Error().stack!; // save to make the stack nicer on failure
snapshot.ts ×12
const nameOrIndex = (options?.name ? sanitizeName(options.name) : this.nextIndex++);
const fileName = this.namePrefix + nameOrIndex + '.' + (options?.extension || 'snap');
this.usedNames.add(fileName);
const fpath = URI.joinPath(this.snapshotsDir, fileName).fsPath;
const actual = formatValue(value);
let expected: string;
try {
expected = await __readFileInTests(fpath);
} catch {
await __mkdirPInTests(this.snapshotsDir.fsPath);
await __writeFileInTests(fpath, actual);
return;
}
if (normalizeCrlf(expected) !== normalizeCrlf(actual)) {
const err: any = new Error(`Snapshot #${nameOrIndex} does not match expected output`);
err.expected = expected;
err.actual = actual;
err.snapshotPath = fpath;
err.stack = (err.stack as string)
.split('\n')
// remove all frames from the async stack and keep the original caller's frame
.slice(0, 1)
.concat(originalStack.split('\n').slice(3))
.join('\n');
throw err;
}
public async removeOldSnapshots() {
const toDelete = contents.filter(f => f.startsWith(this.namePrefix) && !this.usedNames.has(f));
if (toDelete.length) {
console.info(`Deleting ${toDelete.length} old snapshots for ${this.test?.fullTitle()}`);
snapshot.ts ×1
}
await Promise.all(toDelete.map(f => __unlinkInTests(URI.joinPath(this.snapshotsDir, f).fsPath)));
}
const debugDescriptionSymbol = Symbol.for('debug.description');
function formatValue(value: unknown, level = 0, seen: unknown[] = []): string {
snapshot.ts ×12
switch (typeof value) {
case 'bigint':
case 'boolean':
case 'number':
case 'symbol':
case 'undefined':
}
}
}
if (debugDescriptionSymbol in value && typeof (value as any)[debugDescriptionSymbol] === 'function') {
return (value as any)[debugDescriptionSymbol]();
}
const ci = ' '.repeat(level + 1);
if (Array.isArray(value)) {
const multiline = children.some(c => c.includes('\n')) || children.join(', ').length > 80;
return multiline ? `[\n${ci}${children.join(`,\n${ci}`)}\n${oi}]` : `[ ${children.join(', ')} ]`;
}
let entries;
let prefix = '';
if (value instanceof Map) {
entries = [...value.entries()];
entries = [...value.entries()];
entries = Object.entries(value);
}
const lines = entries.map(([k, v]) => `${k}: ${formatValue(v, level + 1, [...seen, value])}`);
return prefix + (lines.length > 1
throw new Error(`Unknown type ${value}`);
}
setup(function () {
const currentTest = this.currentTest;
context = new Lazy(() => new SnapshotContext(currentTest));
});
teardown(async function () {
if (this.currentTest?.state === 'passed') {
await context?.rawValue?.removeOldSnapshots();
}
context = undefined;
});
/**
* Implements a snapshot testing utility. ⚠️ This is async! ⚠️
*
* The first time a snapshot test is run, it'll record the value it's called
* with as the expected value. Subsequent runs will fail if the value differs,
* but the snapshot can be regenerated by hand or using the Selfhost Test
* Provider Extension which'll offer to update it.
*
* The snapshot will be associated with the currently running test and stored
* in a `__snapshots__` directory next to the test file, which is expected to
* be the first `.test.js` file in the callstack.
*/
export function assertSnapshot(value: unknown, options?: ISnapshotOptions): Promise<void> {
throw new Error('assertSnapshot can only be used in a test');
}
return context.value.assert(value, options);
}