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

Commit b4ee631

Browse files
authored
feat: support suite tags (#49)
* feat: working grep by suite tags * add ci test * update readme
1 parent e38238a commit b4ee631

File tree

7 files changed

+62
-14
lines changed

7 files changed

+62
-14
lines changed

.github/workflows/ci.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ jobs:
139139
--expect expects/describe-tags-invert-spec.json
140140
141141
# enable suite of tests using a tag
142-
# DOES NOT WORK YET
143142
- name: Enable suite of tests with a tag 🧪
144143
run: |
145144
npx cypress-expect \
@@ -148,6 +147,22 @@ jobs:
148147
--env grepTags=@smoke \
149148
--expect expects/describe-tags-spec.json
150149
150+
- name: Nested describes with grep 🧪
151+
run: |
152+
npx cypress-expect \
153+
--spec cypress/integration/nested-describe-spec.js \
154+
--config testFiles="nested-describe-spec.js" \
155+
--env grepTags=@smoke \
156+
--expect expects/nested-describe-spec.json
157+
158+
- name: Nested describes without grep 🧪
159+
run: |
160+
npx cypress-expect \
161+
--spec cypress/integration/nested-describe-spec.js \
162+
--config testFiles="nested-describe-spec.js" \
163+
--env grepTags=@does-not-exist \
164+
--pending 1
165+
151166
# repeat the selected test 3 times
152167
- name: Burn grepped test 🧪
153168
run: |

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,19 @@ This package comes with [src/index.d.ts](./src/index.d.ts) definition file that
193193

194194
## Test suites
195195

196-
The tags are also applied to the "describe" blocks with some limitations:
197-
198-
- you can only use the config object tags
196+
The tags are also applied to the "describe" blocks. In that case, the tests look up if any of their outer suites are enabled.
199197

200198
```js
201199
describe('block with config tag', { tags: '@smoke' }, () => {
202200
})
203201
```
204202

205-
- currently only the invert tag to skip the blog has meaningful effect. For example you can skip the above suite of tests by using `--env grepTags=-@smoke` value. Keep an eye on issue [#22](https://github.com/bahmutov/cypress-grep/issues/22) for the full support implementation.
203+
```
204+
# run any tests in the blocks having "@smoke" tag
205+
--env grepTags=@smoke
206+
# skip any blocks with "@smoke" tag
207+
--env grepTags=-@smoke
208+
```
206209

207210
See the [cypress/integration/describe-tags-spec.js](./cypress/integration/describe-tags-spec.js) file.
208211

cypress/integration/describe-tags-spec.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ describe('block with no tags', () => {
88
it('inside describe 2', () => {})
99
})
1010

11-
// WORKING: ignore the entire suite using invert option
12-
// --env grepTags=-@smoke
13-
// NOT WORKING: run all the tests in this suite only
14-
// --env grepTags=@smoke
1511
describe('block with tag smoke', { tags: '@smoke' }, () => {
1612
it('inside describe 3', () => {})
1713

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path="../../src/index.d.ts" />
2+
3+
// @ts-check
4+
describe('grand', () => {
5+
describe('outer', { tags: '@smoke' }, () => {
6+
describe('inner', () => {
7+
it('runs', () => {})
8+
})
9+
})
10+
})

expects/describe-tags-spec.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"inside describe 2": "pending"
55
},
66
"block with tag smoke": {
7-
"inside describe 3": "pending",
8-
"inside describe 4": "pending"
7+
"inside describe 3": "passed",
8+
"inside describe 4": "passed"
99
},
1010
"block without any tags": {
1111
"test with tag smoke": "passed"

expects/nested-describe-spec.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"grand": {
3+
"outer": {
4+
"inner": {
5+
"runs": "passing"
6+
}
7+
}
8+
}
9+
}

src/support.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,25 @@ function cypressGrep() {
5656
if (typeof configTags === 'string') {
5757
configTags = [configTags]
5858
}
59+
60+
const shouldRunBecauseOfParentSuite = suiteStack.includes(true)
5961
const shouldRun = shouldTestRun(parsedGrep, name, configTags)
6062
if (configTags && configTags.length) {
6163
debug(
6264
'should test "%s" with tags %s run? %s',
6365
name,
6466
configTags.join(','),
65-
shouldRun,
67+
shouldRunBecauseOfParentSuite || shouldRun,
6668
)
6769
} else {
68-
debug('should test "%s" run? %s', name, shouldRun)
70+
debug(
71+
'should test "%s" run? %s',
72+
name,
73+
shouldRunBecauseOfParentSuite || shouldRun,
74+
)
6975
}
7076

71-
if (shouldRun) {
77+
if (shouldRunBecauseOfParentSuite || shouldRun) {
7278
if (grepBurn > 1) {
7379
// repeat the same test to make sure it is solid
7480
return Cypress._.times(grepBurn, (k) => {
@@ -83,6 +89,12 @@ function cypressGrep() {
8389
return _it.skip(name, options, callback)
8490
}
8591

92+
// list of "describe" suites for the current test
93+
// when we encounter a new suite, we push it to the stack
94+
// when the "describe" function exits, we pop it
95+
// Thus a test can look up the tags from its parent suites
96+
const suiteStack = []
97+
8698
describe = function describeGrep(name, options, callback) {
8799
if (typeof options === 'function') {
88100
// the block has format describe('...', cb)
@@ -113,12 +125,15 @@ function cypressGrep() {
113125
const shouldRun = shouldTestRun(parsedGrep, configTags)
114126

115127
if (shouldRun) {
128+
suiteStack.push(true)
116129
_describe(name, options, callback)
130+
suiteStack.pop()
117131
return
118132
}
119133

120134
// skip tests without grep string in their names
121135
_describe.skip(name, options, callback)
136+
122137
return
123138
}
124139

0 commit comments

Comments
 (0)