/
test.mjs
179 lines (152 loc) · 4.76 KB
/
test.mjs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// @ts-check
import { doesNotReject, rejects, strictEqual } from "node:assert";
import { execFile } from "node:child_process";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { promisify } from "node:util";
import disposableDirectory from "disposable-directory";
import TestDirector from "test-director";
import assertSnapshot from "./assertSnapshot.mjs";
const execFilePromise = promisify(execFile);
const tests = new TestDirector();
tests.add(
"`assertSnapshot` with argument 1 `actualValue` not a string.",
async () => {
await rejects(
assertSnapshot(
// @ts-ignore Testing invalid.
true,
"snapshot.txt"
),
new TypeError("Argument 1 `actualValue` must be a string.")
);
}
);
tests.add(
"`assertSnapshot` with argument 2 `snapshotFile` not a string or `URL` instance.",
async () => {
await rejects(
assertSnapshot(
"a",
// @ts-ignore Testing invalid.
true
),
new TypeError(
"Argument 2 `snapshotFile` must be a string or `URL` instance."
)
);
}
);
tests.add(
"`assertSnapshot` with argument 3 `assertion` not a function.",
async () => {
await rejects(
assertSnapshot(
"a",
"snapshot.txt",
// @ts-ignore Testing invalid.
true
),
new TypeError("Argument 3 `assertion` must be a function.")
);
}
);
tests.add(
"`assertSnapshot` with snapshot file URL, invalid scheme.",
async () => {
await rejects(assertSnapshot("a", new URL("http://localhost")), TypeError);
}
);
tests.add(
"`assertSnapshot` with snapshot file URL, assertion default, snapshot present.",
async () => {
await disposableDirectory(async (tempDirPath) => {
const snapshotFileUrl = pathToFileURL(join(tempDirPath, "snapshot.txt"));
await writeFile(snapshotFileUrl, "a");
await doesNotReject(assertSnapshot("a", snapshotFileUrl));
await rejects(assertSnapshot("b", snapshotFileUrl), {
code: "ERR_ASSERTION",
});
});
}
);
tests.add(
"`assertSnapshot` with snapshot file path, assertion default, snapshot file missing, `SAVE_SNAPSHOTS` env var missing.",
async () => {
await disposableDirectory(async (tempDirPath) => {
const snapshotFilePath = join(tempDirPath, "snapshot.txt");
await rejects(
assertSnapshot("a", snapshotFilePath),
new Error(
`Use the environment variable \`SAVE_SNAPSHOTS=1\` to create missing snapshot \`${snapshotFilePath}\`.`
)
);
});
}
);
tests.add(
"`assertSnapshot` with snapshot file path, assertion default, snapshot file missing, `SAVE_SNAPSHOTS` env var present.",
async () => {
await disposableDirectory(async (tempDirPath) => {
const snapshotFilePath = join(tempDirPath, "snapshot.txt");
const testPath = join(tempDirPath, "test.mjs");
const snapshotAssertionPath = fileURLToPath(
new URL("./assertSnapshot.mjs", import.meta.url)
);
await Promise.all([
writeFile(snapshotFilePath, "a"),
writeFile(
testPath,
`import assertSnapshot from "${snapshotAssertionPath}";
assertSnapshot("b", "${snapshotFilePath}");
`
),
]);
await execFilePromise("node", [testPath], {
env: {
...process.env,
SAVE_SNAPSHOTS: "1",
},
});
strictEqual(await readFile(snapshotFilePath, "utf8"), "b");
});
}
);
tests.add(
"`assertSnapshot` with snapshot file path, assertion default, snapshot present.",
async () => {
await disposableDirectory(async (tempDirPath) => {
const snapshotFilePath = join(tempDirPath, "snapshot.txt");
await writeFile(snapshotFilePath, "a");
await doesNotReject(assertSnapshot("a", snapshotFilePath));
await rejects(assertSnapshot("b", snapshotFilePath), {
code: "ERR_ASSERTION",
});
});
}
);
tests.add(
"`assertSnapshot` with snapshot file path, assertion custom, snapshot present.",
async () => {
await disposableDirectory(async (tempDirPath) => {
const snapshotFilePath = join(tempDirPath, "snapshot.json");
await writeFile(snapshotFilePath, "a");
const errorMessage = "Different values.";
/**
* Asserts one value is another.
* @param {unknown} actual Actual value.
* @param {unknown} expected Expected value.
*/
function assertIs(actual, expected) {
if (!Object.is(actual, expected)) throw new Error(errorMessage);
}
await doesNotReject(assertSnapshot("a", snapshotFilePath, assertIs));
await rejects(
assertSnapshot("b", snapshotFilePath, assertIs),
new Error(errorMessage)
);
});
}
);
tests.run();