Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6c3105a

Browse files
committedNov 22, 2021
feat: add test for onLine throw error
1 parent bd09533 commit 6c3105a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
 

‎test/unit/node/util.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,33 @@ describe("onLine", () => {
447447

448448
expect(await received).toEqual(expected)
449449
})
450+
451+
describe("used with a process missing stdout ", () => {
452+
it("should throw an error", async () => {
453+
// Initialize a process that does not have stdout.
454+
// "If the child was spawned with stdio set to anything
455+
// other than 'pipe', then subprocess.stdout will be null."
456+
// Source: https://stackoverflow.com/a/46024006/3015595
457+
// Other source: https://nodejs.org/api/child_process.html#child_process_subprocess_stdout
458+
// NOTE@jsjoeio - I'm not sure if this actually happens though
459+
// which is why I have to set proc.stdout = null
460+
// a couple lines below.
461+
const proc = cp.spawn("node", [], {
462+
stdio: [null],
463+
})
464+
const mockCallback = jest.fn()
465+
466+
// @ts-expect-error TypeScript knows proc.stdout should return a Readable
467+
// We overwrite it so that we can get testing coverage for the scenario
468+
// in onLine that would throw an error.
469+
proc.stdout = null
470+
471+
expect(() => util.onLine(proc, mockCallback)).toThrowError(/stdout/)
472+
473+
// Cleanup
474+
proc?.kill()
475+
})
476+
})
450477
})
451478

452479
describe("escapeHtml", () => {

0 commit comments

Comments
 (0)
Please sign in to comment.