|
| 1 | +import { WriteStream } from "tty"; |
1 | 2 | import { jest } from "@jest/globals"; |
2 | 3 | import { StreamLogger } from "../src/io/logs"; |
3 | 4 | import { Output } from "../src/output"; |
4 | 5 | import { Program } from "../src/program"; |
5 | 6 |
|
6 | 7 | describe("Program", () => { |
| 8 | + describe("#constructor", () => { |
| 9 | + // To ensure that Commander is using the stdout stream we provide it. |
| 10 | + test("uses the stdout stream", async () => { |
| 11 | + const stdout = new WriteStream(1); |
| 12 | + const stderr = new WriteStream(2); |
| 13 | + const outSpy = jest.spyOn(stdout, "write").mockImplementation(() => true); |
| 14 | + const output = Output.create({ stdout, stderr }); |
| 15 | + const obj = new Program(output, "Example").exitOverride(() => { |
| 16 | + throw new Error("Fake!"); |
| 17 | + }); |
| 18 | + |
| 19 | + // Commander invokes `process.exit()` when you ask for the help screen. |
| 20 | + // To avoid that, we use the `.exitOverride()` method to throw an `Error`. |
| 21 | + // We might as well expect that `Error`. |
| 22 | + await expect(async () => { |
| 23 | + await obj.parseAsync(["--help"], { from: "user" }); |
| 24 | + }).rejects.toThrow({ name: "Error", message: "Fake!" }); |
| 25 | + |
| 26 | + // Expect the stdout.write method was called with the Help screen |
| 27 | + expect(outSpy).toHaveBeenCalledTimes(1); |
| 28 | + expect(outSpy).toHaveBeenCalledWith(expect.stringMatching("Example")); |
| 29 | + }); |
| 30 | + |
| 31 | + // To ensure that Commander is using the stderr stream we provide it. |
| 32 | + test("uses the stderr stream", async () => { |
| 33 | + const stdout = new WriteStream(1); |
| 34 | + const stderr = new WriteStream(2); |
| 35 | + const errSpy = jest.spyOn(stderr, "write").mockImplementation(() => true); |
| 36 | + const output = Output.create({ stdout, stderr }); |
| 37 | + |
| 38 | + const option = "non-existent"; |
| 39 | + const obj = new Program(output, "Example") |
| 40 | + .showHelpAfterError(true) |
| 41 | + .exitOverride(() => { |
| 42 | + throw new Error("Fake!"); |
| 43 | + }); |
| 44 | + |
| 45 | + // Commander invokes `process.exit()` when you specify a nonexistent |
| 46 | + // option. To avoid that, we use the `.exitOverride()` method to throw an |
| 47 | + // `Error`. We might as well expect that `Error`. |
| 48 | + await expect(async () => { |
| 49 | + await obj.parseAsync([`--${option}`], { from: "user" }); |
| 50 | + }).rejects.toThrow({ name: "Error", message: "Fake!" }); |
| 51 | + |
| 52 | + // Expect the stderr.write method was called with the unrecognized option |
| 53 | + expect(errSpy).toHaveBeenCalledTimes(3); |
| 54 | + expect(errSpy).toHaveBeenCalledWith( |
| 55 | + expect.stringMatching(`unknown option '--${option}'`), |
| 56 | + ); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
7 | 60 | describe("#log", () => { |
8 | 61 | test("behaves as expected", () => { |
9 | 62 | const output = Output.create({}); |
@@ -48,6 +101,32 @@ describe("Program", () => { |
48 | 101 | await obj.parseAsync([]); |
49 | 102 | }); |
50 | 103 |
|
| 104 | + // To ensure that Commander is using the stderr stream we provide it. |
| 105 | + test("uses the stderr stream", async () => { |
| 106 | + const stdout = new WriteStream(1); |
| 107 | + const stderr = new WriteStream(2); |
| 108 | + const errSpy = jest.spyOn(stderr, "write").mockImplementation(() => true); |
| 109 | + const output = Output.create({ stdout, stderr }); |
| 110 | + |
| 111 | + const expected = "Lorem ipsum dolor sit amet"; |
| 112 | + |
| 113 | + // Commander invokes `process.exit()` when you ask for the help screen. |
| 114 | + // To avoid that, we use the `.exitOverride()` method to throw an `Error`. |
| 115 | + const obj: Program<[], {}> = new Program(output) |
| 116 | + .action(() => obj.error(expected)) |
| 117 | + .exitOverride(() => { |
| 118 | + throw new Error("Fake!"); |
| 119 | + }); |
| 120 | + // We might as well expect that `Error`. |
| 121 | + await expect(async () => { |
| 122 | + await obj.parseAsync([]); |
| 123 | + }).rejects.toThrow({ name: "Error", message: "Fake!" }); |
| 124 | + |
| 125 | + // Expect the stderr.write method was called with the unrecognized option |
| 126 | + expect(errSpy).toHaveBeenCalledTimes(1); |
| 127 | + expect(errSpy).toHaveBeenCalledWith(expect.stringMatching(expected)); |
| 128 | + }); |
| 129 | + |
51 | 130 | test("fails on error", async () => { |
52 | 131 | const output = Output.create({}); |
53 | 132 | const obj = new Program(output); |
|
0 commit comments