Skip to content

Commit b013af7

Browse files
committed
Assert console.log messages
1 parent dc33f3c commit b013af7

File tree

12 files changed

+150
-32
lines changed

12 files changed

+150
-32
lines changed

apps/test-app/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
StatusText,
99
} from "mocha-remote-react-native";
1010

11-
import nodeAddonExamples from "@react-native-node-api/node-addon-examples";
11+
import { examples as nodeAddonExamples } from "@react-native-node-api/node-addon-examples";
1212

1313
function loadTests() {
1414
for (const [suiteName, examples] of Object.entries(nodeAddonExamples)) {

apps/test-app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@react-native/metro-config": "0.79.0",
2222
"@react-native/typescript-config": "0.79.0",
2323
"@rnx-kit/metro-config": "^2.0.1",
24+
"@types/mocha": "^10.0.10",
2425
"@types/react": "^19.0.0",
2526
"concurrently": "^9.1.2",
2627
"ferric-example": "^0.1.0",

apps/test-app/tsconfig.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"extends": "@react-native/typescript-config/tsconfig.json"
2+
"extends": "@react-native/typescript-config/tsconfig.json",
3+
"compilerOptions": {
4+
"types": ["react-native", "mocha"]
5+
},
6+
"files": ["App.tsx", "index.ts"],
7+
"references": [{ "path": "./tsconfig.node-scripts.json" }]
38
}

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/node-addon-examples/index.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/node-addon-examples/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@react-native-node-api/node-addon-examples",
33
"type": "commonjs",
4+
"main": "dist/index.js",
45
"private": true,
56
"homepage": "https://github.com/callstackincubator/react-native-node-api",
67
"repository": {

packages/node-addon-examples/scripts/cmake-projects.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const EXAMPLES_DIR = path.resolve(import.meta.dirname, "../examples");
55
export const TESTS_DIR = path.resolve(import.meta.dirname, "../tests");
66
export const DIRS = [EXAMPLES_DIR, TESTS_DIR];
77

8-
export function findCMakeProjectsRecursively(dir): string[] {
8+
export function findCMakeProjectsRecursively(dir: string): string[] {
99
let results: string[] = [];
1010
const files = readdirSync(dir);
1111

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
tests: {
99+
buffers: () => require("../tests/buffers/addon.js"),
100+
async: () => require("./tests/async/addon.js"),
101+
},
102+
};
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
2-
"extends": "@tsconfig/node22/tsconfig.json",
3-
"compilerOptions": {
4-
"module": "node16",
5-
"moduleResolution": "node16"
6-
},
7-
"include": ["scripts"]
2+
"files": [],
3+
"references": [
4+
{ "path": "./tsconfig.tests.json" },
5+
{ "path": "./tsconfig.node-scripts.json" }
6+
]
87
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "@tsconfig/node22/tsconfig.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"emitDeclarationOnly": true,
6+
"outDir": "dist",
7+
"rootDir": "scripts",
8+
"types": ["node", "read-pkg"]
9+
},
10+
"include": ["scripts/**/*.mts"],
11+
"exclude": []
12+
}

0 commit comments

Comments
 (0)