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

Commit c533925

Browse files
feat: add environment checks to skipOn (#99) (#132)
Co-authored-by: Gleb Bahmutov <gleb.bahmutov@gmail.com> Co-authored-by: Rafael Anachoreta <rafael.anachoreta@gmail.com>
1 parent 355dc7a commit c533925

File tree

4 files changed

+69
-14
lines changed

4 files changed

+69
-14
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ You can even run other Cypress commands before deciding to skip or continue
150150

151151
```js
152152
it('runs if task returns production', () => {
153-
cy.task('getDbName').then(name => cy.onlyOn(name === 'production'))
153+
cy.task('getDbName').then((name) => cy.onlyOn(name === 'production'))
154154
// equivalent
155-
cy.task('getDbName').then(name => onlyOn(name === 'production'))
155+
cy.task('getDbName').then((name) => onlyOn(name === 'production'))
156156
// equivalent
157157
cy.task('getDbName')
158-
.then(name => name === 'production')
158+
.then((name) => name === 'production')
159159
.then(onlyOn)
160160
})
161161
```
@@ -213,7 +213,7 @@ CYPRESS_ENVIRONMENT=staging npx cypress run
213213
Inside the spec file you can write
214214

215215
```js
216-
import {onlyOn} from '@cypress/skip-test'
216+
import {onlyOn, skipOn} from '@cypress/skip-test'
217217
const stubServer = () => {
218218
cy.server()
219219
cy.route('/api/me', 'fx:me.json')
@@ -224,9 +224,12 @@ it('works', () => {
224224
onlyOn('staging', stubServer)
225225
...
226226
})
227+
skipOn('staging', () => {
228+
it('works on non-staging', () => {...})
229+
})
227230
```
228231

229-
The test `works` will stub network calls when running on `staging`, but will skip calling `stubServer` for other environments.
232+
The test `works` will stub network calls when running on `staging`, but will skip calling `stubServer` for other environments. The test `works on non-staging` will be skipped when the environment is `staging`.
230233

231234
### Notes
232235

cypress/integration/bool-spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ it('runs if task returns production', () => {
2727
.then(onlyOn)
2828
})
2929

30+
it('runs when on the set environment', () => {
31+
Cypress.env('ENVIRONMENT', 'production')
32+
onlyOn('production')
33+
})
34+
35+
it('skips when on the set environment', () => {
36+
Cypress.env('ENVIRONMENT', 'production')
37+
skipOn('production')
38+
})
39+
3040
it('skips if task returns production', () => {
3141
cy.task('getDbName').then(name => skipOn(name === 'production'))
3242
})

cypress/integration/callback-spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,30 @@ it('does not run given function for other environments', () => {
3131
})
3232
expect(called, 'callback was NOT called').to.be.undefined
3333
})
34+
35+
it('does not run given function for some environment', () => {
36+
Cypress.env('ENVIRONMENT', 'test1')
37+
let called
38+
skipOn('test1', () => {
39+
called = true
40+
})
41+
expect(called, 'callback was called').to.be.undefined
42+
})
43+
44+
it('runs given function for other environments', () => {
45+
Cypress.env('ENVIRONMENT', 'test1')
46+
let called
47+
skipOn('testX', () => {
48+
called = true
49+
})
50+
expect(called, 'callback was NOT called').to.be.true
51+
})
52+
53+
it('ignores non-string environment', () => {
54+
Cypress.env('ENVIRONMENT', 42)
55+
let called
56+
skipOn('42', () => {
57+
called = true
58+
})
59+
expect(called, 'callback was NOT called').to.be.true
60+
})

index.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const { _ } = Cypress
55

6-
const checkBrowserName = name => {
6+
const checkBrowserName = (name) => {
77
if ('isBrowser' in Cypress) {
88
// use the new v4.0 method
99
// @ts-ignore
@@ -23,7 +23,7 @@ const checkBrowserName = name => {
2323
* normalizeName('WIN') // 'win32'
2424
* normalizeName('localhost') // 'localhost'
2525
*/
26-
const normalizeName = name => {
26+
const normalizeName = (name) => {
2727
name = name.toLowerCase()
2828

2929
// values are normalized strings we will use
@@ -42,7 +42,7 @@ const normalizeName = name => {
4242
* @param {string} name Browser name, platform or url.
4343
* @returns {boolean} Returns true if the test runs against the given condition.
4444
*/
45-
const isOn = name => {
45+
const isOn = (name) => {
4646
if (!_.isString(name)) {
4747
throw new Error('Invalid syntax: isOn expects a string argument')
4848
}
@@ -75,11 +75,13 @@ const skip = () => {
7575
return ctx.skip()
7676
}
7777

78-
const isPlatform = name => ['win32', 'darwin', 'linux'].includes(name)
79-
const isBrowser = name => ['electron', 'chrome', 'firefox'].includes(name)
80-
const isHeadedName = name => ['headed', 'headless'].includes(name)
78+
const isPlatform = (name) => ['win32', 'darwin', 'linux'].includes(name)
79+
const isBrowser = (name) => ['electron', 'chrome', 'firefox'].includes(name)
80+
const isHeadedName = (name) => ['headed', 'headless'].includes(name)
81+
const isEnvironmentSet = () =>
82+
typeof Cypress.env('ENVIRONMENT') === 'string' && Cypress.env('ENVIRONMENT')
8183

82-
const headedMatches = name => {
84+
const headedMatches = (name) => {
8385
if (name === 'headed') {
8486
return Cypress.browser.isHeaded
8587
}
@@ -96,10 +98,10 @@ const headedMatches = name => {
9698
* @param {string} name Is checked against `ENVIRONMENT` value
9799
* @returns {boolean} true if the given argument matches environment string
98100
*/
99-
const isEnvironment = name =>
101+
const isEnvironment = (name) =>
100102
Cypress.env('ENVIRONMENT') && Cypress.env('ENVIRONMENT') === name
101103

102-
const matchesUrlPart = normalizedName => {
104+
const matchesUrlPart = (normalizedName) => {
103105
// assuming name is part of the url, and the baseUrl should be set
104106
const url = Cypress.config('baseUrl') || location.origin
105107
return url && url.includes(normalizedName)
@@ -163,6 +165,13 @@ const skipOn = (name, cb) => {
163165
return it(`Skipping test(s) in ${normalizedName} mode`)
164166
}
165167

168+
if (isEnvironmentSet()) {
169+
if (!isEnvironment(normalizedName)) {
170+
return cb()
171+
}
172+
return it(`Skipping test(s) on ${normalizedName} environment`)
173+
}
174+
166175
if (!matchesUrlPart(normalizedName)) {
167176
return cb()
168177
}
@@ -183,6 +192,12 @@ const skipOn = (name, cb) => {
183192
return
184193
}
185194

195+
if (isEnvironmentSet()) {
196+
if (isEnvironment(normalizedName)) {
197+
return skip()
198+
}
199+
}
200+
186201
if (matchesUrlPart(normalizedName)) {
187202
return skip()
188203
}

0 commit comments

Comments
 (0)