-
Notifications
You must be signed in to change notification settings - Fork 30.3k
/
Copy pathsnapshot.test.ts
146 lines (121 loc) · 3.77 KB
/
snapshot.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { tmpdir } from 'os';
import { getRandomTestPath } from 'vs/base/test/node/testUtils';
import { Promises } from 'vs/base/node/pfs';
import { SnapshotContext, assertSnapshot } from 'vs/base/test/common/snapshot';
import { URI } from 'vs/base/common/uri';
import path = require('path');
import { assertThrowsAsync } from 'vs/base/test/common/utils';
// tests for snapshot are in Node so that we can use native FS operations to
// set up and validate things.
//
// Uses snapshots for testing snapshots. It's snapception!
suite('snapshot', () => {
let testDir: string;
setup(function () {
testDir = getRandomTestPath(tmpdir(), 'vsctests', 'snapshot');
return Promises.mkdir(testDir, { recursive: true });
});
teardown(function () {
return Promises.rm(testDir);
});
const makeContext = (test: Partial<Mocha.Test> | undefined) => {
return new class extends SnapshotContext {
constructor() {
super(test as Mocha.Test);
this.snapshotsDir = URI.file(testDir);
}
};
};
const snapshotFileTree = async () => {
let str = '';
const printDir = async (dir: string, indent: number) => {
const children = await Promises.readdir(dir);
for (const child of children) {
const p = path.join(dir, child);
if ((await Promises.stat(p)).isFile()) {
const content = await Promises.readFile(p, 'utf-8');
str += `${' '.repeat(indent)}${child}:\n`;
for (const line of content.split('\n')) {
str += `${' '.repeat(indent + 2)}${line}\n`;
}
} else {
str += `${' '.repeat(indent)}${child}/\n`;
await printDir(p, indent + 2);
}
}
};
await printDir(testDir, 0);
await assertSnapshot(str);
};
test('creates a snapshot', async () => {
const ctx = makeContext({
file: 'foo/bar',
fullTitle: () => 'hello world!'
});
await ctx.assert({ cool: true });
await snapshotFileTree();
});
test('validates a snapshot', async () => {
const ctx1 = makeContext({
file: 'foo/bar',
fullTitle: () => 'hello world!'
});
await ctx1.assert({ cool: true });
const ctx2 = makeContext({
file: 'foo/bar',
fullTitle: () => 'hello world!'
});
// should pass:
await ctx2.assert({ cool: true });
const ctx3 = makeContext({
file: 'foo/bar',
fullTitle: () => 'hello world!'
});
// should fail:
await assertThrowsAsync(() => ctx3.assert({ cool: false }));
});
test('cleans up old snapshots', async () => {
const ctx1 = makeContext({
file: 'foo/bar',
fullTitle: () => 'hello world!'
});
await ctx1.assert({ cool: true });
await ctx1.assert({ nifty: true });
await ctx1.assert({ customName: 1 }, { name: 'thirdTest', extension: 'txt' });
await ctx1.assert({ customName: 2 }, { name: 'fourthTest' });
await snapshotFileTree();
const ctx2 = makeContext({
file: 'foo/bar',
fullTitle: () => 'hello world!'
});
await ctx2.assert({ cool: true });
await ctx2.assert({ customName: 1 }, { name: 'thirdTest' });
await ctx2.removeOldSnapshots();
await snapshotFileTree();
});
test('formats object nicely', async () => {
const circular: any = {};
circular.a = circular;
await assertSnapshot([
1,
true,
undefined,
null,
123n,
Symbol('heyo'),
'hello',
{ hello: 'world' },
circular,
new Map([['hello', 1], ['goodbye', 2]]),
new Set([1, 2, 3]),
function helloWorld() { },
/hello/g,
new Array(10).fill('long string'.repeat(10)),
{ [Symbol.for('debug.description')]() { return `Range [1 -> 5]`; } },
]);
});
});