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

Commit

Permalink
feat: add isOn function
Browse files Browse the repository at this point in the history
You can check the condition against a browser name or an environment
yourself.

```js
import {isOn} from '@cypress/skip-test'
it('loads users', () => {
  // when running on Windows locally, the backend is not running
  // thus we need to stub XHR requests
  if (isOn('windows') && isOn('localhost')) {
    cy.server()
    cy.route('/users', 'fixture:users')
  }
  cy.visit('/')
  cy.get('.user').should('have.length', 10)
})
```
  • Loading branch information
bahmutov committed Nov 8, 2019
1 parent 34955e3 commit 0d293da
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 33 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ it.only('runs if task returns production', () => {
})
```

### `isOn`

You can check the condition against a browser name or an environment yourself.

```js
import {isOn} from '@cypress/skip-test'
it('loads users', () => {
// when running on Windows locally, the backend is not running
// thus we need to stub XHR requests
if (isOn('windows') && isOn('localhost')) {
cy.server()
cy.route('/users', 'fixture:users')
}
cy.visit('/')
cy.get('.user').should('have.length', 10)
})
```

### Notes

You can chain conditions together
Expand Down
17 changes: 17 additions & 0 deletions cypress/integration/is-on-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference types="cypress" />
import { isOn } from '../..'

it('returns true for current browser', () => {
expect(isOn(Cypress.browser.name)).to.be.true
})

it('returns true for current platform', () => {
expect(isOn(Cypress.platform)).to.be.true
})

it('returns true for current url', () => {
const url = Cypress.config('baseUrl')
expect(url).to.be.a('string')
expect(isOn(url)).to.be.true
expect(isOn('foo.com')).to.be.false
})
6 changes: 6 additions & 0 deletions cypress/integration/nested-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ onlyOn('mac', () => {
it('works', () => {})
})
})

onlyOn('mac', () => {
onlyOn('electron', () => {
it('runs on Mac Electron', () => {})
})
})
62 changes: 29 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ const normalizeName = name => {
return normalizedName
}

/**
* Returns true if the test is running on the given browser or platform
* or against given url.
* @param {string} name Browser name, platform or url.
* @returns {boolean} Returns true if the test runs against the given condition.
*/
const isOn = name => {
if (!_.isString(name)) {
throw new Error('Invalid syntax: isOn expects a string argument')
}

const normalizedName = normalizeName(name)

if (isPlatform(normalizedName)) {
return Cypress.platform === normalizedName
}

if (isBrowser(normalizedName)) {
return Cypress.browser.name === normalizedName
}

return matchesUrlPart(normalizedName)
}

const getMochaContext = () => cy.state('runnable').ctx
const skip = () => {
const ctx = getMochaContext()
Expand Down Expand Up @@ -75,11 +99,6 @@ const skipOn = (name, cb) => {
)
}

const skip = () => {
const ctx = getMochaContext()
return ctx.skip()
}

const normalizedName = normalizeName(name)

if (cb) {
Expand Down Expand Up @@ -159,44 +178,21 @@ const onlyOn = (name, cb) => {
)
}

const normalizedName = normalizeName(name)

if (cb) {
if (isPlatform(normalizedName) && Cypress.platform === normalizedName) {
return cb()
}

if (isBrowser(normalizedName) && Cypress.browser.name === normalizedName) {
return cb()
}

if (matchesUrlPart(normalizedName)) {
if (isOn(name)) {
return cb()
}
} else {
const normalizedName = normalizeName(name)
cy.log(`onlyOn **${normalizedName}**`)

if (isPlatform(normalizedName)) {
if (Cypress.platform !== normalizedName) {
skip()
}
return
}

if (isBrowser(normalizedName)) {
if (Cypress.browser.name !== normalizedName) {
skip()
}
return
}

if (!matchesUrlPart(normalizedName)) {
if (!isOn(name)) {
return skip()
}
}
}

module.exports = {
skipOn,
onlyOn
onlyOn,
isOn
}

0 comments on commit 0d293da

Please sign in to comment.