From 6f12b94ae93c3190ae040bb621d061991ef69413 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 8 Jan 2025 10:00:39 +0800 Subject: [PATCH] Fix tests for ts config (#16970) --- changelog_unreleased/api/16828.md | 2 +- .../__tests__/config-file-typescript.js | 43 +++++++++++-------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/changelog_unreleased/api/16828.md b/changelog_unreleased/api/16828.md index c737f1a24..7d7769320 100644 --- a/changelog_unreleased/api/16828.md +++ b/changelog_unreleased/api/16828.md @@ -13,7 +13,7 @@ Note: Currently TypeScript support in Node.js is experimental. -To make TypeScript config files work, Node.js>=22.6.0 is required and `--experimental-strip-types` is required to run Node.js. +To make TypeScript config files work, Node.js>=22.6.0 is required and Node.js v22 requires `--experimental-strip-types`. You can run prettier with diff --git a/tests/integration/__tests__/config-file-typescript.js b/tests/integration/__tests__/config-file-typescript.js index 29c76bfe3..585ea19ac 100644 --- a/tests/integration/__tests__/config-file-typescript.js +++ b/tests/integration/__tests__/config-file-typescript.js @@ -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(?\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/",