From aedfc04b63ba2f2f139c9cc85b182d111181dc9d Mon Sep 17 00:00:00 2001 From: Evan Jacobs Date: Fri, 6 Dec 2024 15:19:02 -0500 Subject: [PATCH 1/2] feat(types): export CurrentsConfig for typed config files --- packages/cypress-cloud/index.ts | 9 +++-- packages/cypress-cloud/lib/config/config.ts | 41 ++++++++++++--------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/packages/cypress-cloud/index.ts b/packages/cypress-cloud/index.ts index a8c53bf7..d031232a 100644 --- a/packages/cypress-cloud/index.ts +++ b/packages/cypress-cloud/index.ts @@ -1,9 +1,10 @@ /// -import "source-map-support/register.js"; +import 'source-map-support/register.js'; -import { run as internalRun } from "./lib/run"; -import { CurrentsRunAPI } from "./types"; -export type { CurrentsRunAPI } from "./types"; +import { run as internalRun } from './lib/run'; +export type { CurrentsConfig } from './lib/config/config'; +import { CurrentsRunAPI } from './types'; +export type { CurrentsRunAPI } from './types'; /** * Run Cypress tests with a cloud service of your choice and return the results * diff --git a/packages/cypress-cloud/lib/config/config.ts b/packages/cypress-cloud/lib/config/config.ts index 08c2317a..95962a79 100644 --- a/packages/cypress-cloud/lib/config/config.ts +++ b/packages/cypress-cloud/lib/config/config.ts @@ -1,12 +1,12 @@ -import Debug from "debug"; +import Debug from 'debug'; -import { P, match } from "ts-pattern"; -import { DetectedBrowser, ValidatedCurrentsParameters } from "../../types"; -import { bootCypress } from "../bootstrap"; -import { info, warn } from "../log"; -import { getConfigFilePath } from "./path"; +import { P, match } from 'ts-pattern'; +import { DetectedBrowser, ValidatedCurrentsParameters } from '../../types'; +import { bootCypress } from '../bootstrap'; +import { info, warn } from '../log'; +import { getConfigFilePath } from './path'; -const debug = Debug("currents:config"); +const debug = Debug('currents:config'); export type E2EConfig = { batchSize: number; @@ -18,6 +18,14 @@ export type ComponentConfig = { type RetryConfig = { hardFailureMaxRetries: number; }; + +/** + * This is the type for `currents.config.*s`. If you are not officially using TypeScript, + * you can still type the exported config in your IDE by adding the following as a block comment + * above `module.exports` / `export default`: + * + * `@type {import('cypress-cloud').CurrentsConfig}` + */ export type CurrentsConfig = { projectId?: string; recordKey?: string; @@ -37,7 +45,7 @@ const defaultConfig: CurrentsConfig = { component: { batchSize: 5, }, - cloudServiceUrl: "https://cy.currents.dev", + cloudServiceUrl: 'https://cy.currents.dev', networkHeaders: undefined, }; @@ -68,10 +76,7 @@ export async function getCurrentsConfig( } } - warn( - "Failed to load config file, falling back to the default config. Attempted locations: %s", - configFilePath - ); + warn('Failed to load config file, falling back to the default config. Attempted locations: %s', configFilePath); _config = defaultConfig; return _config; } @@ -81,14 +86,14 @@ async function loadConfigFile(filepath: string) { debug("loading currents config file from '%s'", filepath); return await import(filepath); } catch (e) { - debug("failed loading config file from: %s", e); + debug('failed loading config file from: %s', e); return null; } } export type MergedConfig = Awaited>; export async function getMergedConfig(params: ValidatedCurrentsParameters) { - debug("resolving cypress config"); + debug('resolving cypress config'); const cypressResolvedConfig: | (Cypress.ResolvedConfigOptions & { projectRoot: string; @@ -97,12 +102,12 @@ export async function getMergedConfig(params: ValidatedCurrentsParameters) { }) | undefined = await bootCypress(params); - debug("cypress resolvedConfig: %O", cypressResolvedConfig); + debug('cypress resolvedConfig: %O', cypressResolvedConfig); // @ts-ignore const rawE2EPattern = cypressResolvedConfig.rawJson?.e2e?.specPattern; let additionalIgnorePattern: string[] = []; - if (params.testingType === "component" && rawE2EPattern) { + if (params.testingType === 'component' && rawE2EPattern) { // @ts-ignore additionalIgnorePattern = rawE2EPattern; } @@ -112,7 +117,7 @@ export async function getMergedConfig(params: ValidatedCurrentsParameters) { const result = { projectRoot: cypressResolvedConfig?.projectRoot || process.cwd(), projectId: params.projectId, - specPattern: cypressResolvedConfig?.specPattern || "**/*.*", + specPattern: cypressResolvedConfig?.specPattern || '**/*.*', excludeSpecPattern: // @ts-ignore cypressResolvedConfig?.resolved.excludeSpecPattern.value ?? [], @@ -120,6 +125,6 @@ export async function getMergedConfig(params: ValidatedCurrentsParameters) { resolved: cypressResolvedConfig, experimentalCoverageRecording: params.experimentalCoverageRecording, }; - debug("merged config: %O", result); + debug('merged config: %O', result); return result; } From e93aa7b98ab2903c4d9b4c8a7c615a6edccb5297 Mon Sep 17 00:00:00 2001 From: Evan Jacobs Date: Fri, 6 Dec 2024 15:20:59 -0500 Subject: [PATCH 2/2] chore: ensure consistency by adding prettier config for IDE to pick up --- .prettierrc | 3 ++ packages/cypress-cloud/index.ts | 10 +++--- packages/cypress-cloud/lib/config/config.ts | 35 +++++++++++---------- 3 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..1ca87ab7 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,3 @@ +{ + "singleQuote": false +} diff --git a/packages/cypress-cloud/index.ts b/packages/cypress-cloud/index.ts index d031232a..3486d50a 100644 --- a/packages/cypress-cloud/index.ts +++ b/packages/cypress-cloud/index.ts @@ -1,10 +1,10 @@ /// -import 'source-map-support/register.js'; +import "source-map-support/register.js"; -import { run as internalRun } from './lib/run'; -export type { CurrentsConfig } from './lib/config/config'; -import { CurrentsRunAPI } from './types'; -export type { CurrentsRunAPI } from './types'; +import { run as internalRun } from "./lib/run"; +export type { CurrentsConfig } from "./lib/config/config"; +import { CurrentsRunAPI } from "./types"; +export type { CurrentsRunAPI } from "./types"; /** * Run Cypress tests with a cloud service of your choice and return the results * diff --git a/packages/cypress-cloud/lib/config/config.ts b/packages/cypress-cloud/lib/config/config.ts index 95962a79..26e414f4 100644 --- a/packages/cypress-cloud/lib/config/config.ts +++ b/packages/cypress-cloud/lib/config/config.ts @@ -1,12 +1,12 @@ -import Debug from 'debug'; +import Debug from "debug"; -import { P, match } from 'ts-pattern'; -import { DetectedBrowser, ValidatedCurrentsParameters } from '../../types'; -import { bootCypress } from '../bootstrap'; -import { info, warn } from '../log'; -import { getConfigFilePath } from './path'; +import { P, match } from "ts-pattern"; +import { DetectedBrowser, ValidatedCurrentsParameters } from "../../types"; +import { bootCypress } from "../bootstrap"; +import { info, warn } from "../log"; +import { getConfigFilePath } from "./path"; -const debug = Debug('currents:config'); +const debug = Debug("currents:config"); export type E2EConfig = { batchSize: number; @@ -45,13 +45,13 @@ const defaultConfig: CurrentsConfig = { component: { batchSize: 5, }, - cloudServiceUrl: 'https://cy.currents.dev', + cloudServiceUrl: "https://cy.currents.dev", networkHeaders: undefined, }; export async function getCurrentsConfig( projectRoot?: string, - explicitConfigFilePath?: string + explicitConfigFilePath?: string, ): Promise { if (_config) { return _config; @@ -76,7 +76,10 @@ export async function getCurrentsConfig( } } - warn('Failed to load config file, falling back to the default config. Attempted locations: %s', configFilePath); + warn( + "Failed to load config file, falling back to the default config. Attempted locations: %s", + configFilePath, + ); _config = defaultConfig; return _config; } @@ -86,14 +89,14 @@ async function loadConfigFile(filepath: string) { debug("loading currents config file from '%s'", filepath); return await import(filepath); } catch (e) { - debug('failed loading config file from: %s', e); + debug("failed loading config file from: %s", e); return null; } } export type MergedConfig = Awaited>; export async function getMergedConfig(params: ValidatedCurrentsParameters) { - debug('resolving cypress config'); + debug("resolving cypress config"); const cypressResolvedConfig: | (Cypress.ResolvedConfigOptions & { projectRoot: string; @@ -102,12 +105,12 @@ export async function getMergedConfig(params: ValidatedCurrentsParameters) { }) | undefined = await bootCypress(params); - debug('cypress resolvedConfig: %O', cypressResolvedConfig); + debug("cypress resolvedConfig: %O", cypressResolvedConfig); // @ts-ignore const rawE2EPattern = cypressResolvedConfig.rawJson?.e2e?.specPattern; let additionalIgnorePattern: string[] = []; - if (params.testingType === 'component' && rawE2EPattern) { + if (params.testingType === "component" && rawE2EPattern) { // @ts-ignore additionalIgnorePattern = rawE2EPattern; } @@ -117,7 +120,7 @@ export async function getMergedConfig(params: ValidatedCurrentsParameters) { const result = { projectRoot: cypressResolvedConfig?.projectRoot || process.cwd(), projectId: params.projectId, - specPattern: cypressResolvedConfig?.specPattern || '**/*.*', + specPattern: cypressResolvedConfig?.specPattern || "**/*.*", excludeSpecPattern: // @ts-ignore cypressResolvedConfig?.resolved.excludeSpecPattern.value ?? [], @@ -125,6 +128,6 @@ export async function getMergedConfig(params: ValidatedCurrentsParameters) { resolved: cypressResolvedConfig, experimentalCoverageRecording: params.experimentalCoverageRecording, }; - debug('merged config: %O', result); + debug("merged config: %O", result); return result; }