From ee17a2ff8ba43dbce93258f64383bf21ebef98d8 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 21 May 2022 11:11:06 +0800 Subject: [PATCH] Add setup file to integration test (#12855) --- .eslintrc.cjs | 3 ++- jest-integration-test.config.mjs | 1 + tests/integration/__tests__/arg-parsing.js | 1 - tests/integration/__tests__/check.js | 2 -- tests/integration/__tests__/cursor-offset.js | 2 -- tests/integration/__tests__/debug-check.js | 2 -- tests/integration/__tests__/debug-print-ast.js | 2 -- tests/integration/__tests__/debug-print-comments.js | 2 -- tests/integration/__tests__/debug-print-doc.js | 2 -- tests/integration/__tests__/early-exit.js | 2 -- tests/integration/__tests__/error-on-unmatched-pattern.js | 2 -- tests/integration/__tests__/ignore-absolute-path.js | 2 -- tests/integration/__tests__/ignore-path.js | 2 -- tests/integration/__tests__/ignore-unknown.js | 2 -- tests/integration/__tests__/infer-parser.js | 2 -- tests/integration/__tests__/list-different.js | 2 -- tests/integration/__tests__/loglevel.js | 2 -- tests/integration/__tests__/parser-api.js | 2 -- tests/integration/__tests__/performance-flags.js | 2 -- tests/integration/__tests__/piped-output.js | 2 -- tests/integration/__tests__/plugin-default-options.js | 2 -- tests/integration/__tests__/plugin-flush-line-suffix.js | 2 -- tests/integration/__tests__/plugin-options-string.js | 2 -- tests/integration/__tests__/plugin-options.js | 2 -- tests/integration/__tests__/plugin-precedence.js | 2 -- tests/integration/__tests__/plugin-virtual-directory.js | 2 -- tests/integration/__tests__/stdin-filepath.js | 2 -- tests/integration/__tests__/support-info.js | 2 -- tests/integration/__tests__/syntax-error.js | 2 -- tests/integration/__tests__/with-config-precedence.js | 2 -- tests/integration/__tests__/with-parser-inference.js | 2 -- tests/integration/__tests__/with-shebang.js | 2 -- tests/integration/__tests__/write.js | 2 -- tests/integration/integration-test-setup.js | 3 +++ 34 files changed, 6 insertions(+), 62 deletions(-) create mode 100644 tests/integration/integration-test-setup.js diff --git a/.eslintrc.cjs b/.eslintrc.cjs index b8ab17800..1ad73341d 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -237,7 +237,8 @@ module.exports = { "unicorn/prefer-array-flat-map": "off", }, globals: { - run_spec: false, + run_spec: "readonly", + runPrettier: "readonly", }, }, { diff --git a/jest-integration-test.config.mjs b/jest-integration-test.config.mjs index 20d03a514..5e7ec0ea1 100644 --- a/jest-integration-test.config.mjs +++ b/jest-integration-test.config.mjs @@ -3,6 +3,7 @@ import baseConfig from "./jest.config.mjs"; const config = { ...baseConfig, testRegex: "tests/integration/__tests__/.*\\.js$", + setupFiles: ["/tests/integration/integration-test-setup.js"], projects: [], moduleNameMapper: { ...baseConfig.moduleNameMapper, diff --git a/tests/integration/__tests__/arg-parsing.js b/tests/integration/__tests__/arg-parsing.js index 19235f407..8d5939a26 100644 --- a/tests/integration/__tests__/arg-parsing.js +++ b/tests/integration/__tests__/arg-parsing.js @@ -1,4 +1,3 @@ -import runPrettier from "../run-prettier.js"; import jestPathSerializer from "../path-serializer.js"; expect.addSnapshotSerializer(jestPathSerializer); diff --git a/tests/integration/__tests__/check.js b/tests/integration/__tests__/check.js index b6c9b7852..1b40713bc 100644 --- a/tests/integration/__tests__/check.js +++ b/tests/integration/__tests__/check.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("checks stdin with --check", () => { runPrettier("cli/with-shebang", ["--check", "--parser", "babel"], { input: "0", diff --git a/tests/integration/__tests__/cursor-offset.js b/tests/integration/__tests__/cursor-offset.js index ae493b783..9a18330c3 100644 --- a/tests/integration/__tests__/cursor-offset.js +++ b/tests/integration/__tests__/cursor-offset.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("write cursorOffset to stderr with --cursor-offset ", () => { runPrettier("cli", ["--cursor-offset", "2", "--parser", "babel"], { input: " 1", diff --git a/tests/integration/__tests__/debug-check.js b/tests/integration/__tests__/debug-check.js index ea14c95f6..7f55ac4cd 100644 --- a/tests/integration/__tests__/debug-check.js +++ b/tests/integration/__tests__/debug-check.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("doesn't crash when --debug-check is passed", () => { runPrettier("cli/with-shebang", ["issue1890.js", "--debug-check"]).test({ stdout: "issue1890.js\n", diff --git a/tests/integration/__tests__/debug-print-ast.js b/tests/integration/__tests__/debug-print-ast.js index 108523c0c..84a63146f 100644 --- a/tests/integration/__tests__/debug-print-ast.js +++ b/tests/integration/__tests__/debug-print-ast.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("prints information for debugging AST --debug-print-ast", () => { runPrettier("cli/with-shebang", ["--debug-print-ast", "--parser", "babel"], { input: "const foo = 'foo';", diff --git a/tests/integration/__tests__/debug-print-comments.js b/tests/integration/__tests__/debug-print-comments.js index e89b4d1b7..015063b8a 100644 --- a/tests/integration/__tests__/debug-print-comments.js +++ b/tests/integration/__tests__/debug-print-comments.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("prints information for debugging comment attachment with --debug-print-comments", () => { runPrettier( "cli/with-shebang", diff --git a/tests/integration/__tests__/debug-print-doc.js b/tests/integration/__tests__/debug-print-doc.js index aeb6807de..3a00d22f1 100644 --- a/tests/integration/__tests__/debug-print-doc.js +++ b/tests/integration/__tests__/debug-print-doc.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("prints doc with --debug-print-doc", () => { runPrettier("cli/with-shebang", ["--debug-print-doc", "--parser", "babel"], { input: "0", diff --git a/tests/integration/__tests__/early-exit.js b/tests/integration/__tests__/early-exit.js index cb5b93f97..870fa667c 100644 --- a/tests/integration/__tests__/early-exit.js +++ b/tests/integration/__tests__/early-exit.js @@ -1,6 +1,4 @@ import prettier from "prettier-local"; -import runPrettier from "../run-prettier.js"; - describe("show version with --version", () => { runPrettier("cli/with-shebang", ["--version"]).test({ stdout: prettier.version + "\n", diff --git a/tests/integration/__tests__/error-on-unmatched-pattern.js b/tests/integration/__tests__/error-on-unmatched-pattern.js index 22c7981d0..1d29b9d36 100644 --- a/tests/integration/__tests__/error-on-unmatched-pattern.js +++ b/tests/integration/__tests__/error-on-unmatched-pattern.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("no error on unmatched pattern", () => { runPrettier("cli/error-on-unmatched-pattern", [ "--no-error-on-unmatched-pattern", diff --git a/tests/integration/__tests__/ignore-absolute-path.js b/tests/integration/__tests__/ignore-absolute-path.js index 0c1a323c3..867b2717a 100644 --- a/tests/integration/__tests__/ignore-absolute-path.js +++ b/tests/integration/__tests__/ignore-absolute-path.js @@ -1,7 +1,5 @@ import path from "node:path"; import createEsmUtils from "esm-utils"; -import runPrettier from "../run-prettier.js"; - const { __dirname } = createEsmUtils(import.meta); describe("support absolute filename", () => { diff --git a/tests/integration/__tests__/ignore-path.js b/tests/integration/__tests__/ignore-path.js index 5f83fc05c..c74b7d009 100644 --- a/tests/integration/__tests__/ignore-path.js +++ b/tests/integration/__tests__/ignore-path.js @@ -1,8 +1,6 @@ import path from "node:path"; import fs from "node:fs"; import createEsmUtils from "esm-utils"; -import runPrettier from "../run-prettier.js"; - const { __dirname } = createEsmUtils(import.meta); fs.writeFileSync( diff --git a/tests/integration/__tests__/ignore-unknown.js b/tests/integration/__tests__/ignore-unknown.js index 5a4a14120..65d2a192a 100644 --- a/tests/integration/__tests__/ignore-unknown.js +++ b/tests/integration/__tests__/ignore-unknown.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("ignore-unknown dir", () => { runPrettier("cli/ignore-unknown", [ ".", diff --git a/tests/integration/__tests__/infer-parser.js b/tests/integration/__tests__/infer-parser.js index 36fe42bdb..74add0d7b 100644 --- a/tests/integration/__tests__/infer-parser.js +++ b/tests/integration/__tests__/infer-parser.js @@ -1,6 +1,4 @@ import prettier from "prettier-local"; -import runPrettier from "../run-prettier.js"; - const { jest } = import.meta; describe("stdin no path and no parser", () => { diff --git a/tests/integration/__tests__/list-different.js b/tests/integration/__tests__/list-different.js index 8d20b4de1..e72d279e3 100644 --- a/tests/integration/__tests__/list-different.js +++ b/tests/integration/__tests__/list-different.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("checks stdin with --list-different", () => { runPrettier("cli/with-shebang", ["--list-different", "--parser", "babel"], { input: "0", diff --git a/tests/integration/__tests__/loglevel.js b/tests/integration/__tests__/loglevel.js index 16130d3af..6767f9533 100644 --- a/tests/integration/__tests__/loglevel.js +++ b/tests/integration/__tests__/loglevel.js @@ -1,6 +1,4 @@ import stripAnsi from "strip-ansi"; -import runPrettier from "../run-prettier.js"; - test("do not show logs with --loglevel silent", async () => { await runPrettierWithLogLevel("silent", null); }); diff --git a/tests/integration/__tests__/parser-api.js b/tests/integration/__tests__/parser-api.js index 500c9256a..9d2ac5034 100644 --- a/tests/integration/__tests__/parser-api.js +++ b/tests/integration/__tests__/parser-api.js @@ -1,6 +1,4 @@ import prettier from "prettier-local"; -import runPrettier from "../run-prettier.js"; - test("allows custom parser provided as object", async () => { const output = await prettier.format("1", { parser(text) { diff --git a/tests/integration/__tests__/performance-flags.js b/tests/integration/__tests__/performance-flags.js index 15d1e0e92..0a6af5223 100644 --- a/tests/integration/__tests__/performance-flags.js +++ b/tests/integration/__tests__/performance-flags.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("should not write file or print code when `--debug-benchmark` or `--debug-repeat` found", () => { const assertStderr = (message) => { expect( diff --git a/tests/integration/__tests__/piped-output.js b/tests/integration/__tests__/piped-output.js index 5e60b8eba..84fa3e3f4 100644 --- a/tests/integration/__tests__/piped-output.js +++ b/tests/integration/__tests__/piped-output.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("output with --check + unformatted differs when piped", () => { const cli0 = runPrettier( "cli/write", diff --git a/tests/integration/__tests__/plugin-default-options.js b/tests/integration/__tests__/plugin-default-options.js index e4065f81c..4013d51ca 100644 --- a/tests/integration/__tests__/plugin-default-options.js +++ b/tests/integration/__tests__/plugin-default-options.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("plugin default options should work", () => { runPrettier( "plugins/defaultOptions", diff --git a/tests/integration/__tests__/plugin-flush-line-suffix.js b/tests/integration/__tests__/plugin-flush-line-suffix.js index a77b301b2..9e27ca228 100644 --- a/tests/integration/__tests__/plugin-flush-line-suffix.js +++ b/tests/integration/__tests__/plugin-flush-line-suffix.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("flush all line-suffix content", () => { runPrettier("plugins/flushLineSuffix", ["*.foo", "--plugin=./plugin.cjs"], { ignoreLineEndings: true, diff --git a/tests/integration/__tests__/plugin-options-string.js b/tests/integration/__tests__/plugin-options-string.js index 69b3aecc2..7de030322 100644 --- a/tests/integration/__tests__/plugin-options-string.js +++ b/tests/integration/__tests__/plugin-options-string.js @@ -1,6 +1,4 @@ import snapshotDiff from "snapshot-diff"; -import runPrettier from "../run-prettier.js"; - test("show external options with `--help`", async () => { const originalStdout = await runPrettier("plugins/options-string", ["--help"]) .stdout; diff --git a/tests/integration/__tests__/plugin-options.js b/tests/integration/__tests__/plugin-options.js index 13ad3a9a7..685d66561 100644 --- a/tests/integration/__tests__/plugin-options.js +++ b/tests/integration/__tests__/plugin-options.js @@ -1,6 +1,4 @@ import snapshotDiff from "snapshot-diff"; -import runPrettier from "../run-prettier.js"; - test("show external options with `--help`", async () => { const originalStdout = await runPrettier("plugins/options", ["--help"]) .stdout; diff --git a/tests/integration/__tests__/plugin-precedence.js b/tests/integration/__tests__/plugin-precedence.js index 8975f1728..b2780b726 100644 --- a/tests/integration/__tests__/plugin-precedence.js +++ b/tests/integration/__tests__/plugin-precedence.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("json-stringify takes precedence over json for package.json", () => { runPrettier("plugins", ["--stdin-filepath=package.json"], { input: diff --git a/tests/integration/__tests__/plugin-virtual-directory.js b/tests/integration/__tests__/plugin-virtual-directory.js index 3b697aff7..75b80e964 100644 --- a/tests/integration/__tests__/plugin-virtual-directory.js +++ b/tests/integration/__tests__/plugin-virtual-directory.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("plugin search should not crash when prettier isn't inside a directory", () => { runPrettier( "plugins/virtualDirectory", diff --git a/tests/integration/__tests__/stdin-filepath.js b/tests/integration/__tests__/stdin-filepath.js index 78db9989f..d68f7a427 100644 --- a/tests/integration/__tests__/stdin-filepath.js +++ b/tests/integration/__tests__/stdin-filepath.js @@ -1,7 +1,5 @@ import { isCI } from "ci-info"; import { outdent } from "outdent"; -import runPrettier from "../run-prettier.js"; - describe("format correctly if stdin content compatible with stdin-filepath", () => { runPrettier( "cli", diff --git a/tests/integration/__tests__/support-info.js b/tests/integration/__tests__/support-info.js index b8b9bc4e7..86cad768d 100644 --- a/tests/integration/__tests__/support-info.js +++ b/tests/integration/__tests__/support-info.js @@ -1,6 +1,4 @@ import prettier from "prettier-local"; -import runPrettier from "../run-prettier.js"; - test("API getSupportInfo()", async () => { expect(await getCoreInfo()).toMatchSnapshot(); }); diff --git a/tests/integration/__tests__/syntax-error.js b/tests/integration/__tests__/syntax-error.js index 238b6f00d..944feb63a 100644 --- a/tests/integration/__tests__/syntax-error.js +++ b/tests/integration/__tests__/syntax-error.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("exits with non-zero code when input has a syntax error", () => { runPrettier("cli/with-shebang", ["--parser", "babel"], { input: "a.2", diff --git a/tests/integration/__tests__/with-config-precedence.js b/tests/integration/__tests__/with-config-precedence.js index d4f7bc95b..b1e112a39 100644 --- a/tests/integration/__tests__/with-config-precedence.js +++ b/tests/integration/__tests__/with-config-precedence.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("CLI overrides take precedence without --config-precedence", () => { runPrettier("cli/config/", [ "--end-of-line", diff --git a/tests/integration/__tests__/with-parser-inference.js b/tests/integration/__tests__/with-parser-inference.js index 93e0f97df..8d88e3ef7 100644 --- a/tests/integration/__tests__/with-parser-inference.js +++ b/tests/integration/__tests__/with-parser-inference.js @@ -1,7 +1,5 @@ import prettier from "prettier-local"; import { outdent } from "outdent"; -import runPrettier from "../run-prettier.js"; - describe("infers postcss parser", () => { runPrettier("cli/with-parser-inference", ["--end-of-line", "lf", "*"]).test({ status: 0, diff --git a/tests/integration/__tests__/with-shebang.js b/tests/integration/__tests__/with-shebang.js index 24ea9d908..288809da5 100644 --- a/tests/integration/__tests__/with-shebang.js +++ b/tests/integration/__tests__/with-shebang.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("preserves shebang", () => { runPrettier("cli/with-shebang", ["--end-of-line", "lf", "issue1890.js"]).test( { diff --git a/tests/integration/__tests__/write.js b/tests/integration/__tests__/write.js index e6319f750..61b9ddbb4 100644 --- a/tests/integration/__tests__/write.js +++ b/tests/integration/__tests__/write.js @@ -1,5 +1,3 @@ -import runPrettier from "../run-prettier.js"; - describe("write file with --write + unformatted file", () => { runPrettier("cli/write", ["--write", "unformatted.js"]).test({ status: 0, diff --git a/tests/integration/integration-test-setup.js b/tests/integration/integration-test-setup.js new file mode 100644 index 000000000..0009cb720 --- /dev/null +++ b/tests/integration/integration-test-setup.js @@ -0,0 +1,3 @@ +import runPrettier from "./run-prettier.js"; + +globalThis.runPrettier = runPrettier;