From 39502368330765e288eb540b938e8d971ddeac46 Mon Sep 17 00:00:00 2001 From: Fabio Spampinato Date: Sun, 15 Sep 2024 17:30:03 +0100 Subject: [PATCH] Ported test suite: check --- test/__tests__/__snapshots__/check.js.snap | 33 +++++++++++ test/__tests__/check.js | 65 ++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 test/__tests__/__snapshots__/check.js.snap create mode 100644 test/__tests__/check.js diff --git a/test/__tests__/__snapshots__/check.js.snap b/test/__tests__/__snapshots__/check.js.snap new file mode 100644 index 0000000..4267983 --- /dev/null +++ b/test/__tests__/__snapshots__/check.js.snap @@ -0,0 +1,33 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`--checks should print the number of files that need formatting (stderr) 1`] = ` +"[warn] unformatted.js +[warn] unformatted2.js +[warn] Code style issues found in 2 files. Run Prettier with --write to fix." +`; + +exports[`--checks should print the number of files that need formatting (stdout) 1`] = `"Checking formatting..."`; + +exports[`--checks should print the number of files that need formatting (write) 1`] = `[]`; + +exports[`--checks works in CI just as in a non-TTY mode (stderr) 1`] = ` +"[warn] unformatted.js +[warn] Code style issues found in 1 file. Run Prettier with --write to fix." +`; + +exports[`--checks works in CI just as in a non-TTY mode (stderr) 2`] = ` +"[warn] unformatted.js +[warn] Code style issues found in 1 file. Run Prettier with --write to fix." +`; + +exports[`--checks works in CI just as in a non-TTY mode (stdout) 1`] = `"Checking formatting..."`; + +exports[`--checks works in CI just as in a non-TTY mode (stdout) 2`] = `"Checking formatting..."`; + +exports[`--checks works in CI just as in a non-TTY mode (write) 1`] = `[]`; + +exports[`--checks works in CI just as in a non-TTY mode (write) 2`] = `[]`; + +exports[`checks stdin with --check (write) 1`] = `[]`; + +exports[`checks stdin with -c (alias for --check) (write) 1`] = `[]`; diff --git a/test/__tests__/check.js b/test/__tests__/check.js new file mode 100644 index 0000000..1a8ac77 --- /dev/null +++ b/test/__tests__/check.js @@ -0,0 +1,65 @@ +import { runCli } from "../utils"; + +describe("checks stdin with --check", () => { + runCli("with-shebang", [ + "--check", + "--parser", + "babel", + ], { + input: "0", + }).test({ + stdout: "(stdin)", + stderr: "", + status: "non-zero", + }); +}); + +describe("checks stdin with -c (alias for --check)", () => { + runCli("with-shebang", [ + "-c", + "--parser", + "babel", + ], { + input: "0", + }).test({ + stdout: "(stdin)", + stderr: "", + status: "non-zero", + }); +}); + +describe("--checks works in CI just as in a non-TTY mode", () => { + const result0 = runCli("write", [ + "--check", + "formatted.js", + "unformatted.js", + ]).test({ + status: 1, + }); + + const result1 = runCli("write", [ + "--check", + "formatted.js", + "unformatted.js", + ], { + tty: false, //TODO: actually implement this, read .isTTY via a env-mockable module + }).test({ + status: 1, + }); + + test("Should have same stdout", async () => { + expect(await result0.stdout).toEqual(await result1.stdout); + }); +}); + +describe("--checks should print the number of files that need formatting", () => { + runCli("write", [ + "--check", + "unformatted.js", + "unformatted2.js", + ], { + input: "0", + }).test({ + status: 1, + }); +});