From 8f585cada9d840f508ff06d816139846441c2a2c Mon Sep 17 00:00:00 2001 From: Veaceslav <118342408+vCaisim@users.noreply.github.com> Date: Mon, 26 Jun 2023 23:09:44 +0300 Subject: [PATCH] [CSR-594] Add ability to only include trace for failing tests (#47) * feat: add ability to only include trace for failing tests * feat: implement failedTestsOnly option --------- Co-authored-by: Andrew Goldis --- README.md | 29 ++++++++++++++++++++----- apps/web/cypress.config.js | 2 +- packages/plugin/src/install.ts | 13 ++++++++++- packages/plugin/src/types.ts | 1 + packages/support/src/cy/testHandlers.ts | 13 +++++++++++ 5 files changed, 50 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 35ffcf8..eef7d6d 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ const { debuggerPlugin } = require('cypress-debugger'); module.exports = defineConfig({ e2e: { setupNodeEvents(on, config) { - debuggerPlugin(on, { + debuggerPlugin(on, config, { meta: { key: 'value', }, @@ -98,14 +98,16 @@ See an example in [apps/web](https://github.com/currents-dev/cypress-debugger//b Installs cypress-debugger. ```ts -debuggerPlugin(on: Cypress.PluginEvents, options?: PluginOptions): void +debuggerPlugin(on: Cypress.PluginEvents, config: Cypress.PluginConfig, options?: PluginOptions): void ``` -- `on` - [`Cypress.PluginEvents`](https://docs.cypress.io/guides/tooling/plugins-guide) `setupNodeEvents` method first argument +- `on` - [`Cypress.PluginEvents`](https://docs.cypress.io/guides/references/configuration#setupNodeEvents) `setupNodeEvents` method first argument +- `on` - [`Cypress.PluginConfig`](https://docs.cypress.io/guides/references/configuration#setupNodeEvents) `setupNodeEvents` method second argument - `options` - [`PluginOptions`](./packages/plugin/src/types.ts): - `meta: Record`: an optional field that is added to the `TestExecutionResult` as `pluginMeta` - `callback: (path: string, data: TestExecutionResult`: a callback function that will be called after each test - `targetDirectory: string`: the path to the reports directory. Default is `dump` + - `failedTestsOnly: boolean`: whether to generate debug traces for failed tests only, default is `false` Example: @@ -116,7 +118,7 @@ const { debuggerPlugin } = require('cypress-debugger'); module.exports = defineConfig({ e2e: { setupNodeEvents(on, config) { - debuggerPlugin(on, { + return debuggerPlugin(on, config, { meta: { key: 'value', }, @@ -125,7 +127,22 @@ module.exports = defineConfig({ }, targetDirectory: 'cypress/e2e/reports', }); - return config; + }, + }, +}); +``` + +In order to generate traces for failing tests only, set the `failedTestsOnly` configuration to `true` + +Example: + +```js +module.exports = defineConfig({ + e2e: { + setupNodeEvents(on, config) { + return debuggerPlugin(on, config, { + failedTestsOnly: true, + }); }, }, }); @@ -135,7 +152,7 @@ module.exports = defineConfig({ Attaches required handlers to [Cypress events](https://docs.cypress.io/api/cypress-api/catalog-of-events) -```ts +```typescript debuggerSupport(): void ``` diff --git a/apps/web/cypress.config.js b/apps/web/cypress.config.js index fe47c6a..2d0b450 100644 --- a/apps/web/cypress.config.js +++ b/apps/web/cypress.config.js @@ -7,7 +7,7 @@ module.exports = defineConfig({ specPattern: 'cypress/e2e/*.spec.js', supportFile: 'cypress/support/e2e.ts', setupNodeEvents(on, config) { - debuggerPlugin(on, { + debuggerPlugin(on, config, { meta: { key: 'value', }, diff --git a/packages/plugin/src/install.ts b/packages/plugin/src/install.ts index 2d8339f..57629ec 100644 --- a/packages/plugin/src/install.ts +++ b/packages/plugin/src/install.ts @@ -36,7 +36,16 @@ const getHar = (filename: string): HttpArchiveLog | null => { } }; -function install(on: Cypress.PluginEvents, options?: PluginOptions) { +function install( + on: Cypress.PluginEvents, + config: Cypress.PluginConfigOptions, + options?: PluginOptions +) { + if (options?.failedTestsOnly) { + // eslint-disable-next-line no-underscore-dangle, no-param-reassign + config.env.__cypress_debugger_failedTestsOnly = true; + } + on('task', { // called in "afterEach" hook dumpEvents( @@ -97,6 +106,8 @@ function install(on: Cypress.PluginEvents, options?: PluginOptions) { on('before:spec', async () => { await recordLogs(); }); + + return config; } export default install; diff --git a/packages/plugin/src/types.ts b/packages/plugin/src/types.ts index f0bcd48..cfdf69b 100644 --- a/packages/plugin/src/types.ts +++ b/packages/plugin/src/types.ts @@ -133,5 +133,6 @@ export type TestExecutionResult = { export type PluginOptions = { meta?: Record; targetDirectory?: string; + failedTestsOnly?: boolean; callback?: (path: string, result: TestExecutionResult) => void; }; diff --git a/packages/support/src/cy/testHandlers.ts b/packages/support/src/cy/testHandlers.ts index a767970..14facf9 100644 --- a/packages/support/src/cy/testHandlers.ts +++ b/packages/support/src/cy/testHandlers.ts @@ -23,6 +23,19 @@ export function handleAfterEach() { const eventsBatch = eventsContainer.getEvents(); const harFilename = `${eventsBatch.testId}.raw.json`; + const { state } = this.currentTest; + const reportFailedTestsOnly = !!Cypress.env( + '__cypress_debugger_failedTestsOnly' + ); + + if (state === 'passed' && reportFailedTestsOnly) { + cy.log('🎥 skipping trace generation for a non-failed test'); + // do not save the recorded network logs + cy.disposeOfHar(); + // do not save reports + return; + } + // create dump file for network data cy.saveHar({ outDir: 'dump_har',