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

Commit a597df3

Browse files
authored
feat: add skipOn and onlyOn with headed or headless mode (#39)
* add skipOn headed or headless * add onlyOn and tests
1 parent 82cd528 commit a597df3

File tree

6 files changed

+166
-1
lines changed

6 files changed

+166
-1
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,30 @@ it('loads users', () => {
167167
})
168168
```
169169

170+
### Headed
171+
172+
You can skip or run tests in headed / headless environments
173+
174+
```js
175+
import { skipOn, onlyOn } from '@cypress/skip-test'
176+
177+
skipOn('headed', () => {
178+
it('skips the current test in headed mode', () => {
179+
cy.wrap(true).should('equal', true)
180+
})
181+
})
182+
183+
onlyOn('headless', () => {
184+
it('runs only in headless mode', () => { ... })
185+
})
186+
```
187+
188+
**Note:** when skipping tests in this case, it will insert an empty placeholder test to provide information why the tests were skipped.
189+
190+
```text
191+
- Skipping test(s), not on headed
192+
```
193+
170194
### `ENVIRONMENT`
171195

172196
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:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference types="cypress" />
2+
import { onlyOn } from '../..'
3+
4+
onlyOn('headed', () => {
5+
it('runs the current test only in headed mode', () => {
6+
cy.wrap(true).should('equal', true)
7+
})
8+
})

cypress/integration/headed-spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference types="cypress" />
2+
import { skipOn } from '../..'
3+
4+
skipOn('headed', () => {
5+
it('skips the current test in headed mode', () => {
6+
cy.wrap(true).should('equal', true)
7+
})
8+
})

index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ const isOn = name => {
5757
return checkBrowserName(normalizedName)
5858
}
5959

60+
if (isHeadedName(normalizedName)) {
61+
return headedMatches(normalizedName)
62+
}
63+
6064
if (isEnvironment(name)) {
6165
return true
6266
}
@@ -73,6 +77,18 @@ const skip = () => {
7377

7478
const isPlatform = name => ['win32', 'darwin', 'linux'].includes(name)
7579
const isBrowser = name => ['electron', 'chrome', 'firefox'].includes(name)
80+
const isHeadedName = name => ['headed', 'headless'].includes(name)
81+
82+
const headedMatches = name => {
83+
if (name === 'headed') {
84+
return Cypress.browser.isHeaded
85+
}
86+
if (name === 'headless') {
87+
return Cypress.browser.isHeadless
88+
}
89+
throw new Error(`Do not know how to treat headed flag "${name}"`)
90+
}
91+
7692
/**
7793
* You can pass custom environment name when running Cypress
7894
* @example
@@ -140,6 +156,13 @@ const skipOn = (name, cb) => {
140156
return it(`Skipping test(s) on ${normalizedName}`)
141157
}
142158

159+
if (isHeadedName(normalizedName)) {
160+
if (!headedMatches(normalizedName)) {
161+
return cb()
162+
}
163+
return it(`Skipping test(s) in ${normalizedName} mode`)
164+
}
165+
143166
if (!matchesUrlPart(normalizedName)) {
144167
return cb()
145168
}
@@ -205,6 +228,8 @@ const onlyOn = (name, cb) => {
205228
if (cb) {
206229
if (isOn(name)) {
207230
return cb()
231+
} else {
232+
return it(`Skipping test(s), not on ${name}`)
208233
}
209234
} else {
210235
const normalizedName = normalizeName(name)

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"test": "cypress run",
99
"semantic-release": "semantic-release",
1010
"format": "prettier --write '*.js' 'cypress/**/*.js'",
11-
"e2e": "mocha --timeout 30000 test"
11+
"e2e": "mocha --timeout 30000 test",
12+
"cy:open": "cypress open",
13+
"cy:run": "cypress run"
1214
},
1315
"keywords": [
1416
"cypress",

test/headed-spec.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
const spok = require('spok').default
2+
const cypress = require('cypress')
3+
const assert = require('assert')
4+
const debug = require('debug')('test')
5+
6+
const assertResults = results => {
7+
// let's confirm the number of tests
8+
// and maybe some additional information
9+
debug('results %o', results)
10+
11+
spok(assert, results, {
12+
totalSuites: 0,
13+
totalTests: 1,
14+
runs: spok.array
15+
})
16+
assert(results.runs.length === 1, 'single run')
17+
}
18+
19+
const shouldHaveTest = (title, state, results) => {
20+
assert(results.runs.length === 1, 'there should be just a single run')
21+
const test = results.runs[0].tests.find(t => t.title[0] === title)
22+
debug('found test %o', test)
23+
assert(test, 'could not find test')
24+
25+
spok(assert, test, {
26+
title,
27+
state,
28+
// there should not be any errors
29+
error: null
30+
})
31+
}
32+
33+
/* eslint-env mocha */
34+
describe('skipping test in headed mode', () => {
35+
it('runs the test in headless mode', () => {
36+
return cypress
37+
.run({
38+
spec: 'cypress/integration/headed-spec.js',
39+
headed: false,
40+
video: false
41+
})
42+
.then(results => {
43+
assertResults(results)
44+
shouldHaveTest(
45+
'skips the current test in headed mode',
46+
'passed',
47+
results
48+
)
49+
})
50+
})
51+
52+
it('skips test in headed mode', () => {
53+
return cypress
54+
.run({
55+
spec: 'cypress/integration/headed-spec.js',
56+
headed: true,
57+
video: false
58+
})
59+
.then(results => {
60+
assertResults(results)
61+
// the plugin automatically replaces any tests in skipped block
62+
// with a single dummy test with this name
63+
shouldHaveTest('Skipping test(s) in headed mode', 'pending', results)
64+
})
65+
})
66+
})
67+
68+
describe('only running test in headed mode', () => {
69+
it('runs the test in headed mode', () => {
70+
return cypress
71+
.run({
72+
spec: 'cypress/integration/headed-only-spec.js',
73+
headed: true,
74+
video: false
75+
})
76+
.then(results => {
77+
assertResults(results)
78+
shouldHaveTest(
79+
'runs the current test only in headed mode',
80+
'passed',
81+
results
82+
)
83+
})
84+
})
85+
86+
it('skips test in headless mode', () => {
87+
return cypress
88+
.run({
89+
spec: 'cypress/integration/headed-only-spec.js',
90+
headed: false,
91+
video: false
92+
})
93+
.then(results => {
94+
assertResults(results)
95+
shouldHaveTest('Skipping test(s), not on headed', 'pending', results)
96+
})
97+
})
98+
})

0 commit comments

Comments
 (0)