Skip to content

Commit 13762be

Browse files
committed
refacto CLI test code
1 parent 3f3780c commit 13762be

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/cli.test.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { runCommand } from "@oclif/test";
22
import fs from "fs";
3-
import { sep, posix } from "path";
3+
import { sep, posix, join } from "path";
44

55
/**
66
* For the CLI tests to run, we need to run them in a Node environment with
77
* the NODE_OPTIONS=--experimental-vm-modules flag. This is because Jest ships
88
* with experimental support for ECMAScript Modules (ESM).
99
* See: https://jestjs.io/docs/ecmascript-modules
1010
*/
11+
1112
describe("Oclif-provided Flags Tests", () => {
1213
describe("--help flag", () => {
1314
it("should provide the right help message", async () => {
@@ -89,14 +90,8 @@ describe("Config Prompt Tests", () => {
8990
expect(stderr).toContain("- Validating generated types");
9091
expect(stderr).toContain("✔ Validating generated types");
9192

92-
expect(
93-
normalizeLineEndings(
94-
fs.readFileSync(basicOutputPath, "utf-8").toString()
95-
)
96-
).toEqual(
97-
normalizeLineEndings(
98-
fs.readFileSync(basicSnapshotPath, "utf-8").toString()
99-
)
93+
expect(readFileCrossEnv(basicOutputPath)).toEqual(
94+
readFileCrossEnv(basicSnapshotPath)
10095
);
10196

10297
removeFile(basicOutputPath);
@@ -119,6 +114,19 @@ function normalizeLineEndings(content: string) {
119114
return content.replace(/\r\n/g, "\n"); // Replace Windows (\r\n) with Unix (\n)
120115
}
121116

117+
/**
118+
* Angle brackets from inquirer prompts are not the same in Windows & Unix
119+
* This function replaces them with a consistent character
120+
*/
122121
function replaceAngleBracket(content: string) {
123122
return content.replace(/>/g, "❯");
124123
}
124+
125+
/**
126+
* Gets the string content of a file and normalizes a few things to make it comparable to snapshots
127+
*/
128+
function readFileCrossEnv(path: string) {
129+
return replaceAngleBracket(
130+
normalizeLineEndings(fs.readFileSync(path, "utf-8").toString())
131+
);
132+
}

0 commit comments

Comments
 (0)