Skip to content

Commit

Permalink
refacto CLI test code
Browse files Browse the repository at this point in the history
  • Loading branch information
tvillaren committed Jun 24, 2024
1 parent fdf0f68 commit 5f3c975
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { runCommand } from "@oclif/test";
import fs from "fs";
import { sep, posix } from "path";
import { sep, posix, join } from "path";

/**
* For the CLI tests to run, we need to run them in a Node environment with
* the NODE_OPTIONS=--experimental-vm-modules flag. This is because Jest ships
* with experimental support for ECMAScript Modules (ESM).
* See: https://jestjs.io/docs/ecmascript-modules
*/

describe("Oclif-provided Flags Tests", () => {
describe("--help flag", () => {
it("should provide the right help message", async () => {
Expand Down Expand Up @@ -89,14 +90,8 @@ describe("Config Prompt Tests", () => {
expect(stderr).toContain("- Validating generated types");
expect(stderr).toContain("✔ Validating generated types");

expect(
normalizeLineEndings(
fs.readFileSync(basicOutputPath, "utf-8").toString()
)
).toEqual(
normalizeLineEndings(
fs.readFileSync(basicSnapshotPath, "utf-8").toString()
)
expect(readFileCrossEnv(basicOutputPath)).toEqual(
readFileCrossEnv(basicSnapshotPath)
);

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

/**
* Angle brackets from inquirer prompts are not the same in Windows & Unix
* This function replaces them with a consistent character
*/
function replaceAngleBracket(content: string) {
return content.replace(/>/g, "❯");
}

/**
* Gets the string content of a file and normalizes a few things to make it comparable to snapshots
*/
function readFileCrossEnv(path: string) {
return replaceAngleBracket(
normalizeLineEndings(fs.readFileSync(path, "utf-8").toString())
);
}

0 comments on commit 5f3c975

Please sign in to comment.