@bahmutov/cy-grep 1.5.2 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bahmutov/cy-grep",
3
- "version": "1.5.2",
3
+ "version": "1.6.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
@@ -1,13 +1,12 @@
1
1
  // @ts-check
2
2
  const debug = require('debug')('cy-grep')
3
- const globby = require('globby')
4
3
 
5
4
  const { getSpecs } = require('find-cypress-specs')
6
5
  const { getTestNames, findEffectiveTestTags } = require('find-test-names')
7
6
  const fs = require('fs')
8
7
  const path = require('path')
9
8
  const { version } = require('../package.json')
10
- const { parseGrep, shouldTestRun } = require('./utils')
9
+ const { parseGrep, shouldTestRun, getMentionedTags } = require('./utils')
11
10
 
12
11
  const isCypressV9 = (config) => !('specPattern' in config)
13
12
 
@@ -107,8 +106,12 @@ function cypressGrepPlugin(config) {
107
106
  debug('%o', greppedSpecs)
108
107
  } else if (grepTags) {
109
108
  const parsedGrep = parseGrep(null, grepTags)
110
-
111
109
  debug('parsed grep tags %o', parsedGrep)
110
+ const mentionedTags = getMentionedTags(grepTags)
111
+ debug('user mentioned tags %o', mentionedTags)
112
+ // unique tags found across all specs we search
113
+ const foundTags = new Set()
114
+
112
115
  greppedSpecs = specFiles.filter((specFile) => {
113
116
  const text = fs.readFileSync(specFile, { encoding: 'utf8' })
114
117
 
@@ -121,6 +124,15 @@ function cypressGrepPlugin(config) {
121
124
  return Object.keys(testTags).some((testTitle) => {
122
125
  const effectiveTags = testTags[testTitle].effectiveTags
123
126
  const requiredTags = testTags[testTitle].requiredTags
127
+
128
+ // remember all found tags
129
+ effectiveTags.forEach((tag) => {
130
+ foundTags.add(tag)
131
+ })
132
+ requiredTags.forEach((tag) => {
133
+ foundTags.add(tag)
134
+ })
135
+
124
136
  return shouldTestRun(
125
137
  parsedGrep,
126
138
  undefined,
@@ -139,6 +151,17 @@ function cypressGrepPlugin(config) {
139
151
 
140
152
  debug('found grep tags "%s" in %d specs', grepTags, greppedSpecs.length)
141
153
  debug('%o', greppedSpecs)
154
+
155
+ debug('all found tags across the specs %o', ...foundTags)
156
+ debug('user mentioned tags %o', mentionedTags)
157
+ mentionedTags.forEach((tag) => {
158
+ if (!foundTags.has(tag)) {
159
+ console.warn(
160
+ 'cy-grep: could not find the tag "%s" in any of the specs',
161
+ tag,
162
+ )
163
+ }
164
+ })
142
165
  } else {
143
166
  // we have no tags to grep
144
167
  debug('will try eliminating specs with required tags')
@@ -191,10 +214,10 @@ function cypressGrepPlugin(config) {
191
214
  }
192
215
  } else {
193
216
  // hmm, we filtered out all specs, probably something is wrong
194
- console.warn('grep and/or grepTags has eliminated all specs')
195
- grep ? console.warn('grep: %s', grep) : null
196
- grepTags ? console.warn('grepTags: %s', grepTags) : null
197
- console.warn('Will leave all specs to run to filter at run-time')
217
+ console.warn('cy-grep: grep and/or grepTags has eliminated all specs')
218
+ grep ? console.warn('cy-grep: title: %s', grep) : null
219
+ grepTags ? console.warn('cy-grep: tags: %s', grepTags) : null
220
+ console.warn('cy-grep: Will leave all specs to run to filter at run-time')
198
221
  }
199
222
  }
200
223
 
package/src/utils.js CHANGED
@@ -95,6 +95,28 @@ function parseTagsGrep(s) {
95
95
  return ORS_filtered
96
96
  }
97
97
 
98
+ /**
99
+ * Given a user string of tags to find, with various connectors,
100
+ * returns the list of just the tags themselves. Could be used to
101
+ * quickly filter test specs or find misspelled tags.
102
+ * @returns {string[]} list of unique tags
103
+ */
104
+ function getMentionedTags(s) {
105
+ if (!s) {
106
+ return []
107
+ }
108
+ const spaced = s.replaceAll(/[+,]/g, ' ')
109
+ const tags = spaced
110
+ .split(' ')
111
+ .map((s) => s.trim())
112
+ .filter(Boolean)
113
+ // remove any "-" at the start of the tag
114
+ // because these are to signal inverted tags
115
+ .map((s) => (s.startsWith('-') ? s.slice(1) : s))
116
+ const uniqueTags = [...new Set(tags)]
117
+ return uniqueTags.sort()
118
+ }
119
+
98
120
  function shouldTestRunRequiredTags(parsedGrepTags, requiredTags = []) {
99
121
  if (!requiredTags.length) {
100
122
  // there are no tags to check
@@ -216,4 +238,5 @@ module.exports = {
216
238
  shouldTestRunTags,
217
239
  shouldTestRunRequiredTags,
218
240
  shouldTestRunTitle,
241
+ getMentionedTags,
219
242
  }