@bahmutov/cy-grep 1.7.3 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -60,7 +60,9 @@ Watch the video [intro to cypress-grep plugin](https://www.youtube.com/watch?v=H
60
60
  - [Disable grep](#disable-grep)
61
61
  - [Burn (repeat) tests](#burn-repeat-tests)
62
62
  - [Required tags](#required-tags)
63
+ - [Negative grep](#negative-grep)
63
64
  - [TypeScript support](#typescript-support)
65
+ - [grepPrefixAt](#grepprefixat)
64
66
  - [General advice](#general-advice)
65
67
  - [DevTools console](#devtools-console)
66
68
  - [grepFailed](#grepfailed)
@@ -486,6 +488,26 @@ If `grepFilterSpecs=true` and a spec has only required tags, and you are running
486
488
 
487
489
  Read the blog post 📝 [Required Tags](https://glebbahmutov.com/blog/required-tags/).
488
490
 
491
+ ## Negative grep
492
+
493
+ When grepping tests by title, the parent suite title is included. For example if this is the spec
494
+
495
+ ```js
496
+ describe('users', () => {
497
+ it('works 1', () => {})
498
+ it('works 2', () => {})
499
+ it('works 3', () => {})
500
+ })
501
+
502
+ describe('projects', () => {
503
+ it('load 1', () => {})
504
+ it('load 2', () => {})
505
+ it('load 3', () => {})
506
+ })
507
+ ```
508
+
509
+ You can run the tests inside the suite "projects" by using `--env grep=projects` and you can skip the tests in the suite `projects` by using `--env grep=-projects`.
510
+
489
511
  ## TypeScript support
490
512
 
491
513
  Because the Cypress test config object type definition does not have the `tags` property we are using above, the TypeScript linter will show an error. Just add an ignore comment above the test:
@@ -517,6 +539,19 @@ If you have `tsconfig.json` file, add this library to the types list
517
539
  }
518
540
  ```
519
541
 
542
+ ## grepPrefixAt
543
+
544
+ Using test tags that start with `@` is so common, you can enforce it using the env option `grepPrefixAt: true`. This lets you use `@tag1,@tag2, ...` or `tag1,tag2, ...` when calling.
545
+
546
+ ```
547
+ # use grepPrefixAt in your env settings object
548
+ # use { tags: '@tag1' } in your tests
549
+
550
+ # then these two are equivalent
551
+ --env grepTags=@tag1
552
+ --env grepTags=tag1
553
+ ```
554
+
520
555
  ## General advice
521
556
 
522
557
  - keep it simple.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bahmutov/cy-grep",
3
- "version": "1.7.3",
3
+ "version": "1.9.0",
4
4
  "description": "Filter Cypress tests using title or tags",
5
5
  "main": "src/support.js",
6
6
  "scripts": {
package/src/plugin.js CHANGED
@@ -22,11 +22,13 @@ function getGrepSettings(config) {
22
22
  console.log('cy-grep: tests with "%s" in their names', grep.trim())
23
23
  }
24
24
 
25
+ const grepPrefixAt = env.grepPrefixAt || env['grep-prefix-at']
26
+
25
27
  const grepTags = env.grepTags || env['grep-tags']
26
28
 
27
29
  if (grepTags) {
28
30
  console.log('cy-grep: filtering using tag(s) "%s"', grepTags)
29
- const parsedGrep = parseGrep(null, grepTags)
31
+ const parsedGrep = parseGrep(null, grepTags, grepPrefixAt)
30
32
 
31
33
  debug('parsed grep tags %o', parsedGrep.tags)
32
34
  }
@@ -51,7 +53,11 @@ function getGrepSettings(config) {
51
53
 
52
54
  const grepFilterSpecs = env.grepFilterSpecs === true
53
55
 
54
- return { grep, grepTags, grepFilterSpecs }
56
+ if (grepPrefixAt) {
57
+ console.log('cy-grep: all tags will be forced to start with @')
58
+ }
59
+
60
+ return { grep, grepTags, grepFilterSpecs, grepPrefixAt }
55
61
  }
56
62
 
57
63
  /**
@@ -63,7 +69,8 @@ function cypressGrepPlugin(config) {
63
69
  return config
64
70
  }
65
71
 
66
- const { grep, grepTags, grepFilterSpecs } = getGrepSettings(config)
72
+ const { grep, grepTags, grepFilterSpecs, grepPrefixAt } =
73
+ getGrepSettings(config)
67
74
 
68
75
  if (grepFilterSpecs) {
69
76
  const specFiles = getSpecs(config)
@@ -74,20 +81,20 @@ function cypressGrepPlugin(config) {
74
81
 
75
82
  if (grep) {
76
83
  console.log('cy-grep: filtering specs using "%s" in the title', grep)
77
- const parsedGrep = parseGrep(grep)
84
+ const parsedGrep = parseGrep(grep, undefined, grepPrefixAt)
78
85
 
79
86
  debug('parsed grep %o', parsedGrep)
80
87
  greppedSpecs = specFiles.filter((specFile) => {
81
88
  const text = fs.readFileSync(specFile, { encoding: 'utf8' })
82
89
 
83
90
  try {
84
- const names = getTestNames(text, false)
85
- const testAndSuiteNames = names.suiteNames.concat(names.testNames)
91
+ const result = getTestNames(text, true)
92
+ const testNames = result.fullTestNames
86
93
 
87
94
  debug('spec file %s', specFile)
88
- debug('suite and test names: %o', testAndSuiteNames)
95
+ debug('full test names: %o', testNames)
89
96
 
90
- return testAndSuiteNames.some((name) => {
97
+ return testNames.some((name) => {
91
98
  const shouldRun = shouldTestRun(parsedGrep, name)
92
99
 
93
100
  return shouldRun
@@ -105,7 +112,7 @@ function cypressGrepPlugin(config) {
105
112
  debug('found grep "%s" in %d specs', grep, greppedSpecs.length)
106
113
  debug('%o', greppedSpecs)
107
114
  } else if (grepTags) {
108
- const parsedGrep = parseGrep(null, grepTags)
115
+ const parsedGrep = parseGrep(null, grepTags, grepPrefixAt)
109
116
  debug('parsed grep tags %o', parsedGrep)
110
117
  const mentionedTags = getMentionedTags(grepTags)
111
118
  debug('user mentioned tags %o', mentionedTags)
package/src/support.js CHANGED
@@ -61,12 +61,23 @@ function registerCyGrep() {
61
61
  getPluginConfigValue('grepOmitFiltered') ||
62
62
  getPluginConfigValue('grep-omit-filtered')
63
63
 
64
- debug('grep %o', { grep, grepTags, grepBurn, omitFiltered, version })
64
+ const grepPrefixAt =
65
+ getPluginConfigValue('grepPrefixAt') ||
66
+ getPluginConfigValue('grep-prefix-at')
67
+
68
+ debug('grep %o', {
69
+ grep,
70
+ grepTags,
71
+ grepBurn,
72
+ omitFiltered,
73
+ grepPrefixAt,
74
+ version,
75
+ })
65
76
  if (!Cypress._.isInteger(grepBurn) || grepBurn < 1) {
66
77
  throw new Error(`Invalid grep burn value: ${grepBurn}`)
67
78
  }
68
79
 
69
- const parsedGrep = parseGrep(grep, grepTags)
80
+ const parsedGrep = parseGrep(grep, grepTags, grepPrefixAt)
70
81
 
71
82
  debug('parsed grep %o', parsedGrep)
72
83
 
package/src/utils.js CHANGED
@@ -38,12 +38,13 @@ function parseFullTitleGrep(s) {
38
38
  /**
39
39
  * Parses tags to grep for.
40
40
  * @param {string|string[]} s Tags string like "@tag1+@tag2", or array of tags
41
+ * @param {boolean} grepPrefixAt Prefix all tags with "@" if needed
41
42
  * @example
42
43
  * parseTagsGrep('@tag1+@tag2')
43
44
  * @example
44
45
  * parseTagsGrep(['@tag1', '@tag2'])
45
46
  */
46
- function parseTagsGrep(s) {
47
+ function parseTagsGrep(s, grepPrefixAt = false) {
47
48
  if (!s) {
48
49
  return []
49
50
  }
@@ -99,6 +100,17 @@ function parseTagsGrep(s) {
99
100
  }
100
101
  }
101
102
 
103
+ if (grepPrefixAt) {
104
+ const prefix = '@'
105
+ ORS_filtered.forEach((OR) => {
106
+ OR.forEach((token) => {
107
+ if (token.tag && !token.tag.startsWith(prefix)) {
108
+ token.tag = prefix + token.tag
109
+ }
110
+ })
111
+ })
112
+ }
113
+
102
114
  return ORS_filtered
103
115
  }
104
116
 
@@ -229,10 +241,10 @@ function shouldTestRun(
229
241
  )
230
242
  }
231
243
 
232
- function parseGrep(titlePart, tags) {
244
+ function parseGrep(titlePart, tags, grepPrefixAt) {
233
245
  return {
234
246
  title: parseFullTitleGrep(titlePart),
235
- tags: parseTagsGrep(tags),
247
+ tags: parseTagsGrep(tags, grepPrefixAt),
236
248
  }
237
249
  }
238
250