Skip to content

Commit a52e073

Browse files
committed
Better type inference
1 parent dd218e3 commit a52e073

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

script/test-svg.spec.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ import { JSDOM } from "jsdom";
66
// Collect into an array so we can iterate over it again multiple times.
77
const svgFiles = await Array.fromAsync(new Glob("./src/svg/*/*.svg").scan());
88

9+
/**
10+
* Helper so TypeScript can infer `expect(...).not.toBeNull()`.
11+
*/
12+
function expectNotNull<T>(value: T | null, message: string): asserts value is T {
13+
expect(value, message).not.toBeNull();
14+
}
15+
916
test.concurrent("SVG files are all 500×500", async () => {
1017
let numSVGs = 0;
1118
for await (const svgFile of svgFiles) {
1219
numSVGs++;
1320
const svgElem = new JSDOM(
1421
await file(svgFile).text(),
1522
).window.document.querySelector("svg");
16-
expect(svgElem, `${svgFile}: no svg`).not.toBeNull();
23+
expectNotNull(svgElem, `${svgFile}: no svg`);
1724
expect(svgElem?.getAttribute("width"), `${svgFile}: wrong width`).toEqual("500");
1825
expect(svgElem?.getAttribute("height"), `${svgFile}: wrong height`).toEqual("500");
1926
expect(svgElem?.getAttribute("viewBox"), `${svgFile}: wrong viewBox`).toEqual("0 0 500 500");
@@ -68,10 +75,8 @@ test.concurrent("SVGs have no hardcoded colors", async () => {
6875
await file(svgFile).text(),
6976
).window.document.querySelector("svg");
7077

71-
expect(svgElem, `${svgFile}: no svg`).not.toBeNull();
72-
if (svgElem !== null) {
73-
checkElement(svgElem);
74-
}
78+
expectNotNull(svgElem, `${svgFile}: no svg`);
79+
checkElement(svgElem);
7580
}
7681
});
7782

@@ -81,10 +86,10 @@ test.concurrent("SVGs are well-formed with no extraneous attributes", async () =
8186
await file(svgFile).text(),
8287
).window.document.querySelector("svg");
8388

84-
expect(svgElem, `${svgFile}: no svg`).not.toBeNull();
85-
expect(svgElem?.getAttribute("xmlns"), `${svgFile}: bad xmlns`)
89+
expectNotNull(svgElem, `${svgFile}: no svg`);
90+
expect(svgElem.getAttribute("xmlns"), `${svgFile}: bad xmlns`)
8691
.toBe("http://www.w3.org/2000/svg");
87-
expect(svgElem?.getAttributeNames().sort(), `${svgFile}: wrong attributes`)
92+
expect(svgElem.getAttributeNames().sort(), `${svgFile}: wrong attributes`)
8893
.toEqual(["width", "height", "viewBox", "xmlns"].sort());
8994
}
9095
});
@@ -105,9 +110,7 @@ test.concurrent("SVGs only have allowed elements", async () => {
105110
await file(svgFile).text(),
106111
).window.document.querySelector("svg");
107112

108-
expect(svgElem, `${svgFile}: no svg`).not.toBeNull();
109-
if (svgElem !== null) {
110-
checkElement(svgElem);
111-
}
113+
expectNotNull(svgElem, `${svgFile}: no svg`);
114+
checkElement(svgElem);
112115
}
113116
});

0 commit comments

Comments
 (0)