Skip to content

Commit ba3659c

Browse files
committed
use node:test mock to clean up manual mock boilerplate a bit
1 parent 97f8c85 commit ba3659c

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

packages/react-native-node-api-modules/src/node/path-utils.test.ts

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
21
import assert from "node:assert/strict";
3-
import { describe, it } from "node:test";
2+
import { describe, it, mock as themock } from "node:test";
43
import path from "node:path";
54
import fs from "node:fs";
65

@@ -86,16 +85,11 @@ describe("isNodeApiModule", () => {
8685
// only android module is unreadable
8786
fs.chmodSync(unreadable, 0);
8887
const warnings: string[] = [];
89-
const origWarn = console.warn;
90-
console.warn = (msg: string) => warnings.push(msg);
91-
try {
92-
const result = isNodeApiModule(path.join(tempDirectoryPath, "addon"));
93-
assert.equal(result, true);
94-
assert.deepEqual(warnings, ["skipping unreadable module addon.android.node"]);
95-
} finally {
96-
console.warn = origWarn;
97-
fs.chmodSync(unreadable, 0o600);
98-
}
88+
themock.method(console, 'warn', (msg: string) => warnings.push(msg));
89+
const result = isNodeApiModule(path.join(tempDirectoryPath, "addon"));
90+
assert.equal(result, true);
91+
assert.deepEqual(warnings, ["skipping unreadable module addon.android.node"]);
92+
fs.chmodSync(unreadable, 0o600);
9993
});
10094
});
10195

@@ -313,25 +307,19 @@ describe("determineModuleContext", () => {
313307
"subdir1/file1.xcframework": ""
314308
});
315309
let readCount = 0;
316-
// inject mock fs.readFileSync to count number of package.json file reads
317-
const fsa: any = fs;
318-
const origSync = fsa.readFileSync;
319-
fsa.readFileSync = (pathArg: string, encoding?: string): any => {
320-
if (typeof pathArg === 'string' && pathArg.endsWith("package.json")) {
310+
const orig = fs.readFileSync;
311+
themock.method(fs, "readFileSync", (...args: Parameters<typeof fs.readFileSync>) => {
312+
const [pathArg] = args;
313+
if (typeof pathArg === "string" && pathArg.endsWith("package.json")) {
321314
readCount++;
322315
}
323-
return origSync(pathArg, encoding);
324-
};
316+
return orig(...args);
317+
});
325318

326-
try {
327-
const ctx1 = determineModuleContext(path.join(tempDir, "subdir1/file1.node"));
328-
const ctx2 = determineModuleContext(path.join(tempDir, "subdir2/file2.node"));
329-
assert.equal(ctx1.packageName, "cached-pkg");
330-
assert.equal(ctx2.packageName, "cached-pkg");
331-
assert.equal(readCount, 1);
332-
} finally {
333-
// restore original method
334-
(fs as any).readFileSync = origSync;
335-
}
319+
const ctx1 = determineModuleContext(path.join(tempDir, "subdir1/file1.node"));
320+
const ctx2 = determineModuleContext(path.join(tempDir, "subdir2/file2.node"));
321+
assert.equal(ctx1.packageName, "cached-pkg");
322+
assert.equal(ctx2.packageName, "cached-pkg");
323+
assert.equal(readCount, 1);
336324
});
337325
});

packages/react-native-node-api-modules/src/node/path-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function determineModuleContext(
106106
if (fs.existsSync(pkgJsonPath)) {
107107
// Resolve real path of package directory for caching
108108
let pkgDir = currentPath;
109-
try { pkgDir = fs.realpathSync(currentPath); } catch {}
109+
try { pkgDir = fs.realpathSync(currentPath); } catch { /* ignore realpath errors */ }
110110
let pkgName: string;
111111
if (packageNameCache.has(pkgDir)) {
112112
pkgName = packageNameCache.get(pkgDir)!;

0 commit comments

Comments
 (0)