Skip to content

Commit

Permalink
fix: scope config to current testing type (#20677)
Browse files Browse the repository at this point in the history
* fix: scope config to current testing type

* Revert options changes

* Fix some tests

* Fix more tests

* Fix more tests

* Fix more tests

* Update options

* Fix tests

* Fix tests

* Add comments

* Remove un-needed quote

* Fix test

* Fix tests
  • Loading branch information
estrada9166 authored Mar 24, 2022
1 parent 3042ef8 commit 61f7cfc
Show file tree
Hide file tree
Showing 32 changed files with 183 additions and 67 deletions.
2 changes: 1 addition & 1 deletion npm/angular/patches/@cypress+code-coverage+3.9.5.patch
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ index 31e00ee..0c56908 100644
/** @type {string} Cypress run-time config has test files string pattern */
// @ts-ignore
- const testFilePattern = config('testFiles')
+ const testFilePattern = config(Cypress.testingType).specPattern
+ const testFilePattern = config('specPattern')

// test files chould be:
// wild card string "**/*.*" (default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,12 @@ describe('lib/util/config-file-updater', () => {
}
`

const output = await insertValueInJSString(src, { component: { specFilePattern: 'src/**/*.spec.cy.js' } })
const output = await insertValueInJSString(src, { component: { specPattern: 'src/**/*.spec.cy.js' } })

const expectedOutput = stripIndent`\
module.exports = {
component: {
specFilePattern: "src/**/*.spec.cy.js",
specPattern: "src/**/*.spec.cy.js",
},
foo: 42
}
Expand All @@ -296,12 +296,12 @@ describe('lib/util/config-file-updater', () => {
}
`

const output = await insertValueInJSString(src, { component: { specFilePattern: 'src/**/*.spec.cy.js' } })
const output = await insertValueInJSString(src, { component: { specPattern: 'src/**/*.spec.cy.js' } })

const expectedOutput = stripIndent`\
module.exports = {
component: {
specFilePattern: "src/**/*.spec.cy.js",
specPattern: "src/**/*.spec.cy.js",
viewportWidth: 800
},
foo: 42
Expand All @@ -316,18 +316,18 @@ describe('lib/util/config-file-updater', () => {
module.exports = {
foo: 42,
component: {
specFilePattern: 'components/**/*.spec.cy.js',
specPattern: 'components/**/*.spec.cy.js',
foo: 82
}
}`

const output = await insertValueInJSString(src, { component: { specFilePattern: 'src/**/*.spec.cy.js' } })
const output = await insertValueInJSString(src, { component: { specPattern: 'src/**/*.spec.cy.js' } })

const expectedOutput = stripIndent`\
module.exports = {
foo: 42,
component: {
specFilePattern: "src/**/*.spec.cy.js",
specPattern: "src/**/*.spec.cy.js",
foo: 82
}
}`
Expand Down
2 changes: 1 addition & 1 deletion npm/react/plugins/utils/get-transpile-folders.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getTranspileFolders (config) {
}

// attempt to add directories based on spec pattern
let componentDirs = config.component.specPattern || ''
let componentDirs = config.specPattern || ''

// can be string or array
if (typeof componentDirs === 'string') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ index 31e00ee..0c56908 100644
/** @type {string} Cypress run-time config has test files string pattern */
// @ts-ignore
- const testFilePattern = config('testFiles')
+ const testFilePattern = config(Cypress.testingType).specPattern
+ const testFilePattern = config('specPattern')

// test files chould be:
// wild card string "**/*.*" (default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ index 31e00ee..0c56908 100644
/** @type {string} Cypress run-time config has test files string pattern */
// @ts-ignore
- const testFilePattern = config('testFiles')
+ const testFilePattern = config(Cypress.testingType).specPattern
+ const testFilePattern = config('specPattern')

// test files chould be:
// wild card string "**/*.*" (default)
Expand Down
4 changes: 2 additions & 2 deletions packages/app/cypress/e2e/settings.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ describe('App: Settings', () => {
cy.get('[data-cy="config-code"]').within(() => {
cy.get('.bg-teal-100').contains('tests/_fixtures')
cy.get('.bg-teal-100').contains('abc123')
cy.get('.bg-teal-100').contains('specFilePattern')
cy.get('.bg-teal-100').contains('supportFile')
cy.get('.bg-teal-100').contains('tests/**/*')
cy.get('.bg-teal-100').contains('tests/_support/spec_helper.js')
cy.get('.bg-yellow-100').contains('REMOTE_DEBUGGING_PORT')
cy.get('.bg-yellow-100').contains('INTERNAL_E2E_TESTING_SELF')
cy.get('.bg-yellow-100').contains('INTERNAL_GRAPHQL_PORT')
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/components/SpecPatterns.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const props = defineProps<{
}>()
const specPatterns = computed<string[]>(() => {
let patterns = props.gql.config.find((x) => x.field === props.gql.currentTestingType)?.value?.specPattern
const patterns = props.gql.config.find((x) => x.field === 'specPattern')?.value
if (!patterns) {
return []
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/settings/project/ConfigCode.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('<ConfigCode />', () => {
}} />
</div>))

const expectedText = `{${Object.entries(objectTest).map(([key, value]) => `'${key}': '${value}'`).join(',')},}`
const expectedText = `{${Object.entries(objectTest).map(([key, value]) => `${key}: '${value}'`).join(',')},}`

cy.contains(`objectTest:`).should('contain.text', expectedText)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<span
:class="props.colorClasses"
:style="`margin-left:${(props.depth + 1) * 24}px`"
>'{{ k }}': </span><RenderObject
>{{ k }}: </span><RenderObject
v-if="typeof val === 'object'"
:record-key="k"
:value="val"
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/specs/NoSpecsPage.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ describe('<NoSpecsPage />', { viewportHeight: 655, viewportWidth: 1032 }, () =>
onResult: (res) => {
if (res.currentProject?.config) {
res.currentProject.config = res.currentProject.config.map((x) => {
if (x.field === 'e2e') {
return { ...x, value: { ...x.value, specPattern: customSpecPattern } }
if (x.field === 'specPattern') {
return { ...x, value: customSpecPattern }
}

return x
Expand Down
3 changes: 2 additions & 1 deletion packages/config/__snapshots__/index.spec.ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,6 @@ exports['src/index .getPublicConfigKeys returns list of public config keys 1'] =
"browsers",
"hosts",
"isInteractive",
"modifyObstructiveCode"
"modifyObstructiveCode",
"specPattern"
]
5 changes: 3 additions & 2 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import _ from 'lodash'
import Debug from 'debug'
import { options, breakingOptions, breakingRootOptions, testingTypeBreakingOptions } from './options'
import { defaultSpecPattern, options, breakingOptions, breakingRootOptions, testingTypeBreakingOptions, additionalOptionsToResolveConfig } from './options'
import type { BreakingOption, BreakingOptionErrorKey } from './options'

// this export has to be done in 2 lines because of a bug in babel typescript
import * as validation from './validation'

export {
defaultSpecPattern,
validation,
options,
breakingOptions,
Expand All @@ -31,7 +32,7 @@ function createIndex<T extends Record<string, any>> (arr: Array<T>, keyKey: keyo

const breakingKeys = _.map(breakingOptions, 'name')
const defaultValues = createIndex(options, 'name', 'defaultValue')
const publicConfigKeys = _(options).reject({ isInternal: true }).map('name').value()
const publicConfigKeys = _([...options, ...additionalOptionsToResolveConfig]).reject({ isInternal: true }).map('name').value()
const validationRules = createIndex(options, 'name', 'validation')
const testConfigOverrideOptions = createIndex(options, 'name', 'canUpdateDuringTestTime')

Expand Down
28 changes: 26 additions & 2 deletions packages/config/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ const isValidConfig = (key: string, config: any) => {
return true
}

export const defaultSpecPattern = {
e2e: 'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}',
component: '**/*.cy.{js,jsx,ts,tsx}',
}

// NOTE:
// If you add/remove/change a config value, make sure to update the following
// - cli/types/index.d.ts (including allowed config options on TestOptions)
Expand Down Expand Up @@ -141,7 +146,7 @@ const resolvedOptions: Array<ResolvedConfigOption> = [
name: 'component',
// runner-ct overrides
defaultValue: {
specPattern: '**/*.cy.{js,jsx,ts,tsx}',
specPattern: defaultSpecPattern.component,
},
validation: isValidConfig,
canUpdateDuringTestTime: false,
Expand All @@ -160,7 +165,7 @@ const resolvedOptions: Array<ResolvedConfigOption> = [
name: 'e2e',
// e2e runner overrides
defaultValue: {
specPattern: 'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}',
specPattern: defaultSpecPattern.e2e,
},
validation: isValidConfig,
canUpdateDuringTestTime: false,
Expand Down Expand Up @@ -500,6 +505,16 @@ const runtimeOptions: Array<RuntimeConfigOption> = [
validation: validate.isString,
isInternal: true,
canUpdateDuringTestTime: false,
}, {
// Internal config field, useful to ignore the e2e specPattern set by the user
// or the default one when looking fot CT, it needs to be a config property because after
// having the final config that has the e2e property flattened/compacted
// we may not be able to get the value to ignore.
name: 'additionalIgnorePattern',
defaultValue: (options: Record<string, any> = {}) => options.testingType === 'component' ? defaultSpecPattern.e2e : undefined,
validation: validate.isString,
isInternal: true,
canUpdateDuringTestTime: false,
},
]

Expand All @@ -508,6 +523,15 @@ export const options: Array<ResolvedConfigOption | RuntimeConfigOption> = [
...runtimeOptions,
]

// These properties are going to be added to the resolved properties of the
// config, but do not mean that are valid config properties coming from the user.
export const additionalOptionsToResolveConfig = [
{
name: 'specPattern',
isInternal: false,
},
]

/**
* Values not allowed in 10.X+ in the root, e2e and component config
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/data-context/src/actions/ProjectActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,9 @@ export class ProjectActions {
await this.setSpecsFoundBySpecPattern({
path: this.ctx.currentProject,
testingType,
specPattern: cfg[testingType]?.specPattern,
excludeSpecPattern: cfg[testingType]?.excludeSpecPattern,
additionalIgnorePattern: testingType === 'component' ? cfg?.e2e?.specPattern : undefined,
specPattern: cfg.specPattern,
excludeSpecPattern: cfg.excludeSpecPattern,
additionalIgnorePattern: cfg.additionalIgnorePattern,
})
}

Expand Down
8 changes: 4 additions & 4 deletions packages/data-context/src/data/ProjectLifecycleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1286,13 +1286,13 @@ export class ProjectLifecycleManager {

this._pendingInitialize?.resolve(finalConfig)

if (this._currentTestingType && finalConfig[this._currentTestingType]?.specPattern) {
if (this._currentTestingType && finalConfig.specPattern) {
return this.ctx.actions.project.setSpecsFoundBySpecPattern({
path: this.projectRoot,
testingType: this._currentTestingType,
specPattern: this.ctx.modeOptions.spec || finalConfig[this._currentTestingType]?.specPattern,
excludeSpecPattern: finalConfig[this._currentTestingType]?.excludeSpecPattern,
additionalIgnorePattern: this._currentTestingType === 'component' ? finalConfig.e2e?.specPattern : undefined,
specPattern: this.ctx.modeOptions.spec || finalConfig.specPattern,
excludeSpecPattern: finalConfig.excludeSpecPattern,
additionalIgnorePattern: finalConfig.additionalIgnorePattern,
})
}

Expand Down
16 changes: 8 additions & 8 deletions packages/data-context/src/sources/ProjectDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import path from 'path'
import Debug from 'debug'
import commonPathPrefix from 'common-path-prefix'
import type { FSWatcher } from 'chokidar'
import { defaultSpecPattern } from '@packages/config'
import parseGlob from 'parse-glob'
import mm from 'micromatch'
import RandExp from 'randexp'
Expand All @@ -19,7 +20,6 @@ import type { DataContext } from '..'
import { toPosix } from '../util/file'
import type { FilePartsShape } from '@packages/graphql/src/schemaTypes/objectTypes/gql-FileParts'
import { STORIES_GLOB } from '.'
import { getDefaultSpecPatterns } from '../util/config-options'

export type SpecWithRelativeRoot = FoundSpec & { relativeToCommonRoot: string }

Expand Down Expand Up @@ -198,7 +198,7 @@ export class ProjectDataSource {
this.ctx.coreData.app.relaunchBrowser = relaunchBrowser
}

async specPatternsForTestingType (projectRoot: string, testingType: Cypress.TestingType): Promise<{
async specPatterns (): Promise<{
specPattern?: string[]
excludeSpecPattern?: string[]
}> {
Expand All @@ -207,8 +207,8 @@ export class ProjectDataSource {
const config = await this.getConfig()

return {
specPattern: toArray(config[testingType]?.specPattern),
excludeSpecPattern: toArray(config[testingType]?.excludeSpecPattern),
specPattern: toArray(config.specPattern),
excludeSpecPattern: toArray(config.excludeSpecPattern),
}
}

Expand Down Expand Up @@ -272,7 +272,7 @@ export class ProjectDataSource {
}

let specPatternSet: string | undefined
const { specPattern = [] } = await this.ctx.project.specPatternsForTestingType(this.ctx.currentProject, this.ctx.coreData.currentTestingType)
const { specPattern = [] } = await this.ctx.project.specPatterns()

if (Array.isArray(specPattern)) {
specPatternSet = specPattern[0]
Expand Down Expand Up @@ -301,7 +301,7 @@ export class ProjectDataSource {

const MINIMATCH_OPTIONS = { dot: true, matchBase: true }

const { specPattern = [], excludeSpecPattern = [] } = await this.ctx.project.specPatternsForTestingType(this.ctx.currentProject, this.ctx.coreData.currentTestingType)
const { specPattern = [], excludeSpecPattern = [] } = await this.ctx.project.specPatterns()

for (const pattern of excludeSpecPattern) {
if (minimatch(specFile, pattern, MINIMATCH_OPTIONS)) {
Expand Down Expand Up @@ -415,9 +415,9 @@ export class ProjectDataSource {
assert(this.ctx.currentProject)
assert(this.ctx.coreData.currentTestingType)

const { e2e, component } = getDefaultSpecPatterns()
const { e2e, component } = defaultSpecPattern

const { specPattern } = await this.ctx.project.specPatternsForTestingType(this.ctx.currentProject, this.ctx.coreData.currentTestingType)
const { specPattern } = await this.ctx.project.specPatterns()

if (this.ctx.coreData.currentTestingType === 'e2e') {
return isEqual(specPattern, [e2e])
Expand Down
8 changes: 0 additions & 8 deletions packages/data-context/src/util/config-options.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/data-context/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
export * from './autoBindDebug'
export * from './cached'
export * from './config-file-updater'
export * from './config-options'
export * from './file'
export * from './urqlCacheKeys'
14 changes: 7 additions & 7 deletions packages/data-context/test/unit/config-file-updater.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ describe('lib/util/config-file-updater', () => {
}
`

const output = await insertValueInJSString(src, { component: { specFilePattern: 'src/**/*.spec.cy.js' } }, errors)
const output = await insertValueInJSString(src, { component: { specPattern: 'src/**/*.spec.cy.js' } }, errors)

const expectedOutput = stripIndent`\
module.exports = {
component: {
specFilePattern: 'src/**/*.spec.cy.js',
specPattern: 'src/**/*.spec.cy.js',
},
foo: 42
}
Expand All @@ -279,12 +279,12 @@ describe('lib/util/config-file-updater', () => {
}
`

const output = await insertValueInJSString(src, { component: { specFilePattern: 'src/**/*.spec.cy.js' } }, errors)
const output = await insertValueInJSString(src, { component: { specPattern: 'src/**/*.spec.cy.js' } }, errors)

const expectedOutput = stripIndent`\
module.exports = {
'component': {
specFilePattern: 'src/**/*.spec.cy.js',
specPattern: 'src/**/*.spec.cy.js',
viewportWidth: 800
},
foo: 42
Expand All @@ -299,18 +299,18 @@ describe('lib/util/config-file-updater', () => {
module.exports = {
foo: 42,
component: {
specFilePattern: 'components/**/*.spec.cy.js',
specPattern: 'components/**/*.spec.cy.js',
foo: 82
}
}`

const output = await insertValueInJSString(src, { component: { specFilePattern: 'src/**/*.spec.cy.js' } }, errors)
const output = await insertValueInJSString(src, { component: { specPattern: 'src/**/*.spec.cy.js' } }, errors)

const expectedOutput = stripIndent`\
module.exports = {
foo: 42,
component: {
specFilePattern: 'src/**/*.spec.cy.js',
specPattern: 'src/**/*.spec.cy.js',
foo: 82
}
}`
Expand Down
Loading

3 comments on commit 61f7cfc

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 61f7cfc Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.0.0/linux-x64/10.0-release-61f7cfc59284a2938e0a1c15d74ee75215ba5f8b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 61f7cfc Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.0.0/darwin-x64/10.0-release-61f7cfc59284a2938e0a1c15d74ee75215ba5f8b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 61f7cfc Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.0.0/win32-x64/10.0-release-61f7cfc59284a2938e0a1c15d74ee75215ba5f8b/cypress.tgz

Please sign in to comment.