-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.test.ts
103 lines (85 loc) · 2.8 KB
/
index.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
import { test, expect } from "vitest";
import * as fs from "node:fs";
import * as path from "node:path";
// intentionally a require instead of an import so that vitest doesn't do ESM
// interop transformations on it (which adds otherwise-nonexistent 'default'
// export)
const suchibot = require(".");
test("module exports", () => {
const exportNames = Object.keys(suchibot).filter((key) => suchibot[key]);
expect(exportNames).toMatchInlineSnapshot(`
[
"Mouse",
"Keyboard",
"MouseButton",
"Key",
"MouseEvent",
"KeyboardEvent",
"isKeyboardEvent",
"isMouseEvent",
"sleep",
"sleepSync",
"Screen",
"startListening",
"stopListening",
"eventMatchesFilter",
"keyboardEventFilter",
"mouseEventFilter",
"Tape",
]
`);
});
const readmeText = fs.readFileSync(path.join(__dirname, "README.md"), "utf-8");
test("readme specifies the correct number of exports", () => {
const exportNames = Object.keys(suchibot).filter((key) => suchibot[key]);
const phraseRegExp = /The "suchibot" module has (\d+) named exports/;
const phraseMatches = readmeText.match(phraseRegExp);
if (phraseMatches == null) {
throw new Error(
`Expected readme content to match RegExp ${phraseRegExp}, but it did not`
);
}
const numExports = exportNames.length;
const numExportsInReadme = phraseMatches[1];
expect(numExports).toEqual(parseInt(numExportsInReadme));
});
test("readme mentions all the public methods of all the classes", () => {
const classes = ["Mouse", "Keyboard", "Screen"];
for (const className of classes) {
const klass = suchibot[className];
const staticMethodNames: Array<string> = [];
for (const name in klass) {
const value = klass[name];
if (value === Object.prototype[name]) continue;
if (typeof value === "function" && !name.startsWith("_")) {
staticMethodNames.push(name);
}
}
const instanceMethodNames: Array<string> = [];
for (const name in klass.prototype) {
const value = klass.prototype[name];
if (value === Object.prototype[name]) continue;
if (typeof value === "function" && !name.startsWith("_")) {
instanceMethodNames.push(name);
}
}
for (const name of [...staticMethodNames, ...instanceMethodNames]) {
expect(readmeText).toMatch(new RegExp(`${className}.*${name}`, "m"));
}
}
});
test("readme mentions all the module exports", () => {
const exportNames = Object.keys(suchibot)
.filter((key) => suchibot[key])
.filter((name) => {
if (name === "sleepSync") {
// ignore, because it's deprecated in favor of sleep.sync
return false;
} else {
return true;
}
});
for (const name of exportNames) {
expect(readmeText).toContain(name);
}
});