Path: blob/main/src/vs/base/test/node/snapshot.test.ts
3296 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*--------------------------------------------------------------------------------------------*/45import * as fs from 'fs';6import { tmpdir } from 'os';7import { getRandomTestPath } from './testUtils.js';8import { Promises } from '../../node/pfs.js';9import { SnapshotContext, assertSnapshot } from '../common/snapshot.js';10import { URI } from '../../common/uri.js';11import { join } from '../../common/path.js';12import { assertThrowsAsync, ensureNoDisposablesAreLeakedInTestSuite } from '../common/utils.js';1314// tests for snapshot are in Node so that we can use native FS operations to15// set up and validate things.16//17// Uses snapshots for testing snapshots. It's snapception!1819suite('snapshot', () => {20let testDir: string;2122ensureNoDisposablesAreLeakedInTestSuite();2324setup(function () {25testDir = getRandomTestPath(tmpdir(), 'vsctests', 'snapshot');26return fs.promises.mkdir(testDir, { recursive: true });27});2829teardown(function () {30return Promises.rm(testDir);31});3233const makeContext = (test: Partial<Mocha.Test> | undefined) => {34return new class extends SnapshotContext {35constructor() {36super(test as Mocha.Test);37this.snapshotsDir = URI.file(testDir);38}39};40};4142const snapshotFileTree = async () => {43let str = '';4445const printDir = async (dir: string, indent: number) => {46const children = await Promises.readdir(dir);47for (const child of children) {48const p = join(dir, child);49if ((await fs.promises.stat(p)).isFile()) {50const content = await fs.promises.readFile(p, 'utf-8');51str += `${' '.repeat(indent)}${child}:\n`;52for (const line of content.split('\n')) {53str += `${' '.repeat(indent + 2)}${line}\n`;54}55} else {56str += `${' '.repeat(indent)}${child}/\n`;57await printDir(p, indent + 2);58}59}60};6162await printDir(testDir, 0);63await assertSnapshot(str);64};6566test('creates a snapshot', async () => {67const ctx = makeContext({68file: 'foo/bar',69fullTitle: () => 'hello world!'70});7172await ctx.assert({ cool: true });73await snapshotFileTree();74});7576test('validates a snapshot', async () => {77const ctx1 = makeContext({78file: 'foo/bar',79fullTitle: () => 'hello world!'80});8182await ctx1.assert({ cool: true });8384const ctx2 = makeContext({85file: 'foo/bar',86fullTitle: () => 'hello world!'87});8889// should pass:90await ctx2.assert({ cool: true });9192const ctx3 = makeContext({93file: 'foo/bar',94fullTitle: () => 'hello world!'95});9697// should fail:98await assertThrowsAsync(() => ctx3.assert({ cool: false }));99});100101test('cleans up old snapshots', async () => {102const ctx1 = makeContext({103file: 'foo/bar',104fullTitle: () => 'hello world!'105});106107await ctx1.assert({ cool: true });108await ctx1.assert({ nifty: true });109await ctx1.assert({ customName: 1 }, { name: 'thirdTest', extension: 'txt' });110await ctx1.assert({ customName: 2 }, { name: 'fourthTest' });111112await snapshotFileTree();113114const ctx2 = makeContext({115file: 'foo/bar',116fullTitle: () => 'hello world!'117});118119await ctx2.assert({ cool: true });120await ctx2.assert({ customName: 1 }, { name: 'thirdTest' });121await ctx2.removeOldSnapshots();122123await snapshotFileTree();124});125126test('formats object nicely', async () => {127const circular: any = {};128circular.a = circular;129130await assertSnapshot([1311,132true,133undefined,134null,135123n,136Symbol('heyo'),137'hello',138{ hello: 'world' },139circular,140new Map([['hello', 1], ['goodbye', 2]]),141new Set([1, 2, 3]),142function helloWorld() { },143/hello/g,144new Array(10).fill('long string'.repeat(10)),145{ [Symbol.for('debug.description')]() { return `Range [1 -> 5]`; } },146]);147});148});149150151