Skip to content

Commit 7783217

Browse files
Pass projectRoot and configFile to plugins file through config (#6317)
* decaffeinate: Rename index.coffee from .coffee to .js * decaffeinate: Convert index.coffee to JS * decaffeinate: Run post-processing cleanups on index.coffee * refactor decaffeinated plugins/index.js * decaffeinate: Rename 3_plugins_spec.coffee from .coffee to .js * decaffeinate: Convert 3_plugins_spec.coffee to JS * decaffeinate: Run post-processing cleanups on 3_plugins_spec.coffee * fix wrongly removed return * refactor e2e plugins spec, update snapshot * pass env argument to plugins file * decaffeinate: Rename index_spec.coffee from .coffee to .js * decaffeinate: Convert index_spec.coffee to JS * decaffeinate: Run post-processing cleanups on index_spec.coffee * update plugins tests * update scaffold snapshot * add back server test script and document running individual tests * add projectRoot and configFile directly to config * normalize browsers in snapshot * add types for configFile and projectRoot * fix linting issues * return return * Merge * remove file * remove unnecessary returns
1 parent e2ea5bf commit 7783217

File tree

13 files changed

+200
-495
lines changed

13 files changed

+200
-495
lines changed

cli/types/index.d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ declare namespace Cypress {
5252
type RequestBody = string | object
5353
type ViewportOrientation = "portrait" | "landscape"
5454
type PrevSubject = "optional" | "element" | "document" | "window"
55-
type PluginConfig = (on: PluginEvents, config: ConfigOptions) => void | Partial<ConfigOptions> | Promise<Partial<ConfigOptions>>
55+
type PluginConfig = (on: PluginEvents, config: PluginConfigOptions) => void | Partial<ConfigOptions> | Promise<Partial<ConfigOptions>>
5656

5757
interface CommandOptions {
5858
prevSubject: boolean | PrevSubject | PrevSubject[]
@@ -2282,6 +2282,17 @@ declare namespace Cypress {
22822282
firefoxGcInterval: Nullable<number | { runMode: Nullable<number>, openMode: Nullable<number> }>
22832283
}
22842284

2285+
interface PluginConfigOptions extends ConfigOptions {
2286+
/**
2287+
* Absolute path to the config file (default: <projectRoot>/cypress.json) or false
2288+
*/
2289+
configFile: string | false
2290+
/**
2291+
* Absolute path to the root of the project
2292+
*/
2293+
projectRoot: string
2294+
}
2295+
22852296
interface DebugOptions {
22862297
verbose: boolean
22872298
}

cli/types/tests/plugins-config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ const pluginConfig: Cypress.PluginConfig = (on, config) => {}
55

66
// allows synchronous returns
77
const pluginConfig2: Cypress.PluginConfig = (on, config) => {
8-
config // $ExpectType ConfigOptions
8+
config // $ExpectType PluginConfigOptions
99
config.baseUrl // $ExpectType: string
10+
config.configFile // $ExpectType: string | false
11+
config.projectRoot // $ExpectType: string
1012

1113
on('before:browser:launch', (browser, options) => {
1214
browser.displayName // $ExpectType string

packages/server/__snapshots__/3_plugins_spec.coffee.js

Lines changed: 0 additions & 404 deletions
This file was deleted.

packages/server/__snapshots__/3_plugins_spec.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
exports['e2e plugins passes 1'] = `
1+
exports['e2e plugins passes with working preprocessor 1'] = `
22
33
====================================================================================================
44
@@ -58,7 +58,7 @@ exports['e2e plugins passes 1'] = `
5858
5959
`
6060

61-
exports['e2e plugins fails 1'] = `
61+
exports['e2e plugins fails with async error 1'] = `
6262
6363
====================================================================================================
6464
@@ -175,13 +175,6 @@ exports['e2e plugins can modify config from plugins 1'] = `
175175
✔ All specs passed! XX:XX 2 2 - - -
176176
177177
178-
`
179-
180-
exports['e2e plugins catches invalid viewportWidth returned from plugins 1'] = `
181-
An invalid configuration value returned from the plugins file: \`cypress/plugins/index.coffee\`
182-
183-
Expected \`viewportWidth\` to be a number. Instead the value was: \`"foo"\`
184-
185178
`
186179

187180
exports['e2e plugins catches invalid browsers list returned from plugins 1'] = `
@@ -402,3 +395,10 @@ exports['e2e plugins calls after:screenshot for cy.screenshot() and failure scre
402395
403396
404397
`
398+
399+
exports['e2e plugins catches invalid viewportWidth returned from plugins 1'] = `
400+
An invalid configuration value returned from the plugins file: \`cypress/plugins/index.coffee\`
401+
402+
Expected \`viewportWidth\` to be a number. Instead the value was: \`"foo"\`
403+
404+
`

packages/server/lib/plugins/index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,16 @@ const init = (config, options) => {
6868
handler(ipc)
6969
}
7070

71+
_.extend(config, {
72+
projectRoot: options.projectRoot,
73+
configFile: options.configFile,
74+
})
75+
7176
ipc.send('load', config)
7277

7378
ipc.on('loaded', (newCfg, registrations) => {
79+
_.omit(config, 'projectRoot', 'configFile')
80+
7481
_.each(registrations, (registration) => {
7582
debug('register plugins process event', registration.event, 'with id', registration.eventId)
7683

@@ -105,9 +112,7 @@ const init = (config, options) => {
105112

106113
const handleError = (err) => {
107114
debug('plugins process error:', err.stack)
108-
if (!pluginsProcess) {
109-
return // prevent repeating this in case of multiple errors
110-
}
115+
if (!pluginsProcess) return // prevent repeating this in case of multiple errors
111116

112117
killPluginsProcess()
113118
err = errors.get('PLUGINS_ERROR', err.annotated || err.stack || err.message)
@@ -116,11 +121,9 @@ const init = (config, options) => {
116121
return options.onError(err)
117122
}
118123

119-
const handleWarning = function (warningErr) {
124+
const handleWarning = (warningErr) => {
120125
debug('plugins process warning:', warningErr.stack)
121-
if (!pluginsProcess) {
122-
return // prevent repeating this in case of multiple warnings
123-
}
126+
if (!pluginsProcess) return // prevent repeating this in case of multiple warnings
124127

125128
return options.onWarning(warningErr)
126129
}

packages/server/lib/project.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ class Project extends EE {
159159
cfg = config.whitelist(cfg)
160160

161161
return plugins.init(cfg, {
162+
projectRoot: this.projectRoot,
163+
configFile: settings.pathToConfigFile(this.projectRoot, options),
162164
onError (err) {
163165
debug('got plugins error', err.stack)
164166

packages/server/test/e2e/3_plugins_spec.js

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path')
33
const e2e = require('../support/helpers/e2e')
44
const Fixtures = require('../support/helpers/fixtures')
55

6+
const e2eProject = Fixtures.projectPath('e2e')
67
const pluginExtension = Fixtures.projectPath('plugin-extension')
78
const pluginConfig = Fixtures.projectPath('plugin-config')
89
const pluginFilterBrowsers = Fixtures.projectPath('plugin-filter-browsers')
@@ -14,10 +15,10 @@ const pluginReturnsBadConfig = Fixtures.projectPath('plugin-returns-bad-config')
1415
const pluginReturnsEmptyBrowsersList = Fixtures.projectPath('plugin-returns-empty-browsers-list')
1516
const pluginReturnsInvalidBrowser = Fixtures.projectPath('plugin-returns-invalid-browser')
1617

17-
describe('e2e plugins', () => {
18+
describe('e2e plugins', function () {
1819
e2e.setup()
1920

20-
it('passes', function () {
21+
it('passes with working preprocessor', function () {
2122
return e2e.exec(this, {
2223
spec: 'app_spec.coffee',
2324
project: workingPreprocessor,
@@ -26,7 +27,7 @@ describe('e2e plugins', () => {
2627
})
2728
})
2829

29-
it('fails', function () {
30+
it('fails with async error', function () {
3031
return e2e.exec(this, {
3132
spec: 'app_spec.coffee',
3233
project: pluginsAsyncError,
@@ -121,4 +122,44 @@ describe('e2e plugins', () => {
121122
expectedExitCode: 1,
122123
})
123124
})
125+
126+
describe('projectRoot and configFile', function () {
127+
it('passes projectRoot and default configFile to plugins function', function () {
128+
return e2e.exec(this, {
129+
spec: 'plugins_config_extras_spec.js',
130+
config: {
131+
env: {
132+
projectRoot: e2eProject,
133+
configFile: path.join(e2eProject, 'cypress.json'),
134+
},
135+
},
136+
})
137+
})
138+
139+
it('passes custom configFile to plugins function', function () {
140+
return e2e.exec(this, {
141+
spec: 'plugins_config_extras_spec.js',
142+
configFile: 'cypress-alt.json',
143+
config: {
144+
env: {
145+
projectRoot: e2eProject,
146+
configFile: path.join(e2eProject, 'cypress-alt.json'),
147+
},
148+
},
149+
})
150+
})
151+
152+
it('passes false configFile to plugins function', function () {
153+
return e2e.exec(this, {
154+
spec: 'plugins_config_extras_spec.js',
155+
configFile: 'false',
156+
config: {
157+
env: {
158+
projectRoot: e2eProject,
159+
configFile: false,
160+
},
161+
},
162+
})
163+
})
164+
})
124165
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
describe('plugins config extras', () => {
2+
it('has correct projectRoot', () => {
3+
cy.task('get:config:value', 'projectRoot')
4+
.should('not.be.undefined')
5+
.and('equal', Cypress.env('projectRoot'))
6+
})
7+
8+
it('has correct configFile', () => {
9+
cy.task('get:config:value', 'configFile')
10+
.should('not.be.undefined')
11+
.and('equal', Cypress.env('configFile'))
12+
})
13+
})

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,9 @@ module.exports = (on, config) => {
151151
'get:browser:args' () {
152152
return browserArgs
153153
},
154+
155+
'get:config:value' (key) {
156+
return config[key]
157+
},
154158
})
155159
}

0 commit comments

Comments
 (0)