Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ You can even run other Cypress commands before deciding to skip or continue

```js
it('runs if task returns production', () => {
cy.task('getDbName').then(name => cy.onlyOn(name === 'production'))
cy.task('getDbName').then((name) => cy.onlyOn(name === 'production'))
// equivalent
cy.task('getDbName').then(name => onlyOn(name === 'production'))
cy.task('getDbName').then((name) => onlyOn(name === 'production'))
// equivalent
cy.task('getDbName')
.then(name => name === 'production')
.then((name) => name === 'production')
.then(onlyOn)
})
```
Expand Down Expand Up @@ -213,7 +213,7 @@ CYPRESS_ENVIRONMENT=staging npx cypress run
Inside the spec file you can write

```js
import {onlyOn} from '@cypress/skip-test'
import {onlyOn, skipOn} from '@cypress/skip-test'
const stubServer = () => {
cy.server()
cy.route('/api/me', 'fx:me.json')
Expand All @@ -224,9 +224,12 @@ it('works', () => {
onlyOn('staging', stubServer)
...
})
skipOn('staging', () => {
it('works on non-staging', () => {...})
})
```

The test `works` will stub network calls when running on `staging`, but will skip calling `stubServer` for other environments.
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`.

### Notes

Expand Down
10 changes: 10 additions & 0 deletions cypress/integration/bool-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ it('runs if task returns production', () => {
.then(onlyOn)
})

it('runs when on the set environment', () => {
Cypress.env('ENVIRONMENT', 'production')
onlyOn('production')
})

it('skips when on the set environment', () => {
Cypress.env('ENVIRONMENT', 'production')
skipOn('production')
})

it('skips if task returns production', () => {
cy.task('getDbName').then(name => skipOn(name === 'production'))
})
27 changes: 27 additions & 0 deletions cypress/integration/callback-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,30 @@ it('does not run given function for other environments', () => {
})
expect(called, 'callback was NOT called').to.be.undefined
})

it('does not run given function for some environment', () => {
Cypress.env('ENVIRONMENT', 'test1')
let called
skipOn('test1', () => {
called = true
})
expect(called, 'callback was called').to.be.undefined
})

it('runs given function for other environments', () => {
Cypress.env('ENVIRONMENT', 'test1')
let called
skipOn('testX', () => {
called = true
})
expect(called, 'callback was NOT called').to.be.true
})

it('ignores non-string environment', () => {
Cypress.env('ENVIRONMENT', 42)
let called
skipOn('42', () => {
called = true
})
expect(called, 'callback was NOT called').to.be.true
})
33 changes: 24 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const { _ } = Cypress

const checkBrowserName = name => {
const checkBrowserName = (name) => {
if ('isBrowser' in Cypress) {
// use the new v4.0 method
// @ts-ignore
Expand All @@ -23,7 +23,7 @@ const checkBrowserName = name => {
* normalizeName('WIN') // 'win32'
* normalizeName('localhost') // 'localhost'
*/
const normalizeName = name => {
const normalizeName = (name) => {
name = name.toLowerCase()

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

const isPlatform = name => ['win32', 'darwin', 'linux'].includes(name)
const isBrowser = name => ['electron', 'chrome', 'firefox'].includes(name)
const isHeadedName = name => ['headed', 'headless'].includes(name)
const isPlatform = (name) => ['win32', 'darwin', 'linux'].includes(name)
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 headedMatches = name => {
const headedMatches = (name) => {
if (name === 'headed') {
return Cypress.browser.isHeaded
}
Expand All @@ -96,10 +98,10 @@ const headedMatches = name => {
* @param {string} name Is checked against `ENVIRONMENT` value
* @returns {boolean} true if the given argument matches environment string
*/
const isEnvironment = name =>
const isEnvironment = (name) =>
Cypress.env('ENVIRONMENT') && Cypress.env('ENVIRONMENT') === name

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

if (isEnvironmentSet()) {
if (!isEnvironment(normalizedName)) {
return cb()
}
return it(`Skipping test(s) on ${normalizedName} environment`)
}

if (!matchesUrlPart(normalizedName)) {
return cb()
}
Expand All @@ -183,6 +192,12 @@ const skipOn = (name, cb) => {
return
}

if (isEnvironmentSet()) {
if (isEnvironment(normalizedName)) {
return skip()
}
}

if (matchesUrlPart(normalizedName)) {
return skip()
}
Expand Down