snapshot.ts ×5

Frontier kind: Code frontier

unlabeled · c_f4018775a83c

306 tests · 5363 LOC · 28 files · introduces 0 tests · 71 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
5 ranges71 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
751 ranges5363 lines · 28 files · Browse complete extent
All tests (intent)
306 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

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 native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 71 introduced LOC across 5 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/base/test/common/snapshot.ts 71 introduced LOC · 5 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- snapshot.ts
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 >
6 > import { Lazy } from '../../common/lazy.js';
7 > import { FileAccess } from '../../common/network.js';
8 > import { URI } from '../../common/uri.js';
9 >
10 > declare const __readFileInTests: (path: string) => Promise<string>;
11 > declare const __writeFileInTests: (path: string, contents: string) => Promise<void>;
12 > declare const __readDirInTests: (path: string) => Promise<string[]>;
13 > declare const __unlinkInTests: (path: string) => Promise<void>;
14 > declare const __mkdirPInTests: (path: string) => Promise<void>;
15 >
16 > // setup on import so assertSnapshot has the current context without explicit passing
17 > let context: Lazy<SnapshotContext> | undefined;
18 > const sanitizeName = (name: string) => name.replace(/[^a-z0-9_-]/gi, '_');
19 > const normalizeCrlf = (str: string) => str.replace(/\r\n/g, '\n');
20 >
21 > export interface ISnapshotOptions {
22 > /** Name for snapshot file, rather than an incremented number */
23 > name?: string;
24 > /** Extension name of the snapshot file, defaults to `.snap` */
25 > extension?: string;
26 > }
27 >
28 > /**
29 > * This is exported only for tests against the snapshotting itself! Use
30 > * {@link assertSnapshot} as a consumer!
31 > */
32 > export class SnapshotContext {
33 > private nextIndex = 0;
34 > protected snapshotsDir: URI;
35 > private readonly namePrefix: string;
36 > private readonly usedNames = new Set();
37 >
38 > constructor(private readonly test: Mocha.Test | undefined) {
39 if (!test) {
40 throw new Error('assertSnapshot can only be used in a test');
51 this.snapshotsDir = URI.joinPath(src, ...[...parts.slice(0, -1), '__snapshots__']);
52 }
54 > public async assert(value: unknown, options?: ISnapshotOptions) {
55 const originalStack = new Error().stack!; // save to make the stack nicer on failure
56 const nameOrIndex = (options?.name ? sanitizeName(options.name) : this.nextIndex++);
85 }
86 }
88 > public async removeOldSnapshots() {
89 const contents = await __readDirInTests(this.snapshotsDir.fsPath);
90 const toDelete = contents.filter(f => f.startsWith(this.namePrefix) && !this.usedNames.has(f));
95 await Promise.all(toDelete.map(f => __unlinkInTests(URI.joinPath(this.snapshotsDir, f).fsPath)));
96 }
97 > } snapshot.ts
98 >
99 > const debugDescriptionSymbol = Symbol.for('debug.description');
100 >
101 function formatValue(value: unknown, level = 0, seen: unknown[] = []): string {
102 switch (typeof value) {
155 }
156 }
157 > snapshot.ts
158 > setup(function () {
159 > const currentTest = this.currentTest;
160 > context = new Lazy(() => new SnapshotContext(currentTest));
161 > });
162 > teardown(async function () {
163 > if (this.currentTest?.state === 'passed') {
164 > await context?.rawValue?.removeOldSnapshots();
165 > }
166 > context = undefined;
167 > });
168 >
169 > /**
170 > * Implements a snapshot testing utility. ⚠️ This is async! ⚠️
171 > *
172 > * The first time a snapshot test is run, it'll record the value it's called
173 > * with as the expected value. Subsequent runs will fail if the value differs,
174 > * but the snapshot can be regenerated by hand or using the Selfhost Test
175 > * Provider Extension which'll offer to update it.
176 > *
177 > * The snapshot will be associated with the currently running test and stored
178 > * in a `__snapshots__` directory next to the test file, which is expected to
179 > * be the first `.test.js` file in the callstack.
180 > */
181 > export function assertSnapshot(value: unknown, options?: ISnapshotOptions): Promise<void> {
182 if (!context) {
183 throw new Error('assertSnapshot can only be used in a test');