|
1 | | -/* eslint-disable @typescript-eslint/no-explicit-any */ |
2 | 1 | import assert from "node:assert/strict"; |
3 | | -import { describe, it } from "node:test"; |
| 2 | +import { describe, it, mock as themock } from "node:test"; |
4 | 3 | import path from "node:path"; |
5 | 4 | import fs from "node:fs"; |
6 | 5 |
|
@@ -86,16 +85,11 @@ describe("isNodeApiModule", () => { |
86 | 85 | // only android module is unreadable |
87 | 86 | fs.chmodSync(unreadable, 0); |
88 | 87 | 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); |
99 | 93 | }); |
100 | 94 | }); |
101 | 95 |
|
@@ -313,25 +307,19 @@ describe("determineModuleContext", () => { |
313 | 307 | "subdir1/file1.xcframework": "" |
314 | 308 | }); |
315 | 309 | 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")) { |
321 | 314 | readCount++; |
322 | 315 | } |
323 | | - return origSync(pathArg, encoding); |
324 | | - }; |
| 316 | + return orig(...args); |
| 317 | + }); |
325 | 318 |
|
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); |
336 | 324 | }); |
337 | 325 | }); |
0 commit comments