Skip to content

Commit c1e1221

Browse files
committed
Run tests conditionally
1 parent faefcf1 commit c1e1221

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-17
lines changed

apps/test-app/App.tsx

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,44 @@ import {
88
StatusText,
99
} from "mocha-remote-react-native";
1010

11-
import { examples as 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, () => {
18-
requireExample();
19-
});
20-
}
21-
});
22-
}
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, () => {
39+
requireExample();
40+
});
41+
}
42+
});
43+
}
44+
});
2345

24-
describe("ferric-example", () => {
25-
it("exports a callable sum function", () => {
26-
// eslint-disable-next-line @typescript-eslint/no-require-imports
27-
const exampleAddon = require("ferric-example");
46+
describeIf(ferricExample, "ferric-example", () => {
47+
it("exports a callable sum function", async () => {
48+
const exampleAddon = await import("ferric-example");
2849
const result = exampleAddon.sum(1, 3);
2950
if (result !== 4) {
3051
throw new Error(`Expected 1 + 3 to equal 4, but got ${result}`);

apps/test-app/package.json

Lines changed: 7 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",
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+
}

packages/node-addon-examples/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function assertLogs(cb: () => void, expectedMessages: string[]) {
2828
}
2929
}
3030

31-
export const examples: Record<string, Record<string, () => void>> = {
31+
export const suites: Record<string, Record<string, () => void>> = {
3232
"1-getting-started": {
3333
"1_hello_world/napi": () =>
3434
assertLogs(

0 commit comments

Comments
 (0)