diff --git a/README.md b/README.md index 8281ec4..44e869a 100644 --- a/README.md +++ b/README.md @@ -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) }) ``` @@ -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') @@ -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 diff --git a/cypress/integration/bool-spec.js b/cypress/integration/bool-spec.js index 6086a2a..cbb849e 100644 --- a/cypress/integration/bool-spec.js +++ b/cypress/integration/bool-spec.js @@ -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')) }) diff --git a/cypress/integration/callback-spec.js b/cypress/integration/callback-spec.js index 2fcd7d7..46f5e4d 100644 --- a/cypress/integration/callback-spec.js +++ b/cypress/integration/callback-spec.js @@ -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 +}) diff --git a/index.js b/index.js index 2b9f05a..03bf1c6 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ const { _ } = Cypress -const checkBrowserName = name => { +const checkBrowserName = (name) => { if ('isBrowser' in Cypress) { // use the new v4.0 method // @ts-ignore @@ -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 @@ -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') } @@ -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 } @@ -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) @@ -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() } @@ -183,6 +192,12 @@ const skipOn = (name, cb) => { return } + if (isEnvironmentSet()) { + if (isEnvironment(normalizedName)) { + return skip() + } + } + if (matchesUrlPart(normalizedName)) { return skip() }