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

Commit dbacc07

Browse files
authored
feat: allow user to pass sync and async function returning boolean flag (#162)
1 parent 01a994b commit dbacc07

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ onlyOn(S === 'foo', () => {
146146
})
147147
```
148148

149+
Also you can pass sync and async function returning boolean flag.
150+
151+
```js
152+
// run this test if function returns true
153+
const fnReturningTrue = () => true
154+
155+
cy.onlyOn(fnReturningTrue())
156+
```
157+
158+
```js
159+
// run this test if async function returns true
160+
const fnReturningPromiseTrue = () => new Promise((resolve) => resolve(true))
161+
162+
cy.onlyOn(fnReturningPromiseTrue())
163+
```
164+
149165
You can even run other Cypress commands before deciding to skip or continue
150166

151167
```js
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// <reference types="cypress" />
2+
import { onlyOn, skipOn } from '../..'
3+
4+
const functionReturningTrue = () => true
5+
const asyncFunctionReturningTrue = () => new Promise((resolve) => resolve(true))
6+
7+
it('runs on true', () => {
8+
onlyOn(functionReturningTrue())
9+
})
10+
11+
it('skips on true', () => {
12+
skipOn(functionReturningTrue())
13+
})
14+
15+
onlyOn(functionReturningTrue(), () => {
16+
it('runs it as callback', () => {})
17+
})
18+
19+
skipOn(functionReturningTrue(), () => {
20+
it('skips this completely', () => {})
21+
})
22+
23+
it('runs on true', () => {
24+
onlyOn(asyncFunctionReturningTrue())
25+
})
26+
27+
it('skips on true', () => {
28+
skipOn(asyncFunctionReturningTrue())
29+
})
30+
31+
onlyOn(asyncFunctionReturningTrue(), () => {
32+
it('runs it as callback', () => {})
33+
})
34+
35+
skipOn(asyncFunctionReturningTrue(), () => {
36+
it('skips this completely', () => {})
37+
})

index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ const isBrowser = (name) => ['electron', 'chrome', 'firefox'].includes(name)
8080
const isHeadedName = (name) => ['headed', 'headless'].includes(name)
8181
const isEnvironmentSet = () =>
8282
typeof Cypress.env('ENVIRONMENT') === 'string' && Cypress.env('ENVIRONMENT')
83+
const isAsyncFn = (name) =>
84+
Object.prototype.toString.call(name) === '[object AsyncFunction]'
8385

8486
const headedMatches = (name) => {
8587
if (name === 'headed') {
@@ -129,12 +131,22 @@ const skipOnBool = (flag, cb) => {
129131

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

142+
if (isAsyncFn(name)) {
143+
return name().then((result) => onlyOnBool(result, cb))
144+
}
145+
146+
if (_.isFunction(name)) {
147+
return onlyOnBool(name(), cb)
148+
}
149+
138150
if (!_.isString(name) || '') {
139151
throw new Error(
140152
'Invalid syntax: cy.skipOn(<name>), for example cy.skipOn("linux")'
@@ -226,7 +238,7 @@ const onlyOnBool = (flag, cb) => {
226238

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

255+
if (isAsyncFn(name)) {
256+
return name().then((result) => onlyOnBool(result, cb))
257+
}
258+
259+
if (_.isFunction(name)) {
260+
return onlyOnBool(name(), cb)
261+
}
262+
243263
if (cb) {
244264
if (isOn(name)) {
245265
return cb()

0 commit comments

Comments
 (0)