Skip to content

Assert logs and run test suites conditionally #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
- run: npm run pod-install
working-directory: apps/test-app
- name: Run tests (iOS)
run: npm run test:ios
run: npm run test:ios:allTests
# TODO: Enable release mode when it works
# run: npm run test:ios -- --mode Release
working-directory: apps/test-app
Expand Down Expand Up @@ -156,7 +156,7 @@ jobs:
adb logcat > emulator-logcat.txt 2>&1 &
LOGCAT_PID=$!
# Build, install and run the app
npm run test:android -- --mode Release
npm run test:android:allTests -- --mode Release
# Wait a bit for the sub-process to terminate, before terminating the emulator
sleep 5
# Stop logcat
Expand Down
54 changes: 38 additions & 16 deletions apps/test-app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,47 @@ import {
StatusText,
} from "mocha-remote-react-native";

import nodeAddonExamples from "@react-native-node-api/node-addon-examples";
import { suites as nodeAddonExamplesSuites } from "@react-native-node-api/node-addon-examples";

function loadTests() {
for (const [suiteName, examples] of Object.entries(nodeAddonExamples)) {
describe(suiteName, () => {
for (const [exampleName, requireExample] of Object.entries(examples)) {
it(exampleName, async () => {
const test = requireExample();
if (test instanceof Function) {
await test();
}
});
}
});
}
function describeIf(
condition: boolean,
title: string,
fn: (this: Mocha.Suite) => void
) {
return condition ? describe(title, fn) : describe.skip(title, fn);
}

type Context = {
allTests?: boolean;
nodeAddonExamples?: boolean;
ferricExample?: boolean;
};

function loadTests({
allTests = false,
nodeAddonExamples = allTests,
ferricExample = allTests,
Comment on lines +28 to +30
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So by default, both nodeAddonExamples and ferricExample resolve to false and so both the "Node Addon Examples" and "ferric-example" tests get skipped? Is that the intention?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might still be debating this with myself 😊

On one hand it might be confusing to skip so many tests by default and on the other it does leave a potential to cut down on the time pre-building while bootstrapping and complexity (having to have the Rust toolchain installed) when someone wants to contribute to the project.

Looking back at this (I wrote it some weeks back), the optimization does seem a bit premature.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my (though not strongly held)) opinion we could either enable all tests by default or skip only ferric-example since it requires additional Rust setup.

}: Context) {
describeIf(nodeAddonExamples, "Node Addon Examples", () => {
for (const [suiteName, examples] of Object.entries(
nodeAddonExamplesSuites
)) {
describe(suiteName, () => {
for (const [exampleName, requireExample] of Object.entries(examples)) {
it(exampleName, async () => {
const test = requireExample();
if (test instanceof Function) {
await test();
}
});
}
});
}
});

describe("ferric-example", () => {
describeIf(ferricExample, "ferric-example", () => {
it("exports a callable sum function", () => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
/* eslint-disable-next-line @typescript-eslint/no-require-imports -- TODO: Determine why a dynamic import doesn't work on Android */
const exampleAddon = require("ferric-example");
const result = exampleAddon.sum(1, 3);
if (result !== 4) {
Expand Down
9 changes: 8 additions & 1 deletion apps/test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
"ios": "react-native run-ios --no-packager",
"pod-install": "cd ios && pod install",
"test:android": "mocha-remote --exit-on-error -- concurrently --kill-others-on-fail --passthrough-arguments npm:metro 'npm:android -- {@}' --",
"test:ios": "mocha-remote --exit-on-error -- concurrently --passthrough-arguments --kill-others-on-fail npm:metro 'npm:ios -- {@}' --"
"test:android:allTests": "MOCHA_REMOTE_CONTEXT=allTests npm run test:android -- ",
"test:android:nodeAddonExamples": "MOCHA_REMOTE_CONTEXT=nodeAddonExamples npm run test:android -- ",
"test:android:ferricExample": "MOCHA_REMOTE_CONTEXT=ferricExample npm run test:android -- ",
"test:ios": "mocha-remote --exit-on-error -- concurrently --passthrough-arguments --kill-others-on-fail npm:metro 'npm:ios -- {@}' --",
"test:ios:allTests": "MOCHA_REMOTE_CONTEXT=allTests npm run test:ios -- ",
"test:ios:nodeAddonExamples": "MOCHA_REMOTE_CONTEXT=nodeAddonExamples npm run test:ios -- ",
"test:ios:ferricExample": "MOCHA_REMOTE_CONTEXT=ferricExample npm run test:ios -- "
},
"dependencies": {
"@babel/core": "^7.26.10",
Expand All @@ -21,6 +27,7 @@
"@react-native/metro-config": "0.79.0",
"@react-native/typescript-config": "0.79.0",
"@rnx-kit/metro-config": "^2.0.1",
"@types/mocha": "^10.0.10",
"@types/react": "^19.0.0",
"concurrently": "^9.1.2",
"ferric-example": "^0.1.0",
Expand Down
7 changes: 6 additions & 1 deletion apps/test-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"extends": "@react-native/typescript-config/tsconfig.json"
"extends": "@react-native/typescript-config/tsconfig.json",
"compilerOptions": {
"types": ["react-native", "mocha"]
},
"files": ["App.tsx", "index.ts"],
"references": [{ "path": "./tsconfig.node-scripts.json" }]
}
11 changes: 11 additions & 0 deletions apps/test-app/tsconfig.node-scripts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@tsconfig/node22/tsconfig.json",
"compilerOptions": {
"composite": true,
"emitDeclarationOnly": true,
"outDir": "dist",
"rootDir": "scripts",
"types": ["node"]
},
"include": ["scripts/**/*.ts"]
}
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/ferric-example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@react-native-node-api/ferric-example",
"private": true,
"type": "commonjs",
"version": "0.1.0",
"homepage": "https://github.com/callstackincubator/react-native-node-api",
"repository": {
Expand Down
22 changes: 0 additions & 22 deletions packages/node-addon-examples/index.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/node-addon-examples/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@react-native-node-api/node-addon-examples",
"type": "commonjs",
"main": "dist/index.js",
"private": true,
"homepage": "https://github.com/callstackincubator/react-native-node-api",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/node-addon-examples/scripts/cmake-projects.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const EXAMPLES_DIR = path.resolve(import.meta.dirname, "../examples");
export const TESTS_DIR = path.resolve(import.meta.dirname, "../tests");
export const DIRS = [EXAMPLES_DIR, TESTS_DIR];

export function findCMakeProjectsRecursively(dir): string[] {
export function findCMakeProjectsRecursively(dir: string): string[] {
let results: string[] = [];
const files = readdirSync(dir);

Expand Down
105 changes: 105 additions & 0 deletions packages/node-addon-examples/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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"),
},
};
11 changes: 5 additions & 6 deletions packages/node-addon-examples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "@tsconfig/node22/tsconfig.json",
"compilerOptions": {
"module": "node16",
"moduleResolution": "node16"
},
"include": ["scripts"]
"files": [],
"references": [
{ "path": "./tsconfig.tests.json" },
{ "path": "./tsconfig.node-scripts.json" }
]
}
12 changes: 12 additions & 0 deletions packages/node-addon-examples/tsconfig.node-scripts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@tsconfig/node22/tsconfig.json",
"compilerOptions": {
"composite": true,
"emitDeclarationOnly": true,
"outDir": "dist",
"rootDir": "scripts",
"types": ["node", "read-pkg"]
},
"include": ["scripts/**/*.mts"],
"exclude": []
}
12 changes: 12 additions & 0 deletions packages/node-addon-examples/tsconfig.tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@tsconfig/react-native",
"compilerOptions": {
"composite": true,
"noEmit": false,
"module": "commonjs",
"outDir": "dist",
"rootDir": "src",
"types": ["react-native"]
},
"include": ["src/*.ts"]
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{ "path": "./packages/host/tsconfig.json" },
{ "path": "./packages/gyp-to-cmake/tsconfig.json" },
{ "path": "./packages/cmake-rn/tsconfig.json" },
{ "path": "./packages/ferric/tsconfig.json" }
{ "path": "./packages/ferric/tsconfig.json" },
{ "path": "./packages/node-addon-examples/tsconfig.json" }
]
}
Loading