Implementation:Cypress io Cypress AddTestingTypeToCypressConfig
| Knowledge Sources | |
|---|---|
| Domains | AST_Transformation, Configuration |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for adding testing type configuration blocks to cypress.config files via AST manipulation provided by @packages/config.
Description
The addTestingTypeToCypressConfig function reads the existing config file, uses addToCypressConfig to parse and inject an AST node representing the testing type block (e2e or component), and writes the result back. The underlying addToCypressConfigPlugin is a Babel visitor that handles multiple export patterns.
Usage
Called during the Cypress setup wizard when the user completes framework detection and the system needs to write the component or E2E configuration block to the config file.
Code Reference
Source Location
- Repository: cypress-io/cypress
- Files:
- packages/config/src/ast-utils/addToCypressConfig.ts:L99-135 (addTestingTypeToCypressConfig)
- packages/config/src/ast-utils/addToCypressConfig.ts:L43-56 (addToCypressConfig)
- packages/config/src/ast-utils/addToCypressConfigPlugin.ts:L22-221 (Babel plugin)
Signature
export async function addTestingTypeToCypressConfig(
options: AddTestingTypeToCypressConfigOptions
): Promise<AddToCypressConfigResult>
interface AddTestingTypeToCypressConfigOptions {
isProjectUsingESModules: boolean
filePath: string
info: ASTComponentDefinitionConfig | { testingType: 'e2e' }
}
interface AddToCypressConfigResult {
result: 'ADDED' | 'MERGED' | 'NEEDS_MERGE'
error?: Error
codeToMerge?: string
}
export async function addToCypressConfig(
filePath: string,
code: string,
toAdd: t.ObjectProperty
): Promise<string>
Import
import { addTestingTypeToCypressConfig } from '@packages/config/src/ast-utils/addToCypressConfig'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| options.filePath | string | Yes | Path to cypress.config.{ts,js} file |
| options.info | object | Yes | Testing type config ({ testingType: 'e2e' } or component config) |
| options.isProjectUsingESModules | boolean | Yes | Whether project uses ES modules |
Outputs
| Name | Type | Description |
|---|---|---|
| result | 'ADDED' / 'MERGED' / 'NEEDS_MERGE' | Outcome of the config modification |
| error | Error (optional) | Error if NEEDS_MERGE |
| codeToMerge | string (optional) | Code for manual merge if needed |
Usage Examples
Adding Component Testing Config
import { addTestingTypeToCypressConfig } from '@packages/config/src/ast-utils/addToCypressConfig'
const result = await addTestingTypeToCypressConfig({
filePath: '/project/cypress.config.ts',
isProjectUsingESModules: false,
info: {
testingType: 'component',
framework: 'react',
bundler: 'vite',
},
})
if (result.result === 'MERGED') {
console.log('Config updated successfully')
} else if (result.result === 'NEEDS_MERGE') {
console.log('Manual merge required:', result.codeToMerge)
}