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

Commit 8e006e1

Browse files
authored
feat: allow using comma to separate OR tags (#103)
* feat: allow using comma to separate OR tags * remove extra spaces and commas
1 parent 5516e5b commit 8e006e1

File tree

5 files changed

+93
-35
lines changed

5 files changed

+93
-35
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ jobs:
217217
--env grepTags="high smoke" \
218218
--expect-exactly expects/tags-or.json
219219
220+
- name: Tags OR specs using commas 🧪
221+
env:
222+
# Cypress CLI has problems parsing --env values with commas
223+
# so pass the value using an environment variable
224+
CYPRESS_grepTags: 'high,smoke'
225+
run: |
226+
npx cypress-expect \
227+
--config testFiles="tags/*.spec.js" \
228+
--expect-exactly expects/tags-or.json
229+
220230
- name: Tags AND specs 🧪
221231
run: |
222232
npx cypress-expect \

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# cypress-grep
2+
23
[![ci status][ci image]][ci url] [![badges status][badges image]][badges url] [![renovate-app badge][renovate-badge]][renovate-app] ![cypress version](https://img.shields.io/badge/cypress-8.7.0-brightgreen)
4+
35
> Filter tests using substring
46
57
```shell
@@ -183,6 +185,13 @@ You can select tests to run or skip using tags by passing `--env grepTags=...` v
183185
--env grep=hello,grepTags=smoke
184186
```
185187

188+
If you can pass commas in the environment variable `grepTags`, you can use `,` to separate the tags
189+
190+
```
191+
# enable the tests with tag "one" or "two"
192+
CYPRESS_grepTags=one,two npx cypress run
193+
```
194+
186195
### Tags in the test config object
187196

188197
Cypress tests can have their own [test config object](https://on.cypress.io/configuration#Test-Configuration), and when using this plugin you can put the test tags there, either as a single tag string or as an array of tags.
@@ -301,8 +310,7 @@ If you have `tsconfig.json` file, add this library to the types list
301310
The tags are also applied to the "describe" blocks. In that case, the tests look up if any of their outer suites are enabled.
302311

303312
```js
304-
describe('block with config tag', { tags: '@smoke' }, () => {
305-
})
313+
describe('block with config tag', { tags: '@smoke' }, () => {})
306314
```
307315

308316
```

cypress/integration/unit.js

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('utils', () => {
7676
])
7777
})
7878

79-
it('parses OR tags', () => {
79+
it('parses OR tags spaces', () => {
8080
// run tests with tag1 OR tag2 or tag3
8181
const parsed = parseTagsGrep('@tag1 @tag2 @tag3')
8282
expect(parsed).to.deep.equal([
@@ -86,6 +86,16 @@ describe('utils', () => {
8686
])
8787
})
8888

89+
it('parses OR tags commas', () => {
90+
// run tests with tag1 OR tag2 or tag3
91+
const parsed = parseTagsGrep('@tag1,@tag2,@tag3')
92+
expect(parsed).to.deep.equal([
93+
[{ tag: '@tag1', invert: false }],
94+
[{ tag: '@tag2', invert: false }],
95+
[{ tag: '@tag3', invert: false }],
96+
])
97+
})
98+
8999
it('parses inverted tag', () => {
90100
const parsed = parseTagsGrep('-@tag1')
91101
expect(parsed).to.deep.equal([[{ tag: '@tag1', invert: true }]])
@@ -99,6 +109,30 @@ describe('utils', () => {
99109
])
100110
})
101111

112+
it('forgives extra spaces', () => {
113+
const parsed = parseTagsGrep(' @tag1 -@tag2 ')
114+
expect(parsed).to.deep.equal([
115+
[{ tag: '@tag1', invert: false }],
116+
[{ tag: '@tag2', invert: true }],
117+
])
118+
})
119+
120+
it('parses tag1 but not tag2 with comma', () => {
121+
const parsed = parseTagsGrep('@tag1,-@tag2')
122+
expect(parsed).to.deep.equal([
123+
[{ tag: '@tag1', invert: false }],
124+
[{ tag: '@tag2', invert: true }],
125+
])
126+
})
127+
128+
it('filters out empty tags', () => {
129+
const parsed = parseTagsGrep(',, @tag1,-@tag2,, ,, ,')
130+
expect(parsed).to.deep.equal([
131+
[{ tag: '@tag1', invert: false }],
132+
[{ tag: '@tag2', invert: true }],
133+
])
134+
})
135+
102136
// would need to change the tokenizer
103137
it.skip('parses tag1 but not tag2', () => {
104138
const parsed = parseTagsGrep('@tag1-@tag2')
@@ -319,22 +353,22 @@ describe('utils', () => {
319353
).to.be.true
320354
})
321355

322-
it("Multiple invert strings and a simple one", () => {
323-
const t = checkName("-name;-hey;number")
324-
expect(t("number should only be matches without a n-a-m-e")).to.be.true
356+
it('Multiple invert strings and a simple one', () => {
357+
const t = checkName('-name;-hey;number')
358+
expect(t('number should only be matches without a n-a-m-e')).to.be.true
325359
expect(t("number can't be name")).to.be.false
326-
expect(t("The man needs a name")).to.be.false
327-
expect(t("number hey name")).to.be.false
328-
expect(t("numbers hey name")).to.be.false
329-
expect(t("number hsey nsame")).to.be.true
330-
expect(t("This wont match")).to.be.false
360+
expect(t('The man needs a name')).to.be.false
361+
expect(t('number hey name')).to.be.false
362+
expect(t('numbers hey name')).to.be.false
363+
expect(t('number hsey nsame')).to.be.true
364+
expect(t('This wont match')).to.be.false
331365
})
332366

333-
it("Only inverted strings", () => {
334-
const t = checkName("-name;-hey")
367+
it('Only inverted strings', () => {
368+
const t = checkName('-name;-hey')
335369
expect(t("I'm matched")).to.be.true
336370
expect(t("hey! I'm not")).to.be.false
337-
expect(t("My name is weird")).to.be.false
371+
expect(t('My name is weird')).to.be.false
338372
})
339373
})
340374

src/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function cypressGrepPlugin(config) {
2424

2525
const grepTags = config.env.grepTags || config.env['grep-tags']
2626
if (grepTags) {
27-
console.log('cypress-grep: filtering using tag "%s"', grepTags)
27+
console.log('cypress-grep: filtering using tag(s) "%s"', grepTags)
2828
}
2929

3030
const grepBurn =

src/utils.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,30 @@ function parseTagsGrep(s) {
4343
return []
4444
}
4545

46-
// top level split - using space, each part is OR
47-
const ORS = s.split(' ').map((part) => {
48-
// now every part is an AND
49-
const parsed = part.split('+').map((tag) => {
50-
if (tag.startsWith('-')) {
46+
// top level split - using space or comma, each part is OR
47+
const ORS = s
48+
.split(/[ ,]/)
49+
// remove any empty tags
50+
.filter(Boolean)
51+
.map((part) => {
52+
// now every part is an AND
53+
const parsed = part.split('+').map((tag) => {
54+
if (tag.startsWith('-')) {
55+
return {
56+
tag: tag.slice(1),
57+
invert: true,
58+
}
59+
}
60+
5161
return {
52-
tag: tag.slice(1),
53-
invert: true,
62+
tag,
63+
invert: false,
5464
}
55-
}
65+
})
5666

57-
return {
58-
tag,
59-
invert: false,
60-
}
67+
return parsed
6168
})
6269

63-
return parsed
64-
})
65-
6670
return ORS
6771
}
6872

@@ -111,12 +115,14 @@ function shouldTestRunTitle(parsedGrep, testName) {
111115
return true
112116
}
113117

114-
const inverted = parsedGrep.filter(g => g.invert)
115-
const straight = parsedGrep.filter(g => !g.invert)
118+
const inverted = parsedGrep.filter((g) => g.invert)
119+
const straight = parsedGrep.filter((g) => !g.invert)
116120

117-
return inverted.every(titleGrep => !testName.includes(titleGrep.title))
118-
&& (!straight.length || straight
119-
.some(titleGrep => testName.includes(titleGrep.title)))
121+
return (
122+
inverted.every((titleGrep) => !testName.includes(titleGrep.title)) &&
123+
(!straight.length ||
124+
straight.some((titleGrep) => testName.includes(titleGrep.title)))
125+
)
120126
}
121127

122128
// note: tags take precedence over the test name

0 commit comments

Comments
 (0)