diff --git a/tests/config/format-test-setup.js b/tests/config/format-test-setup.js index 83d496fe2..3d3b8b4c7 100644 --- a/tests/config/format-test-setup.js +++ b/tests/config/format-test-setup.js @@ -1,17 +1,3 @@ -import createEsmUtils from "esm-utils"; -import getPrettier from "./get-prettier.js"; import runSpec from "./format-test.js"; -async function setup() { - const { TEST_STANDALONE } = process.env; - - const prettier = TEST_STANDALONE - ? createEsmUtils(import.meta).require("./require-standalone.cjs") - : await getPrettier(); - - runSpec.prettier = prettier; - - globalThis.run_spec = runSpec; -} - -export default setup; +globalThis.run_spec = runSpec; diff --git a/tests/config/format-test.js b/tests/config/format-test.js index 94fea05b7..6106c978a 100644 --- a/tests/config/format-test.js +++ b/tests/config/format-test.js @@ -2,6 +2,7 @@ import fs from "node:fs"; import path from "node:path"; import url from "node:url"; import createEsmUtils from "esm-utils"; +import getPrettier from "./get-prettier.js"; import checkParsers from "./utils/check-parsers.js"; import createSnapshot from "./utils/create-snapshot.js"; import visualizeEndOfLine from "./utils/visualize-end-of-line.js"; @@ -10,8 +11,6 @@ import stringifyOptionsForTitle from "./utils/stringify-options-for-title.js"; const { __dirname } = createEsmUtils(import.meta); -let prettier; - const { FULL_TEST, TEST_STANDALONE } = process.env; const BOM = "\uFEFF"; @@ -432,6 +431,7 @@ function shouldSkipEolTest(code, options) { } async function parse(source, options) { + const prettier = await getPrettier(); const { ast } = await prettier.__debug.parse( source, options, @@ -486,6 +486,7 @@ async function format(originalText, originalOptions) { originalOptions ); const inputWithCursor = insertCursor(input, options.cursorOffset); + const prettier = await getPrettier(); const { formatted: output, cursorOffset } = await ensurePromise( prettier.formatWithCursor(input, options) @@ -506,13 +507,4 @@ async function format(originalText, originalOptions) { }; } -Object.defineProperty(runSpec, "prettier", { - get() { - return prettier; - }, - set(prettierModule) { - prettier = prettierModule; - }, - configurable: true, -}); export default runSpec; diff --git a/tests/config/get-prettier.js b/tests/config/get-prettier.js index 555a469a1..f4f3f1998 100644 --- a/tests/config/get-prettier.js +++ b/tests/config/get-prettier.js @@ -1,8 +1,23 @@ import { pathToFileURL } from "node:url"; import path from "node:path"; -const entry = pathToFileURL(path.join(process.env.PRETTIER_DIR, "index.js")); +function getPrettierInternal() { + if (process.env.TEST_STANDALONE) { + const entry = new URL("./require-standalone.cjs", import.meta.url); + return import(entry).then((module) => module.default); + } -const getPrettier = () => import(entry); + const entry = pathToFileURL(path.join(process.env.PRETTIER_DIR, "index.js")); + return import(entry); +} + +let promise; +function getPrettier() { + if (!promise) { + promise = getPrettierInternal(); + } + + return promise; +} export default getPrettier; diff --git a/tests/format/js/cursor/jsfmt.spec.js b/tests/format/js/cursor/jsfmt.spec.js index 8294ec943..e529618e5 100644 --- a/tests/format/js/cursor/jsfmt.spec.js +++ b/tests/format/js/cursor/jsfmt.spec.js @@ -1,10 +1,13 @@ import { outdent } from "outdent"; -// TODO: Use this when we drop support for Node.js 12 -// import prettier from "../../../config/prettier-entry.js"; -const { prettier } = run_spec; +import getPrettier from "../../../config/get-prettier.js"; run_spec(import.meta, ["babel", "typescript", "flow"]); +let prettier; +beforeAll(async () => { + prettier = await getPrettier(); +}); + test("translates cursor correctly in basic case", async () => { expect( await prettier.formatWithCursor(" 1", { parser: "babel", cursorOffset: 2 })