-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsnapshots.test.ts
123 lines (109 loc) · 3.75 KB
/
snapshots.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
import assert from "node:assert";
import { spawnSync } from "node:child_process";
import { access, readFile, writeFile } from "node:fs/promises";
import { after, describe, test } from "node:test";
const attw = new URL("../src/index.ts", import.meta.url).pathname;
const updateSnapshots = process.env.UPDATE_SNAPSHOTS || process.env.U;
const testFilter = (process.env.TEST_FILTER || process.env.T)?.toLowerCase();
const tests = [
["@apollo__client-3.7.16.tgz"],
["@[email protected]"],
["@[email protected]"],
["@[email protected]"],
["[email protected]"],
["[email protected]"],
["[email protected]"],
["[email protected]", "-f table"],
["[email protected]"],
["[email protected]"],
["[email protected]", "-f ascii"],
["[email protected]"],
["[email protected]"],
["[email protected]"],
["[email protected]"],
["[email protected]"],
["[email protected]"],
["[email protected]"],
["[email protected]", "--entrypoints vue"],
["[email protected]", "--entrypoints . jsx-runtime"],
["[email protected]", "--exclude-entrypoints macros -f ascii"],
["[email protected]", "--include-entrypoints ./foo -f ascii"],
[
`--definitely-typed ${new URL("../../core/test/fixtures/@[email protected]", import.meta.url).pathname}`,
],
[
`--definitely-typed ${new URL("../../core/test/fixtures/@[email protected]", import.meta.url).pathname}`,
],
["[email protected]", "--entrypoints-legacy --ignore-rules=cjs-only-exports-default"],
];
const defaultOpts = "-f table-flipped";
describe("snapshots", async () => {
const snapshotsWritten: URL[] = [];
after(() => {
if (updateSnapshots && snapshotsWritten.length > 0) {
console.log(`Updated ${snapshotsWritten.length} snapshots:`);
} else if (snapshotsWritten.length > 0) {
console.log(`Wrote ${snapshotsWritten.length} snapshots:`);
}
if (snapshotsWritten.length > 0) {
console.log(snapshotsWritten.map((url) => ` ${url.pathname}`).join("\n"));
}
});
for (const [tarball, options] of tests) {
const fixture = tarball + (options ? ` ${stripPaths(options)}` : "");
if (testFilter && !fixture.toLowerCase().includes(testFilter)) {
continue;
}
test(fixture, async () => {
const tarballPath = new URL(`../../core/test/fixtures/${tarball}`, import.meta.url).pathname;
const {
stdout,
stderr,
status = 1,
} = spawnSync(
process.execPath,
[...process.execArgv, attw, tarballPath, ...(options ?? defaultOpts).split(" ")],
{
encoding: "utf8",
stdio: "pipe",
env: { ...process.env, FORCE_COLOR: "0", NODE_OPTIONS: "--no-warnings" },
},
);
const snapshotURL = new URL(`snapshots/${fixture}.md`, import.meta.url);
// prettier-ignore
const expectedSnapshot = [
`# ${fixture}`,
"",
"```",
`$ attw ${tarball} ${stripPaths(options ?? defaultOpts)}`,
"",
[stdout, stderr].filter(Boolean).join("\n"),
"",
"```",
"",
`Exit code: ${status}`,
].join("\n");
if (
await access(snapshotURL)
.then(() => true)
.catch(() => false)
) {
const snapshot = await readFile(snapshotURL, "utf8");
if (updateSnapshots) {
await writeFile(snapshotURL, expectedSnapshot);
snapshotsWritten.push(snapshotURL);
} else {
assert.strictEqual(snapshot, expectedSnapshot);
}
} else {
await writeFile(snapshotURL, expectedSnapshot);
snapshotsWritten.push(snapshotURL);
}
});
}
});
function stripPaths(input: string): string {
return input.replace(/ .*[\\\/](?=[^\\\/]*$)/g, " ");
}