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

Commit 3d4d1e7

Browse files
committed
feat: callback form, close #6
1 parent 7b4de11 commit 3d4d1e7

File tree

4 files changed

+97
-28
lines changed

4 files changed

+97
-28
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
node_modules/
2+
cypress/videos
3+
cypress/screenshots

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ it('skips on Mac', () => {
8181
})
8282
```
8383

84+
### imports with callback
85+
86+
Instead of dynamically skipping a test at run-time, you can hide entire blocks of tests using the callback format.
87+
88+
```js
89+
onlyOn('mac', () => {
90+
// this callback will only evaluate on Mac
91+
// thus the tests will be completely hidden from other platforms
92+
describe('Mac tests', () => {
93+
it('works', () => {})
94+
})
95+
})
96+
skipOn('mac', () => {
97+
// this test will run on every platform but Mac
98+
it('hides this test on Mac', () => {})
99+
})
100+
```
101+
84102
### Notes
85103

86104
You can chain conditions together
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference types="cypress" />
2+
import { onlyOn, skipOn } from '../..'
3+
4+
onlyOn('mac', () => {
5+
// this callback will only evaluate on Mac
6+
// thus the tests will be completely hidden from other platforms
7+
describe('Mac tests', () => {
8+
it('works', () => {})
9+
})
10+
})
11+
12+
skipOn('mac', () => {
13+
it('hides this test on Mac', () => {})
14+
})

index.js

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const matchesUrlPart = normalizedName => {
3333
/**
3434
* Skips the current test based on the browser, platform or url.
3535
*/
36-
export const skipOn = name => {
36+
export const skipOn = (name, cb) => {
3737
if (!_.isString(name) || '') {
3838
throw new Error(
3939
'Invalid syntax: cy.skipOn(<name>), for example cy.skipOn("linux")'
@@ -46,31 +46,52 @@ export const skipOn = name => {
4646
}
4747

4848
const normalizedName = normalizeName(name)
49-
cy.log(`skipOn **${normalizedName}**`)
5049

51-
if (isPlatform(normalizedName)) {
52-
if (Cypress.platform === normalizedName) {
53-
skip()
50+
if (cb) {
51+
if (isPlatform(normalizedName)) {
52+
if (Cypress.platform !== normalizedName) {
53+
return cb()
54+
}
55+
return
5456
}
55-
return
56-
}
5757

58-
if (isBrowser(normalizedName)) {
59-
if (Cypress.browser.name === normalizedName) {
60-
skip()
58+
if (isBrowser(normalizedName)) {
59+
if (Cypress.browser.name !== normalizedName) {
60+
return cb()
61+
}
62+
return
63+
}
64+
65+
if (!matchesUrlPart(normalizedName)) {
66+
return cb()
67+
}
68+
} else {
69+
cy.log(`skipOn **${normalizedName}**`)
70+
71+
if (isPlatform(normalizedName)) {
72+
if (Cypress.platform === normalizedName) {
73+
skip()
74+
}
75+
return
76+
}
77+
78+
if (isBrowser(normalizedName)) {
79+
if (Cypress.browser.name === normalizedName) {
80+
skip()
81+
}
82+
return
6183
}
62-
return
63-
}
6484

65-
if (matchesUrlPart(normalizedName)) {
66-
return skip()
85+
if (matchesUrlPart(normalizedName)) {
86+
return skip()
87+
}
6788
}
6889
}
6990

7091
/**
7192
* Runs the current test only in the specified browser, platform or against url.
7293
*/
73-
export const onlyOn = name => {
94+
export const onlyOn = (name, cb) => {
7495
if (!_.isString(name) || '') {
7596
throw new Error(
7697
'Invalid syntax: cy.onlyOn(<name>), for example cy.onlyOn("linux")'
@@ -79,24 +100,38 @@ export const onlyOn = name => {
79100

80101
const normalizedName = normalizeName(name)
81102

82-
cy.log(`onlyOn **${normalizedName}**`)
103+
if (cb) {
104+
if (isPlatform(normalizedName) && Cypress.platform === normalizedName) {
105+
return cb()
106+
}
83107

84-
if (isPlatform(normalizedName)) {
85-
if (Cypress.platform !== normalizedName) {
86-
skip()
108+
if (isBrowser(normalizedName) && Cypress.browser.name === normalizedName) {
109+
return cb()
87110
}
88-
return
89-
}
90111

91-
if (isBrowser(normalizedName)) {
92-
if (Cypress.browser.name !== normalizedName) {
93-
skip()
112+
if (matchesUrlPart(normalizedName)) {
113+
return cb()
114+
}
115+
} else {
116+
cy.log(`onlyOn **${normalizedName}**`)
117+
118+
if (isPlatform(normalizedName)) {
119+
if (Cypress.platform !== normalizedName) {
120+
skip()
121+
}
122+
return
94123
}
95-
return
96-
}
97124

98-
if (!matchesUrlPart(normalizedName)) {
99-
return skip()
125+
if (isBrowser(normalizedName)) {
126+
if (Cypress.browser.name !== normalizedName) {
127+
skip()
128+
}
129+
return
130+
}
131+
132+
if (!matchesUrlPart(normalizedName)) {
133+
return skip()
134+
}
100135
}
101136
}
102137

0 commit comments

Comments
 (0)