-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.ts
More file actions
105 lines (103 loc) · 3.22 KB
/
index.ts
File metadata and controls
105 lines (103 loc) · 3.22 KB
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
/* eslint-disable @typescript-eslint/no-require-imports */
function assertLogs(cb: () => void, expectedMessages: string[]) {
const errors: Error[] = [];
// Spying on the console.log function, as the examples don't assert anything themselves
const originalLog = console.log;
console.log = (message: string, ...args: unknown[]) => {
const nextMessage = expectedMessages.shift();
const combinedMessage = [message, ...args].map(String).join(" ");
if (nextMessage !== combinedMessage) {
errors.push(new Error(`Unexpected log message '${combinedMessage}'`));
}
};
try {
cb();
if (expectedMessages.length > 0) {
errors.push(
new Error(`Missing expected message(s): ${expectedMessages.join(", ")}`)
);
}
} finally {
console.log = originalLog;
}
// Throw and first error
const [firstError] = errors;
if (firstError) {
throw firstError;
}
}
export const suites: Record<
string,
Record<string, () => void | (() => void)>
> = {
"1-getting-started": {
"1_hello_world/napi": () =>
assertLogs(
() =>
require("../examples/1-getting-started/1_hello_world/napi/hello.js"),
["world"]
),
"1_hello_world/node-addon-api": () =>
assertLogs(
() =>
require("../examples/1-getting-started/1_hello_world/node-addon-api/hello.js"),
["world"]
),
"1_hello_world/node-addon-api-addon-class": () =>
assertLogs(
() =>
require("../examples/1-getting-started/1_hello_world/node-addon-api-addon-class/hello.js"),
["world"]
),
"2_function_arguments/napi": () =>
assertLogs(
() =>
require("../examples/1-getting-started/2_function_arguments/napi/addon.js"),
["This should be eight: 8"]
),
"2_function_arguments/node-addon-api": () =>
assertLogs(
() =>
require("../examples/1-getting-started/2_function_arguments/node-addon-api/addon.js"),
["This should be eight: 8"]
),
"3_callbacks/napi": () =>
assertLogs(
() =>
require("../examples/1-getting-started/3_callbacks/napi/addon.js"),
["hello world"]
),
"3_callbacks/node-addon-api": () =>
assertLogs(
() =>
require("../examples/1-getting-started/3_callbacks/node-addon-api/addon.js"),
["hello world"]
),
"4_object_factory/napi": () =>
assertLogs(
() =>
require("../examples/1-getting-started/4_object_factory/napi/addon.js"),
["hello world"]
),
"4_object_factory/node-addon-api": () =>
assertLogs(
() =>
require("../examples/1-getting-started/4_object_factory/node-addon-api/addon.js"),
["hello world"]
),
"5_function_factory": () =>
assertLogs(
() =>
require("../examples/1-getting-started/5_function_factory/napi/addon.js"),
["hello world"]
),
},
"5-async-work": {
// TODO: This crashes (SIGABRT)
// "async_work_thread_safe_function": () => require("../examples/5-async-work/async_work_thread_safe_function/napi/index.js"),
},
tests: {
buffers: () => require("../tests/buffers/addon.js"),
async: () => require("../tests/async/addon.js"),
},
};