Skip to content

Commit 2b7644f

Browse files
authored
Assert logs and run test suites conditionally (#143)
* Assert console.log messages * Run tests conditionally * Run all tests on CI * Use require instead of dynamic import
1 parent dc33f3c commit 2b7644f

File tree

15 files changed

+211
-50
lines changed

15 files changed

+211
-50
lines changed

.github/workflows/check.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ jobs:
8181
- run: npm run pod-install
8282
working-directory: apps/test-app
8383
- name: Run tests (iOS)
84-
run: npm run test:ios
84+
run: npm run test:ios:allTests
8585
# TODO: Enable release mode when it works
8686
# run: npm run test:ios -- --mode Release
8787
working-directory: apps/test-app
@@ -156,7 +156,7 @@ jobs:
156156
adb logcat > emulator-logcat.txt 2>&1 &
157157
LOGCAT_PID=$!
158158
# Build, install and run the app
159-
npm run test:android -- --mode Release
159+
npm run test:android:allTests -- --mode Release
160160
# Wait a bit for the sub-process to terminate, before terminating the emulator
161161
sleep 5
162162
# Stop logcat

apps/test-app/App.tsx

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,47 @@ import {
88
StatusText,
99
} from "mocha-remote-react-native";
1010

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

13-
function loadTests() {
14-
for (const [suiteName, examples] of Object.entries(nodeAddonExamples)) {
15-
describe(suiteName, () => {
16-
for (const [exampleName, requireExample] of Object.entries(examples)) {
17-
it(exampleName, async () => {
18-
const test = requireExample();
19-
if (test instanceof Function) {
20-
await test();
21-
}
22-
});
23-
}
24-
});
25-
}
13+
function describeIf(
14+
condition: boolean,
15+
title: string,
16+
fn: (this: Mocha.Suite) => void
17+
) {
18+
return condition ? describe(title, fn) : describe.skip(title, fn);
19+
}
20+
21+
type Context = {
22+
allTests?: boolean;
23+
nodeAddonExamples?: boolean;
24+
ferricExample?: boolean;
25+
};
26+
27+
function loadTests({
28+
allTests = false,
29+
nodeAddonExamples = allTests,
30+
ferricExample = allTests,
31+
}: Context) {
32+
describeIf(nodeAddonExamples, "Node Addon Examples", () => {
33+
for (const [suiteName, examples] of Object.entries(
34+
nodeAddonExamplesSuites
35+
)) {
36+
describe(suiteName, () => {
37+
for (const [exampleName, requireExample] of Object.entries(examples)) {
38+
it(exampleName, async () => {
39+
const test = requireExample();
40+
if (test instanceof Function) {
41+
await test();
42+
}
43+
});
44+
}
45+
});
46+
}
47+
});
2648

27-
describe("ferric-example", () => {
49+
describeIf(ferricExample, "ferric-example", () => {
2850
it("exports a callable sum function", () => {
29-
// eslint-disable-next-line @typescript-eslint/no-require-imports
51+
/* eslint-disable-next-line @typescript-eslint/no-require-imports -- TODO: Determine why a dynamic import doesn't work on Android */
3052
const exampleAddon = require("ferric-example");
3153
const result = exampleAddon.sum(1, 3);
3254
if (result !== 4) {

apps/test-app/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
"ios": "react-native run-ios --no-packager",
99
"pod-install": "cd ios && pod install",
1010
"test:android": "mocha-remote --exit-on-error -- concurrently --kill-others-on-fail --passthrough-arguments npm:metro 'npm:android -- {@}' --",
11-
"test:ios": "mocha-remote --exit-on-error -- concurrently --passthrough-arguments --kill-others-on-fail npm:metro 'npm:ios -- {@}' --"
11+
"test:android:allTests": "MOCHA_REMOTE_CONTEXT=allTests npm run test:android -- ",
12+
"test:android:nodeAddonExamples": "MOCHA_REMOTE_CONTEXT=nodeAddonExamples npm run test:android -- ",
13+
"test:android:ferricExample": "MOCHA_REMOTE_CONTEXT=ferricExample npm run test:android -- ",
14+
"test:ios": "mocha-remote --exit-on-error -- concurrently --passthrough-arguments --kill-others-on-fail npm:metro 'npm:ios -- {@}' --",
15+
"test:ios:allTests": "MOCHA_REMOTE_CONTEXT=allTests npm run test:ios -- ",
16+
"test:ios:nodeAddonExamples": "MOCHA_REMOTE_CONTEXT=nodeAddonExamples npm run test:ios -- ",
17+
"test:ios:ferricExample": "MOCHA_REMOTE_CONTEXT=ferricExample npm run test:ios -- "
1218
},
1319
"dependencies": {
1420
"@babel/core": "^7.26.10",
@@ -21,6 +27,7 @@
2127
"@react-native/metro-config": "0.79.0",
2228
"@react-native/typescript-config": "0.79.0",
2329
"@rnx-kit/metro-config": "^2.0.1",
30+
"@types/mocha": "^10.0.10",
2431
"@types/react": "^19.0.0",
2532
"concurrently": "^9.1.2",
2633
"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
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "@tsconfig/node22/tsconfig.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"emitDeclarationOnly": true,
6+
"outDir": "dist",
7+
"rootDir": "scripts",
8+
"types": ["node"]
9+
},
10+
"include": ["scripts/**/*.ts"]
11+
}

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/ferric-example/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/ferric-example",
33
"private": true,
4+
"type": "commonjs",
45
"version": "0.1.0",
56
"homepage": "https://github.com/callstackincubator/react-native-node-api",
67
"repository": {

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

0 commit comments

Comments
 (0)