From bac20b5de8f45bfd19343a9229a8af112f38160f Mon Sep 17 00:00:00 2001 From: Rafael Anachoreta Date: Wed, 13 Jan 2021 14:10:26 +0000 Subject: [PATCH] feat: add environment checks to skipOn (#99) Co-authored-by: Gleb Bahmutov --- cypress/integration/bool-spec.js | 10 ++++++++++ cypress/integration/callback-spec.js | 18 ++++++++++++++++++ index.js | 14 ++++++++++++++ 3 files changed, 42 insertions(+) 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..30126c4 100644 --- a/cypress/integration/callback-spec.js +++ b/cypress/integration/callback-spec.js @@ -31,3 +31,21 @@ 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 +}) diff --git a/index.js b/index.js index 2b9f05a..ee97634 100644 --- a/index.js +++ b/index.js @@ -78,6 +78,7 @@ const 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 isEnvironmentSet = () => typeof Cypress.env('ENVIRONMENT') === 'string' && Cypress.env('ENVIRONMENT') const headedMatches = name => { if (name === 'headed') { @@ -163,6 +164,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 +191,12 @@ const skipOn = (name, cb) => { return } + if (isEnvironmentSet()) { + if (isEnvironment(normalizedName)) { + return skip() + } + } + if (matchesUrlPart(normalizedName)) { return skip() }