Fix tests for ts config (#16970)

This commit is contained in:
fisker Cheung authored and GitHub committed 2025-01-08 10:00:39 +08:00
1 parent 678bf05dc9
commit 6f12b94ae9
2 files changed
+26 -19

No files matched your search

@@ -4,7 +4,7 @@ import jestPathSerializer from "../path-serializer.js";
expect.addSnapshotSerializer(jestPathSerializer);
const NODE_TS_SUPPORT_FLAGS = ["--experimental-strip-types"];
const IS_SUPPORTED_NODE_JS = Number(process.versions.node.split(".")[0]) >= 22;
const NODE_JS_MAJOR_VERSION = Number(process.versions.node.split(".")[0]);
const getOutputTabWidth = (code) =>
code.match(/\n(?<indention>\s+)return/u).groups.indention.length;
@@ -16,33 +16,23 @@ const TAB_WIDTH_3_OUTPUT = outdent`
}
`;
if (IS_SUPPORTED_NODE_JS) {
// Node.js>=22 supports ts files, 22 requires experimental flag, 23 doesn't
if (NODE_JS_MAJOR_VERSION >= 22) {
const nodeOptions = NODE_JS_MAJOR_VERSION === 22 ? NODE_TS_SUPPORT_FLAGS : [];
test("Should support typescript config files", async () => {
const output = await runCli(
"cli/config/ts/auto-discovery/",
["--stdin-filepath", "foo.js"],
{
input: code,
nodeOptions: NODE_TS_SUPPORT_FLAGS,
nodeOptions,
},
).stdout;
expect(output).toBe(TAB_WIDTH_3_OUTPUT);
});
test("Should throw errors when flags are missing", async () => {
await runCli(
"cli/config/ts/auto-discovery/",
["--stdin-filepath", "foo.js"],
{ input: code },
).test({
status: "non-zero",
stdout: "",
write: [],
stderr: expect.stringMatching(/Unknown file extension ".ts" for/u),
});
});
describe("Config file names", () => {
for (const { configFileName, expectedTabWidth } of [
{ configFileName: ".prettierrc.cts", expectedTabWidth: 8 },
@@ -58,7 +48,7 @@ if (IS_SUPPORTED_NODE_JS) {
["--stdin-filepath", "foo.js", "--config", configFileName],
{
input: code,
nodeOptions: NODE_TS_SUPPORT_FLAGS,
nodeOptions,
},
).stdout;
@@ -66,7 +56,24 @@ if (IS_SUPPORTED_NODE_JS) {
});
}
});
} else {
}
if (NODE_JS_MAJOR_VERSION === 22) {
test("Should throw errors when flags are missing", async () => {
await runCli(
"cli/config/ts/auto-discovery/",
["--stdin-filepath", "foo.js"],
{ input: code },
).test({
status: "non-zero",
stdout: "",
write: [],
stderr: expect.stringMatching(/Unknown file extension ".ts" for/u),
});
});
}
if (NODE_JS_MAJOR_VERSION < 22) {
test("Should throw errors when Node.js < 22", async () => {
await runCli(
"cli/config/ts/auto-discovery/",