Skip to content

Commit a94c9d5

Browse files
lmiller1990elevatebartJessicaSachs
authored
feat: support ct/e2e specific overrides in cypress.json (#15444)
Co-authored-by: ElevateBart <ledouxb@gmail.com> Co-authored-by: Jessica Sachs <jess@jessicasachs.io>
1 parent 2871b19 commit a94c9d5

File tree

22 files changed

+416
-296
lines changed

22 files changed

+416
-296
lines changed

cli/schema/cypress.schema.json

+288-275
Large diffs are not rendered by default.

cli/types/cypress.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ declare namespace Cypress {
88
type RequestBody = string | object
99
type ViewportOrientation = 'portrait' | 'landscape'
1010
type PrevSubject = 'optional' | 'element' | 'document' | 'window'
11-
type PluginConfig = (on: PluginEvents, config: PluginConfigOptions) => void | ConfigOptions | Promise<ConfigOptions>
11+
type PluginConfig = (on: PluginEvents, config: PluginConfigOptions, mode: 'e2e' | 'component') => void | ConfigOptions | Promise<ConfigOptions>
1212

1313
interface CommandOptions {
1414
prevSubject: boolean | PrevSubject | PrevSubject[]

npm/react/cypress/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ const webpackConfig = {
6060
/**
6161
* @type Cypress.PluginConfig
6262
*/
63-
module.exports = (on, config) => {
63+
module.exports = (on, config, mode) => {
64+
if (mode !== 'component') {
65+
throw Error('This is an component project. mode should be `component`.')
66+
}
67+
6468
on('dev-server:start', (options) => {
6569
return startDevServer({ options, webpackConfig, disableLazyCompilation: false })
6670
})

npm/vue/cypress/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ const webpackConfig = require('../../webpack.config')
55
/**
66
* @type Cypress.PluginConfig
77
*/
8-
module.exports = (on, config) => {
8+
module.exports = (on, config, mode) => {
9+
if (mode !== 'component') {
10+
throw Error('This is a component testing project. mode should be `component`.')
11+
}
12+
913
require('@cypress/code-coverage/task')(on, config)
1014
on('dev-server:start', (options) => startDevServer({ options, webpackConfig }))
1115

packages/server-ct/src/project-ct.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class ProjectCt extends ProjectBase<ServerCt> {
4747
this.server.socket.changeToUrl(targetUrl)
4848
}
4949

50-
open (options) {
50+
open (options: Record<string, unknown>) {
5151
this._server = new ServerCt()
5252

5353
return super.open(options, {
@@ -85,6 +85,7 @@ export class ProjectCt extends ProjectBase<ServerCt> {
8585
return plugins.init(allowedCfg, {
8686
projectRoot: this.projectRoot,
8787
configFile: settings.pathToConfigFile(this.projectRoot, options),
88+
mode: options.mode,
8889
})
8990
.then((modifiedCfg) => {
9091
debug('plugin config yielded: %o', modifiedCfg)

packages/server/lib/open_project.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ const moduleFactory = () => {
328328
debug('opening project %s', path)
329329
debug('and options %o', options)
330330

331-
return openProject.open(options)
331+
return openProject.open({ ...options, mode: args.testingType })
332332
.return(this)
333333
},
334334
}

packages/server/lib/plugins/child/run_plugins.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ const getDefaultPreprocessor = function (config) {
3737

3838
let plugins
3939

40-
const load = (ipc, config, pluginsFile) => {
40+
/**
41+
* @param {EventEmitter} ipc
42+
* @param {object} config
43+
* @param {string} pluginsFile
44+
* @param {'component' | 'e2e'} string
45+
*/
46+
const load = (ipc, config, pluginsFile, mode) => {
4147
debug('run plugins function')
4248

4349
let eventIdCount = 0
@@ -87,7 +93,7 @@ const load = (ipc, config, pluginsFile) => {
8793
.try(() => {
8894
debug('run plugins function')
8995

90-
return plugins(register, config)
96+
return plugins(register, config, mode)
9197
})
9298
.tap(() => {
9399
if (!registeredEventsByName['file:preprocessor']) {
@@ -192,10 +198,10 @@ const runPlugins = (ipc, pluginsFile, projectRoot) => {
192198
return
193199
}
194200

195-
ipc.on('load', (config) => {
201+
ipc.on('load', (config, mode) => {
196202
debug('plugins load file "%s"', pluginsFile)
197203
debug('passing config %o', config)
198-
load(ipc, config, pluginsFile)
204+
load(ipc, config, pluginsFile, mode)
199205
})
200206

201207
ipc.on('execute', (event, ids, args) => {

packages/server/lib/plugins/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const init = (config, options) => {
113113
version: pkg.version,
114114
})
115115

116-
ipc.send('load', config)
116+
ipc.send('load', config, options.mode)
117117

118118
ipc.on('loaded', (newCfg, registrations) => {
119119
_.omit(config, 'projectRoot', 'configFile')

packages/server/lib/project-e2e.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class ProjectE2E extends ProjectBase<ServerE2E> {
1414
return 'e2e'
1515
}
1616

17-
open (options) {
17+
open (options: Record<string, unknown>) {
1818
this._server = new ServerE2E()
1919

2020
return super.open(options, {
@@ -59,6 +59,7 @@ export class ProjectE2E extends ProjectBase<ServerE2E> {
5959
return plugins.init(cfg, {
6060
projectRoot: this.projectRoot,
6161
configFile: settings.pathToConfigFile(this.projectRoot, options),
62+
mode: options.mode,
6263
onError (err) {
6364
debug('got plugins error', err.stack)
6465

packages/server/lib/util/settings.js

+14
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ module.exports = {
9393
}, _.cloneDeep(obj))
9494
},
9595

96+
isComponentTesting (options = {}) {
97+
return options.experimentalComponentTesting || options.componentTesting || options.testingType === 'component'
98+
},
99+
96100
configFile (options = {}) {
97101
return options.configFile === false ? false : (options.configFile || 'cypress.json')
98102
},
@@ -151,6 +155,16 @@ module.exports = {
151155
.catch({ code: 'ENOENT' }, () => {
152156
return this._write(file, {})
153157
}).then((json = {}) => {
158+
if (this.isComponentTesting(options) && 'component' in json) {
159+
json = { ...json, ...json.component }
160+
delete json.component
161+
}
162+
163+
if (!this.isComponentTesting(options) && 'e2e' in json) {
164+
json = { ...json, ...json.e2e }
165+
delete json.e2e
166+
}
167+
154168
const changed = this._applyRewriteRules(json)
155169

156170
// if our object is unchanged

packages/server/test/integration/plugins_spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('lib/plugins', () => {
2626

2727
const options = {
2828
onWarning,
29+
mode: 'e2e',
2930
}
3031

3132
return plugins.init(projectConfig, options)

packages/server/test/support/fixtures/projects/chrome-browser-preferences/cypress/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ const { expect } = require('chai')
33
const fse = require('fs-extra')
44
const path = require('path')
55

6-
module.exports = (on, config) => {
6+
module.exports = (on, config, mode) => {
7+
if (mode !== 'e2e') {
8+
throw Error('This is an e2e project. mode should be `e2e`.')
9+
}
10+
711
const parentPid = process.ppid
812
let { PATH_TO_CHROME_PROFILE } = config.env
913

packages/server/test/support/fixtures/projects/component-tests/cypress/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ const webpackConfig = {
1313
/**
1414
* @type Cypress.PluginConfig
1515
*/
16-
module.exports = (on, config) => {
16+
module.exports = (on, config, mode) => {
17+
if (mode !== 'e2e') {
18+
throw Error('This is an e2e project. mode should be `e2e`.')
19+
}
20+
1721
require('@cypress/code-coverage/task')(on, config)
1822
on('dev-server:start', (options) => startDevServer({ options, webpackConfig }))
1923

packages/server/test/support/fixtures/projects/e2e/cypress/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ const { useFixedFirefoxResolution } = require('../../../utils')
1111
/**
1212
* @type {Cypress.PluginConfig}
1313
*/
14-
module.exports = (on, config) => {
14+
module.exports = (on, config, mode) => {
15+
if (mode !== 'e2e') {
16+
throw Error('This is an e2e project. mode should be `e2e`.')
17+
}
18+
1519
let performance = {
1620
track: () => Promise.resolve(),
1721
}

packages/server/test/support/fixtures/projects/firefox-memory/cypress/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ let timings = []
2020
let rss = []
2121
let intervalId
2222

23-
module.exports = (on, config) => {
23+
module.exports = (on, config, mode) => {
24+
if (mode !== 'e2e') {
25+
throw Error('This is an e2e project. mode should be `e2e`.')
26+
}
27+
2428
on('task', {
2529
'console' (...args) {
2630
console.log(...args)

packages/server/test/support/fixtures/projects/plugin-before-browser-launch-deprecation/cypress/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ const getHandlersByType = (type) => {
109109
}
110110
}
111111

112-
module.exports = (on, config) => {
112+
module.exports = (on, config, mode) => {
113+
if (mode !== 'e2e') {
114+
throw Error('This is an e2e project. mode should be `e2e`.')
115+
}
116+
113117
const beforeBrowserLaunchHandler = config.env.BEFORE_BROWSER_LAUNCH_HANDLER
114118

115119
if (!beforeBrowserLaunchHandler) {

packages/server/test/support/fixtures/projects/plugin-config-version/cypress/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const semver = require('semver')
22

3-
module.exports = (on, config) => {
3+
module.exports = (on, config, mode) => {
4+
if (mode !== 'e2e') {
5+
throw Error('This is an e2e project. mode should be `e2e`.')
6+
}
7+
48
if (!semver.valid(config.version)) {
59
throw new Error('config.version is invalid')
610
}

packages/server/test/support/fixtures/projects/plugin-config/cypress/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
module.exports = (on, config) => {
1+
module.exports = (on, config, mode) => {
2+
if (mode !== 'e2e') {
3+
throw Error('This is an e2e project. mode should be `e2e`.')
4+
}
5+
26
return new Promise((resolve) => {
37
setTimeout(resolve, 100)
48
})

packages/server/test/support/fixtures/projects/plugin-event-deprecated/cypress/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
module.exports = (on, config) => {
1+
module.exports = (on, config, mode) => {
2+
if (mode !== 'e2e') {
3+
throw Error('This is an e2e project. mode should be `e2e`.')
4+
}
5+
26
if (config.env.NO_MUTATE_RETURN) {
37
on('before:browser:launch', (browser, options) => {
48
// this will emit a warning

packages/server/test/support/fixtures/projects/read-only-project-root/cypress/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const fs = require('fs')
22
const { expect } = require('chai')
33

4-
module.exports = (on, config) => {
4+
module.exports = (on, config, mode) => {
5+
if (mode !== 'e2e') {
6+
throw Error('This is an e2e project. mode should be `e2e`.')
7+
}
8+
59
expect(process.geteuid()).to.not.eq(0)
610
console.log('✅ not running as root')
711

packages/server/test/support/fixtures/projects/retries-2/cypress/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ const { useFixedFirefoxResolution } = require('../../../utils')
55
/**
66
* @type {Cypress.PluginConfig}
77
*/
8-
module.exports = (on, config) => {
8+
module.exports = (on, config, mode) => {
9+
if (mode !== 'e2e') {
10+
throw Error('This is an e2e project. mode should be `e2e`.')
11+
}
12+
913
on('before:browser:launch', (browser, options) => {
1014
useFixedFirefoxResolution(browser, options, config)
1115

packages/server/test/unit/settings_spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,42 @@ describe('lib/settings', () => {
111111
})
112112
})
113113

114+
it('promises cypress.json and merges CT specific properties for via testingType: component', function () {
115+
return this.setup({ a: 'b', component: { a: 'c' } })
116+
.then(() => {
117+
return settings.read(projectRoot, { testingType: 'component' })
118+
}).then((obj) => {
119+
expect(obj).to.deep.eq({ a: 'c' })
120+
})
121+
})
122+
123+
it('promises cypress.json and merges CT specific properties for via componentTesting: true', function () {
124+
return this.setup({ a: 'b', component: { a: 'c' } })
125+
.then(() => {
126+
return settings.read(projectRoot, { componentTesting: true })
127+
}).then((obj) => {
128+
expect(obj).to.deep.eq({ a: 'c' })
129+
})
130+
})
131+
132+
it('promises cypress.json and merges CT specific properties for via experimentalComponentTesting: true', function () {
133+
return this.setup({ a: 'b', component: { a: 'c' } })
134+
.then(() => {
135+
return settings.read(projectRoot, { experimentalComponentTesting: true })
136+
}).then((obj) => {
137+
expect(obj).to.deep.eq({ a: 'c' })
138+
})
139+
})
140+
141+
it('promises cypress.json and merges e2e specific properties', function () {
142+
return this.setup({ a: 'b', e2e: { a: 'c' } })
143+
.then(() => {
144+
return settings.read(projectRoot)
145+
}).then((obj) => {
146+
expect(obj).to.deep.eq({ a: 'c' })
147+
})
148+
})
149+
114150
it('renames commandTimeout -> defaultCommandTimeout', function () {
115151
return this.setup({ commandTimeout: 30000, foo: 'bar' })
116152
.then(() => {

0 commit comments

Comments
 (0)