From 360c4bd4ddd8177b530ecd2ed70a5382bfbea754 Mon Sep 17 00:00:00 2001 From: Olivier Martin Date: Mon, 3 Aug 2026 11:07:18 +0200 Subject: [PATCH] fix: resolve prettier config from a file path instead of the cwd directory resolveConfig() expects a file path and searches upward from its parent directory. Passing process.cwd() (a directory) starts the search one level above the project root, so the project's own .prettierrc.json is never found and codegen silently falls back to Prettier's defaults. Pass the cypress/commands/index.ts path instead so the search starts inside the project. Co-Authored-By: Claude Sonnet 5 --- src/codegen.ts | 2 +- test/codegen.test.ts | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/codegen.test.ts diff --git a/src/codegen.ts b/src/codegen.ts index 9d641da2..25b97f7d 100644 --- a/src/codegen.ts +++ b/src/codegen.ts @@ -25,9 +25,9 @@ export const codegen = async (config: Partial) => { nodir: true, ignore: indexTsFile, }); - const prettierConfig = (await resolveConfig(process.cwd())) ?? {}; const commandsIndexPath = "cypress/commands/index.ts"; + const prettierConfig = (await resolveConfig(resolve(commandsIndexPath))) ?? {}; const exportFileContents = await generateContentWithExports( filePaths, prettierConfig, diff --git a/test/codegen.test.ts b/test/codegen.test.ts new file mode 100644 index 00000000..099bd4c4 --- /dev/null +++ b/test/codegen.test.ts @@ -0,0 +1,47 @@ +/* +Copyright 2022 Expedia, Inc. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + https://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import { describe, expect, it, mock } from "bun:test"; +import { resolve } from "path"; + +const resolveConfigMock = mock().mockResolvedValue({ singleQuote: true }); +const actualPrettier = await import("prettier"); +mock.module("prettier", () => ({ + ...actualPrettier, + resolveConfig: resolveConfigMock, +})); + +const globSyncMock = mock().mockReturnValue(["cypress/commands/foo.ts"]); +mock.module("glob", () => ({ + globSync: globSyncMock, +})); + +const actualFs = await import("fs"); +mock.module("fs", () => ({ + ...actualFs, + readFileSync: mock().mockReturnValue("Cypress.Commands.add('foo', () => {});"), + writeFileSync: mock(), +})); + +const { codegen } = await import("../src/codegen"); + +describe("codegen", () => { + it("resolves the prettier config from a file inside the project instead of the cwd directory", async () => { + await codegen({ testingType: "e2e" }); + + expect(resolveConfigMock).toHaveBeenCalledWith( + resolve("cypress/commands/index.ts"), + ); + expect(resolveConfigMock).not.toHaveBeenCalledWith(process.cwd()); + }); +});