|
| 1 | +/* eslint-disable @typescript-eslint/no-require-imports */ |
| 2 | + |
| 3 | +function assertLogs(cb: () => void, expectedMessages: string[]) { |
| 4 | + const errors: Error[] = []; |
| 5 | + // Spying on the console.log function, as the examples don't assert anything themselves |
| 6 | + const originalLog = console.log; |
| 7 | + console.log = (message: string, ...args: unknown[]) => { |
| 8 | + const nextMessage = expectedMessages.shift(); |
| 9 | + const combinedMessage = [message, ...args].map(String).join(" "); |
| 10 | + if (nextMessage !== combinedMessage) { |
| 11 | + errors.push(new Error(`Unexpected log message '${combinedMessage}'`)); |
| 12 | + } |
| 13 | + }; |
| 14 | + try { |
| 15 | + cb(); |
| 16 | + if (expectedMessages.length > 0) { |
| 17 | + errors.push( |
| 18 | + new Error(`Missing expected message(s): ${expectedMessages.join(", ")}`) |
| 19 | + ); |
| 20 | + } |
| 21 | + } finally { |
| 22 | + console.log = originalLog; |
| 23 | + } |
| 24 | + // Throw and first error |
| 25 | + const [firstError] = errors; |
| 26 | + if (firstError) { |
| 27 | + throw firstError; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export const examples: Record<string, Record<string, () => void>> = { |
| 32 | + "1-getting-started": { |
| 33 | + "1_hello_world/napi": () => |
| 34 | + assertLogs( |
| 35 | + () => |
| 36 | + require("../examples/1-getting-started/1_hello_world/napi/hello.js"), |
| 37 | + ["world"] |
| 38 | + ), |
| 39 | + "1_hello_world/node-addon-api": () => |
| 40 | + assertLogs( |
| 41 | + () => |
| 42 | + require("../examples/1-getting-started/1_hello_world/node-addon-api/hello.js"), |
| 43 | + ["world"] |
| 44 | + ), |
| 45 | + "1_hello_world/node-addon-api-addon-class": () => |
| 46 | + assertLogs( |
| 47 | + () => |
| 48 | + require("../examples/1-getting-started/1_hello_world/node-addon-api-addon-class/hello.js"), |
| 49 | + ["world"] |
| 50 | + ), |
| 51 | + "2_function_arguments/napi": () => |
| 52 | + assertLogs( |
| 53 | + () => |
| 54 | + require("../examples/1-getting-started/2_function_arguments/napi/addon.js"), |
| 55 | + ["This should be eight: 8"] |
| 56 | + ), |
| 57 | + "2_function_arguments/node-addon-api": () => |
| 58 | + assertLogs( |
| 59 | + () => |
| 60 | + require("../examples/1-getting-started/2_function_arguments/node-addon-api/addon.js"), |
| 61 | + ["This should be eight: 8"] |
| 62 | + ), |
| 63 | + "3_callbacks/napi": () => |
| 64 | + assertLogs( |
| 65 | + () => |
| 66 | + require("../examples/1-getting-started/3_callbacks/napi/addon.js"), |
| 67 | + ["hello world"] |
| 68 | + ), |
| 69 | + "3_callbacks/node-addon-api": () => |
| 70 | + assertLogs( |
| 71 | + () => |
| 72 | + require("../examples/1-getting-started/3_callbacks/node-addon-api/addon.js"), |
| 73 | + ["hello world"] |
| 74 | + ), |
| 75 | + "4_object_factory/napi": () => |
| 76 | + assertLogs( |
| 77 | + () => |
| 78 | + require("../examples/1-getting-started/4_object_factory/napi/addon.js"), |
| 79 | + ["hello world"] |
| 80 | + ), |
| 81 | + "4_object_factory/node-addon-api": () => |
| 82 | + assertLogs( |
| 83 | + () => |
| 84 | + require("../examples/1-getting-started/4_object_factory/node-addon-api/addon.js"), |
| 85 | + ["hello world"] |
| 86 | + ), |
| 87 | + "5_function_factory": () => |
| 88 | + assertLogs( |
| 89 | + () => |
| 90 | + require("../examples/1-getting-started/5_function_factory/napi/addon.js"), |
| 91 | + ["hello world"] |
| 92 | + ), |
| 93 | + }, |
| 94 | + "5-async-work": { |
| 95 | + // TODO: This crashes (SIGABRT) |
| 96 | + // "async_work_thread_safe_function": () => require("../examples/5-async-work/async_work_thread_safe_function/napi/index.js"), |
| 97 | + }, |
| 98 | +}; |
0 commit comments