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

Commit f62f1fa

Browse files
authored
feat: add environment variable support (#12)
```js // run tests with // CYPRESS_ENVIRONMENT=staging npx cypress run import {onlyOn} from '@cypress/skip-test' const stubServer = () => { cy.server() cy.route('/api/me', 'fx:me.json') cy.route('/api/permissions', 'fx:permissions.json') // Lots of fixtures ... } it('works', () => { onlyOn('staging', stubServer) ... }) ``` * chore: add prettier * add .vscode settings * feat: add ENVIRONMENT support
1 parent cc757fc commit f62f1fa

File tree

8 files changed

+90
-3
lines changed

8 files changed

+90
-3
lines changed

.prettierrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"trailingComma": "none",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true,
6+
"bracketSpacing": true
7+
}

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"editor.defaultFormatter": "esbenp.prettier-vscode"
4+
}

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# @cypress/skip-test [![renovate-app badge][renovate-badge]][renovate-app] [![semantic-release][semantic-image] ][semantic-url] [![CircleCI](https://circleci.com/gh/cypress-io/cypress-skip-test/tree/master.svg?style=svg)](https://circleci.com/gh/cypress-io/cypress-skip-test/tree/master)
2+
23
> Simple commands to skip a test based on platform, browser or an url
34
45
```js
@@ -137,7 +138,7 @@ onlyOn(S === 'foo', () => {
137138
You can even run other Cypress commands before deciding to skip or continue
138139

139140
```js
140-
it.only('runs if task returns production', () => {
141+
it('runs if task returns production', () => {
141142
cy.task('getDbName').then(name => cy.onlyOn(name === 'production'))
142143
// equivalent
143144
cy.task('getDbName').then(name => onlyOn(name === 'production'))
@@ -153,7 +154,7 @@ it.only('runs if task returns production', () => {
153154
You can check the condition against a browser name or an environment yourself.
154155

155156
```js
156-
import {isOn} from '@cypress/skip-test'
157+
import { isOn } from '@cypress/skip-test'
157158
it('loads users', () => {
158159
// when running on Windows locally, the backend is not running
159160
// thus we need to stub XHR requests
@@ -166,6 +167,32 @@ it('loads users', () => {
166167
})
167168
```
168169

170+
### `ENVIRONMENT`
171+
172+
This module also reads special environment variable `ENVIRONMENT` inside its checks. For example, to only stub network calls on `staging` environment, execute the tests like this:
173+
174+
```shell
175+
CYPRESS_ENVIRONMENT=staging npx cypress run
176+
```
177+
178+
Inside the spec file you can write
179+
180+
```js
181+
import {onlyOn} from '@cypress/skip-test'
182+
const stubServer = () => {
183+
cy.server()
184+
cy.route('/api/me', 'fx:me.json')
185+
cy.route('/api/permissions', 'fx:permissions.json')
186+
// Lots of fixtures ...
187+
}
188+
it('works', () => {
189+
onlyOn('staging', stubServer)
190+
...
191+
})
192+
```
193+
194+
The test `works` will stub network calls when running on `staging`, but will skip calling `stubServer` for other environments.
195+
169196
### Notes
170197

171198
You can chain conditions together

cypress/integration/callback-spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,21 @@ skipOn('mac', () => {
1313
// this test will run on every platform but Mac
1414
it('hides this test on Mac', () => {})
1515
})
16+
17+
it('runs given function for some environment', () => {
18+
Cypress.env('ENVIRONMENT', 'test1')
19+
let called
20+
onlyOn('test1', () => {
21+
called = true
22+
})
23+
expect(called, 'callback was called').to.be.true
24+
})
25+
26+
it('does not run given function for other environments', () => {
27+
Cypress.env('ENVIRONMENT', 'test1')
28+
let called
29+
onlyOn('testX', () => {
30+
called = true
31+
})
32+
expect(called, 'callback was NOT called').to.be.undefined
33+
})

cypress/integration/is-on-spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ it('returns true for current url', () => {
1515
expect(isOn(url)).to.be.true
1616
expect(isOn('foo.com')).to.be.false
1717
})
18+
19+
it('uses variable ENVIRONMENT', () => {
20+
Cypress.env('ENVIRONMENT', 'current test')
21+
expect(isOn('current test'), 'matches current ENVIRONMENT').to.be.true
22+
expect(isOn('anywhere else'), 'any other value').to.be.false
23+
24+
Cypress.env('ENVIRONMENT', 'test env 2')
25+
expect(isOn('test env 2'), 'matches second ENVIRONMENT').to.be.true
26+
})

index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ const isOn = name => {
4747
return Cypress.browser.name === normalizedName
4848
}
4949

50+
if (isEnvironment(name)) {
51+
return true
52+
}
53+
5054
return matchesUrlPart(normalizedName)
5155
}
5256

57+
// @ts-ignore "cy.state" is not in the "cy" type
5358
const getMochaContext = () => cy.state('runnable').ctx
5459
const skip = () => {
5560
const ctx = getMochaContext()
@@ -58,6 +63,15 @@ const skip = () => {
5863

5964
const isPlatform = name => ['win32', 'darwin', 'linux'].includes(name)
6065
const isBrowser = name => ['electron', 'chrome', 'firefox'].includes(name)
66+
/**
67+
* You can pass custom environment name when running Cypress
68+
* @example
69+
* CYPRESS_ENVIRONMENT=staging npx cypress run
70+
* @param {string} name Is checked against `ENVIRONMENT` value
71+
* @returns {boolean} true if the given argument matches environment string
72+
*/
73+
const isEnvironment = name =>
74+
Cypress.env('ENVIRONMENT') && Cypress.env('ENVIRONMENT') === name
6175

6276
const matchesUrlPart = normalizedName => {
6377
// assuming name is part of the url, and the baseUrl should be set

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"types": "index.d.ts",
77
"scripts": {
88
"test": "cypress run",
9-
"semantic-release": "semantic-release"
9+
"semantic-release": "semantic-release",
10+
"format": "prettier --write '*.js' 'cypress/**/*.js'"
1011
},
1112
"keywords": [
1213
"cypress",
@@ -17,6 +18,7 @@
1718
"private": false,
1819
"devDependencies": {
1920
"cypress": "3.6.1",
21+
"prettier": "1.19.1",
2022
"semantic-release": "15.13.30"
2123
},
2224
"repository": {

0 commit comments

Comments
 (0)