Skip to content

Commit 2f5770e

Browse files
committed
Run tests conditionally
1 parent b013af7 commit 2f5770e

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

apps/test-app/App.tsx

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,47 @@ 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, 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", () => {
28-
it("exports a callable sum function", () => {
29-
// eslint-disable-next-line @typescript-eslint/no-require-imports
30-
const exampleAddon = require("ferric-example");
49+
describeIf(ferricExample, "ferric-example", () => {
50+
it("exports a callable sum function", async () => {
51+
const exampleAddon = await import("ferric-example");
3152
const result = exampleAddon.sum(1, 3);
3253
if (result !== 4) {
3354
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ function assertLogs(cb: () => void, expectedMessages: string[]) {
2828
}
2929
}
3030

31-
export const examples: Record<string, Record<string, () => void>> = {
31+
export const suites: Record<
32+
string,
33+
Record<string, () => void | (() => void)>
34+
> = {
3235
"1-getting-started": {
3336
"1_hello_world/napi": () =>
3437
assertLogs(
@@ -97,6 +100,6 @@ export const examples: Record<string, Record<string, () => void>> = {
97100
},
98101
tests: {
99102
buffers: () => require("../tests/buffers/addon.js"),
100-
async: () => require("./tests/async/addon.js"),
103+
async: () => require("../tests/async/addon.js"),
101104
},
102105
};

0 commit comments

Comments
 (0)