Skip to content

Commit

Permalink
feat: Add before:spec event (#9646)
Browse files Browse the repository at this point in the history
Co-authored-by: Zach Bloomquist <[email protected]>
  • Loading branch information
chrisbreiding and flotwig authored Dec 16, 2020
1 parent 4ef189e commit f47d5c0
Show file tree
Hide file tree
Showing 25 changed files with 434 additions and 161 deletions.
5 changes: 5 additions & 0 deletions cli/schema/cypress.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@
"default": "bundled",
"description": "If set to 'system', Cypress will try to find a Node.js executable on your path to use when executing your plugins. Otherwise, Cypress will use the Node version bundled with Cypress."
},
"experimentalRunEvents": {
"type": "boolean",
"default": false,
"description": "Allows listening to the `before:spec` event in the plugins file."
},
"experimentalSourceRewriting": {
"type": "boolean",
"default": false,
Expand Down
5 changes: 5 additions & 0 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2569,6 +2569,11 @@ declare namespace Cypress {
* @default { runMode: 1, openMode: null }
*/
firefoxGcInterval: Nullable<number | { runMode: Nullable<number>, openMode: Nullable<number> }>
/**
* Allows listening to the `before:spec` event in the plugins file.
* @default false
*/
experimentalRunEvents: boolean
/**
* Enables AST-based JS/HTML rewriting. This may fix issues caused by the existing regex-based JS/HTML replacement
* algorithm.
Expand Down
79 changes: 3 additions & 76 deletions packages/server/__snapshots__/3_plugins_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,18 +384,12 @@ The following error was thrown by a plugin. We stopped running your tests becaus
│ Pending: 0 │
│ Skipped: 0 │
│ Screenshots: 0 │
│ Video: true
│ Video: false
│ Duration: X seconds │
│ Spec Ran: app_spec.js │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
(Video)
- Started processing: Compressing to 32 CRF
- Finished processing: /XXX/XXX/XXX/cypress/videos/app_spec.js.mp4 (X second)
====================================================================================================
(Run Finished)
Expand Down Expand Up @@ -437,81 +431,14 @@ The following validation error was thrown by your plugins file (\`/foo/bar/.proj
You passed: \`invalid:event\`
The following are valid events:
- file:preprocessor
- after:screenshot
- before:browser:launch
- file:preprocessor
- task
- after:screenshot
[stack trace lines]
`

exports['e2e plugins does not report more screenshots than exist if user overwrites screenshot in afterScreenshot hook 1'] = `
====================================================================================================
(Run Starting)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Cypress: 1.2.3 │
│ Browser: FooBrowser 88 │
│ Specs: 1 found (after_screenshot_overwrite_spec.js) │
│ Searched: cypress/integration/after_screenshot_overwrite_spec.js │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
────────────────────────────────────────────────────────────────────────────────────────────────────
Running: after_screenshot_overwrite_spec.js (1 of 1)
✓ cy.screenshot() - replacement
✓ cy.screenshot() - replacement
✓ cy.screenshot() - replacement
3 passing
(Results)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Tests: 3 │
│ Passing: 3 │
│ Failing: 0 │
│ Pending: 0 │
│ Skipped: 0 │
│ Screenshots: 1 │
│ Video: true │
│ Duration: X seconds │
│ Spec Ran: after_screenshot_overwrite_spec.js │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
(Screenshots)
- /XXX/XXX/XXX/screenshot-replacement.png (2x2)
(Video)
- Started processing: Compressing to 32 CRF
- Finished processing: /XXX/XXX/XXX/cypress/videos/after_screenshot_overwrite_spec (X second)
.js.mp4
====================================================================================================
(Run Finished)
Spec Tests Passing Failing Pending Skipped
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ✔ after_screenshot_overwrite_spec.js XX:XX 3 3 - - - │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
✔ All specs passed! XX:XX 3 3 - - -
`

exports['e2e plugins does not report more screenshots than exist if user overwrites previous screenshot in afterScreenshot 1'] = `
====================================================================================================
Expand Down
121 changes: 121 additions & 0 deletions packages/server/__snapshots__/4_plugin_run_events_spec.ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
exports['e2e plugin run events / sends server events'] = `
====================================================================================================
(Run Starting)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Cypress: 1.2.3 │
│ Browser: FooBrowser 88 │
│ Specs: 2 found (run_events_spec_1.js, run_events_spec_2.js) │
│ Searched: cypress/integration/* │
│ Experiments: experimentalRunEvents=true │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
────────────────────────────────────────────────────────────────────────────────────────────────────
Running: run_events_spec_1.js (1 of 2)
before:spec: cypress/integration/run_events_spec_1.js
before:spec is awaited
✓ is true
1 passing
(Results)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Tests: 1 │
│ Passing: 1 │
│ Failing: 0 │
│ Pending: 0 │
│ Skipped: 0 │
│ Screenshots: 0 │
│ Video: false │
│ Duration: X seconds │
│ Spec Ran: run_events_spec_1.js │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
────────────────────────────────────────────────────────────────────────────────────────────────────
Running: run_events_spec_2.js (2 of 2)
before:spec: cypress/integration/run_events_spec_2.js
before:spec is awaited
✓ is true
1 passing
(Results)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Tests: 1 │
│ Passing: 1 │
│ Failing: 0 │
│ Pending: 0 │
│ Skipped: 0 │
│ Screenshots: 0 │
│ Video: false │
│ Duration: X seconds │
│ Spec Ran: run_events_spec_2.js │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
====================================================================================================
(Run Finished)
Spec Tests Passing Failing Pending Skipped
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ✔ run_events_spec_1.js XX:XX 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ run_events_spec_2.js XX:XX 1 1 - - - │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
✔ All specs passed! XX:XX 2 2 - - -
`

exports['e2e plugin run events / fails run if server event handler throws'] = `
====================================================================================================
(Run Starting)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Cypress: 1.2.3 │
│ Browser: FooBrowser 88 │
│ Specs: 1 found (run_event_throws_spec.js) │
│ Searched: cypress/integration/* │
│ Experiments: experimentalRunEvents=true │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
────────────────────────────────────────────────────────────────────────────────────────────────────
Running: run_event_throws_spec.js (1 of 1)
An error was thrown in your plugins file while executing the handler for the 'before:spec' event.
The error we received was:
Error: error thrown in before:spec
[stack trace lines]
`

exports['e2e plugin run events / fails if experimentalRunEvents is not enabled'] = `
The following validation error was thrown by your plugins file (\`/foo/bar/.projects/plugin-run-events/cypress/plugins/index.js\`).
Error: The \`before:spec\` event requires the experimentalRunEvents flag to be enabled.
To enable it, set \`"experimentalRunEvents": true\` in your cypress.json
[stack trace lines]
`
11 changes: 8 additions & 3 deletions packages/server/lib/config_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,22 @@ export const options = [
defaultValue: 60000,
validation: v.isNumber,
}, {
name: 'experimentalSourceRewriting',
name: 'experimentalComponentTesting',
defaultValue: false,
validation: v.isBoolean,
isExperimental: true,
}, {
name: 'experimentalComponentTesting',
name: 'experimentalFetchPolyfill',
defaultValue: false,
validation: v.isBoolean,
isExperimental: true,
}, {
name: 'experimentalFetchPolyfill',
name: 'experimentalRunEvents',
defaultValue: false,
validation: v.isBoolean,
isExperimental: true,
}, {
name: 'experimentalSourceRewriting',
defaultValue: false,
validation: v.isBoolean,
isExperimental: true,
Expand Down
8 changes: 8 additions & 0 deletions packages/server/lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,14 @@ const getMsgByType = function (type, arg1 = {}, arg2, arg3) {
https://on.cypress.io/configuration
`
case 'PLUGINS_RUN_EVENT_ERROR':
return stripIndent`\
An error was thrown in your plugins file while executing the handler for the '${chalk.blue(arg1)}' event.
The error we received was:
${chalk.yellow(arg2)}
`
default:
}
}
Expand Down
8 changes: 5 additions & 3 deletions packages/server/lib/experiments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ interface StringValues {
```
*/
const _summaries: StringValues = {
experimentalComponentTesting: 'Framework-specific component testing, uses `componentFolder` to load component specs',
experimentalComponentTesting: 'Framework-specific component testing, uses `componentFolder` to load component specs.',
experimentalFetchPolyfill: 'Polyfills `window.fetch` to enable Network spying and stubbing.',
experimentalRunEvents: 'Allows listening to the `before:spec` event in the plugins file.',
experimentalSourceRewriting: 'Enables AST-based JS/HTML rewriting. This may fix issues caused by the existing regex-based JS/HTML replacement algorithm.',
experimentalFetchPolyfill: 'Polyfills `window.fetch` to enable Network spying and stubbing',
}

/**
Expand All @@ -68,8 +69,9 @@ const _summaries: StringValues = {
*/
const _names: StringValues = {
experimentalComponentTesting: 'Component Testing',
experimentalSourceRewriting: 'Improved source rewriting',
experimentalFetchPolyfill: 'Fetch polyfill',
experimentalRunEvents: 'Run Events',
experimentalSourceRewriting: 'Improved source rewriting',
}

/**
Expand Down
Loading

4 comments on commit f47d5c0

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f47d5c0 Dec 16, 2020

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/6.2.0/circle-develop-f47d5c05749c3ab9c2252f4377b561effb72c42b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f47d5c0 Dec 16, 2020

Choose a reason for hiding this comment

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

AppVeyor 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/6.2.0/appveyor-develop-f47d5c05749c3ab9c2252f4377b561effb72c42b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f47d5c0 Dec 16, 2020

Choose a reason for hiding this comment

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

AppVeyor has built the win32 ia32 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/6.2.0/appveyor-develop-f47d5c05749c3ab9c2252f4377b561effb72c42b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f47d5c0 Dec 16, 2020

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/6.2.0/circle-develop-f47d5c05749c3ab9c2252f4377b561effb72c42b/cypress.tgz

Please sign in to comment.