Improve format test setup (#12854)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-05-10 15:43:02 +08:00
1 parent 567e4d9e79
commit e1f972e90f
4 files changed
+27 -31

No files matched your search

+1 -15
View File
@@ -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;
+3 -11
View File
@@ -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;
+17 -2
View File
@@ -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;
+6 -3
View File
@@ -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 })