Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 3, 2023
0 parents commit 46be620
Show file tree
Hide file tree
Showing 9 changed files with 3,505 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "all",
"tabWidth": 2,
"semi": false,
"singleQuote": true
}
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# cypress-on-fix

> Fixes multiple Cypress plugins subscribing to "on" events
## Install

Add this NPM package as a dev dependency

```
# install using NPM
$ npm i -D cypress-on-fix
# install using Yarn
$ yarn add -D cypress-on-fix
```

Wrap the `on` passed by Cypress in your config file

```js
const { defineConfig } = require('cypress')

module.exports = defineConfig({
e2e: {
// baseUrl, etc
supportFile: false,
fixturesFolder: false,
setupNodeEvents(cypressOn, config) {
const on = require('.')(cypressOn)
// use "on" to register plugins, for example
// https://github.com/bahmutov/cypress-split
require('cypress-split')(on, config)
// https://github.com/bahmutov/cypress-watch-and-reload
require('cypress-watch-and-reload/plugins')(on, config)
// https://github.com/bahmutov/cypress-code-coverage
require('@bahmutov/cypress-code-coverage/plugin')(on, config)
},
},
})
```

## Debugging

This module uses [debug](https://github.com/debug-js/debug#readme) to print verbose log messages. Run Cypress with

```
DEBUG=cypress-on-fix npx cypress run ...
```
21 changes: 21 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { defineConfig } = require('cypress')

module.exports = defineConfig({
e2e: {
// baseUrl, etc
supportFile: false,
fixturesFolder: false,
setupNodeEvents(cypressOn, config) {
const on = require('.')(cypressOn)
on('after:spec', (a) => {
console.log('after spec 1', a)
})
on('after:spec', () => {
console.log('after spec 2')
})
on('after:spec', () => {
console.log('after spec 3')
})
},
},
})
5 changes: 5 additions & 0 deletions cypress/e2e/spec1.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// enables intelligent code completion for Cypress commands
// https://on.cypress.io/intelligent-code-completion
/// <reference types="cypress" />

it('works', () => {})
5 changes: 5 additions & 0 deletions cypress/e2e/spec2.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// enables intelligent code completion for Cypress commands
// https://on.cypress.io/intelligent-code-completion
/// <reference types="cypress" />

it('works 2', () => {})
37 changes: 37 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference types="cypress" />

const debug = require('debug')('cypress-on-fix')

/**
* @param {Cypress.PluginEvents} on
*/
function onProxy(on) {
const listeners = {}
return function proxiedOn(eventName, callback) {
if (eventName === 'task') {
// no need to proxy tasks
return on('task', callback)
}

debug('registering %s', eventName)
if (listeners[eventName]) {
debug('there are already %d listeners', listeners[eventName].length)
listeners[eventName].push(callback)
} else {
debug('the only listener so far')
listeners[eventName] = [callback]
on(eventName, function () {
debug(
'proxy %s to %d listeners',
eventName,
listeners[eventName].length,
)
listeners[eventName].forEach((fn) => {
fn.apply(null, arguments)
})
})
}
}
}

module.exports = onProxy
Loading

0 comments on commit 46be620

Please sign in to comment.