|
| 1 | +# :warning: On event handlers |
| 2 | + |
| 3 | +This plugin will register multiple types of life-cycle event handlers. However, only a single event handler can exist for each type of event (due to a limitation in Cypress). Thus, if you attempt to define any of the same handlers, your handler will either be overriden by this plugin or you will override this plugin's handler. Either way, the behavior will be surprising. |
| 4 | + |
| 5 | +There's an (multiple actually) open [issue](https://github.com/cypress-io/cypress/issues/22428) at Cypress tracking this limitation. |
| 6 | + |
| 7 | +These are the event this plugin will subscribe to: |
| 8 | + |
| 9 | +* before:run |
| 10 | +* after:run |
| 11 | +* before:spec |
| 12 | +* after:spec |
| 13 | +* after:screenshot |
| 14 | + |
| 15 | +For example, if you specify a handler for `after:screenshot` and override this plugin's handler, then screenshots will no longer be automatically added to JSON reports. This is just one type of issue that can arise by overriding event handlers. |
| 16 | + |
| 17 | +## Workaround |
| 18 | + |
| 19 | +You can through options, obtain more fine-grained control over event behavior and invoke your own code alongside this plugin, as shown below. |
| 20 | + |
| 21 | +```js |
| 22 | +import { defineConfig } from "cypress"; |
| 23 | +import createBundler from "@bahmutov/cypress-esbuild-preprocessor"; |
| 24 | +import { |
| 25 | + addCucumberPreprocessorPlugin, |
| 26 | + beforeRunHandler, |
| 27 | + afterRunHandler, |
| 28 | + beforeSpecHandler, |
| 29 | + afterSpecHandler, |
| 30 | + afterScreenshotHandler, |
| 31 | +} from "@badeball/cypress-cucumber-preprocessor"; |
| 32 | +import createEsbuildPlugin from "@badeball/cypress-cucumber-preprocessor/esbuild"; |
| 33 | + |
| 34 | +export default defineConfig({ |
| 35 | + e2e: { |
| 36 | + specPattern: "**/*.feature", |
| 37 | + async setupNodeEvents(on, config) { |
| 38 | + // This is required for the preprocessor to be able to generate JSON reports after each run, and more, |
| 39 | + await addCucumberPreprocessorPlugin(on, config, { |
| 40 | + omitBeforeRunHandler: true, |
| 41 | + omitAfterRunHandler: true, |
| 42 | + omitBeforeSpecHandler: true, |
| 43 | + omitAfterSpecHandler: true, |
| 44 | + omitAfterScreenshotHandler: true, |
| 45 | + }); |
| 46 | + |
| 47 | + on("before:run", () => { |
| 48 | + beforeRunHandler(config); |
| 49 | + |
| 50 | + // Your own `before:run` code goes here. |
| 51 | + }); |
| 52 | + |
| 53 | + on("after:run", () => { |
| 54 | + afterRunHandler(config); |
| 55 | + |
| 56 | + // Your own `after:run` code goes here. |
| 57 | + }); |
| 58 | + |
| 59 | + on("before:spec", () => { |
| 60 | + beforeSpecHandler(config); |
| 61 | + |
| 62 | + // Your own `before:spec` code goes here. |
| 63 | + }); |
| 64 | + |
| 65 | + on("after:spec", (spec, results) => { |
| 66 | + afterSpecHandler(config, spec, results); |
| 67 | + |
| 68 | + // Your own `after:spec` code goes here. |
| 69 | + }); |
| 70 | + |
| 71 | + on("after:screenshot", (details) => { |
| 72 | + afterScreenshotHandler(config, details); |
| 73 | + |
| 74 | + // Your own `after:screenshot` code goes here. |
| 75 | + }); |
| 76 | + |
| 77 | + on( |
| 78 | + "file:preprocessor", |
| 79 | + createBundler({ |
| 80 | + plugins: [createEsbuildPlugin(config)], |
| 81 | + }) |
| 82 | + ); |
| 83 | + |
| 84 | + // Make sure to return the config object as it might have been modified by the plugin. |
| 85 | + return config; |
| 86 | + }, |
| 87 | + }, |
| 88 | +}); |
| 89 | +``` |
0 commit comments