Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Commit

Permalink
feat: allow user to pass sync and async function returning boolean fl…
Browse files Browse the repository at this point in the history
…ag (#162)
  • Loading branch information
MCFreddie777 authored Jun 10, 2021
1 parent 01a994b commit dbacc07
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ onlyOn(S === 'foo', () => {
})
```

Also you can pass sync and async function returning boolean flag.

```js
// run this test if function returns true
const fnReturningTrue = () => true

cy.onlyOn(fnReturningTrue())
```

```js
// run this test if async function returns true
const fnReturningPromiseTrue = () => new Promise((resolve) => resolve(true))

cy.onlyOn(fnReturningPromiseTrue())
```

You can even run other Cypress commands before deciding to skip or continue

```js
Expand Down
37 changes: 37 additions & 0 deletions cypress/integration/function-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference types="cypress" />
import { onlyOn, skipOn } from '../..'

const functionReturningTrue = () => true
const asyncFunctionReturningTrue = () => new Promise((resolve) => resolve(true))

it('runs on true', () => {
onlyOn(functionReturningTrue())
})

it('skips on true', () => {
skipOn(functionReturningTrue())
})

onlyOn(functionReturningTrue(), () => {
it('runs it as callback', () => {})
})

skipOn(functionReturningTrue(), () => {
it('skips this completely', () => {})
})

it('runs on true', () => {
onlyOn(asyncFunctionReturningTrue())
})

it('skips on true', () => {
skipOn(asyncFunctionReturningTrue())
})

onlyOn(asyncFunctionReturningTrue(), () => {
it('runs it as callback', () => {})
})

skipOn(asyncFunctionReturningTrue(), () => {
it('skips this completely', () => {})
})
22 changes: 21 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ const isBrowser = (name) => ['electron', 'chrome', 'firefox'].includes(name)
const isHeadedName = (name) => ['headed', 'headless'].includes(name)
const isEnvironmentSet = () =>
typeof Cypress.env('ENVIRONMENT') === 'string' && Cypress.env('ENVIRONMENT')
const isAsyncFn = (name) =>
Object.prototype.toString.call(name) === '[object AsyncFunction]'

const headedMatches = (name) => {
if (name === 'headed') {
Expand Down Expand Up @@ -129,12 +131,22 @@ const skipOnBool = (flag, cb) => {

/**
* Skips the current test based on the browser, platform or url.
* @param {string|boolean|Function} name - condition, could be platform, browser name, url or true|false.
* @param {() => void} cb - Optional, run the given callback if the condition passes
*/
const skipOn = (name, cb) => {
if (_.isBoolean(name)) {
return skipOnBool(name, cb)
}

if (isAsyncFn(name)) {
return name().then((result) => onlyOnBool(result, cb))
}

if (_.isFunction(name)) {
return onlyOnBool(name(), cb)
}

if (!_.isString(name) || '') {
throw new Error(
'Invalid syntax: cy.skipOn(<name>), for example cy.skipOn("linux")'
Expand Down Expand Up @@ -226,7 +238,7 @@ const onlyOnBool = (flag, cb) => {

/**
* Runs the current test only in the specified browser, platform or against url.
* @param {string|boolean} name - condition, could be platform, browser name, url or true|false.
* @param {string|boolean|Function} name - condition, could be platform, browser name, url or true|false.
* @param {() => void} cb - Optional, run the given callback if the condition passes
*/
const onlyOn = (name, cb) => {
Expand All @@ -240,6 +252,14 @@ const onlyOn = (name, cb) => {
)
}

if (isAsyncFn(name)) {
return name().then((result) => onlyOnBool(result, cb))
}

if (_.isFunction(name)) {
return onlyOnBool(name(), cb)
}

if (cb) {
if (isOn(name)) {
return cb()
Expand Down

0 comments on commit dbacc07

Please sign in to comment.