@bahmutov/cy-grep 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/README.md +14 -1
  2. package/package.json +4 -4
  3. package/src/plugin.js +35 -1
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # @bahmutov/cy-grep ![cypress version](https://img.shields.io/badge/cypress-12.3.0-brightgreen)
1
+ # @bahmutov/cy-grep ![cypress version](https://img.shields.io/badge/cypress-12.4.0-brightgreen)
2
2
 
3
3
  > Filter tests using substring or tag
4
4
 
@@ -119,6 +119,15 @@ registerCypressGrep()
119
119
 
120
120
  Installing the plugin via `setupNodeEvents()` is required to enable the [grepFilterSpecs](#pre-filter-specs-grepfilterspecs) feature.
121
121
 
122
+ **Tip:** you probably want to set these `env` settings in your config file
123
+
124
+ ```js
125
+ module.exports = defineConfig({
126
+ env: { grepFilterSpecs: true, grepOmitFiltered: true },
127
+ ...
128
+ })
129
+ ```
130
+
122
131
  ## Usage Overview
123
132
 
124
133
  You can filter tests to run using part of their title via `grep`, and via explicit tags via `grepTags` Cypress environment variables.
@@ -421,6 +430,10 @@ it('cleans up the data', { requiredTags: '@nightly' }, () => {...})
421
430
 
422
431
  When you run the tests now, this test will be skipped, as if it were `it.skip`. It will only run if you use the tag `@nightly`, for example: `npx cypress run --env grepTags=@nightly`.
423
432
 
433
+ If `grepFilterSpecs=true` and a spec has only required tags, and you are running without any tags, the the spec will be skipped completely.
434
+
435
+ Read the blog post 📝 [Required Tags](https://glebbahmutov.com/blog/required-tags/).
436
+
424
437
  ## TypeScript support
425
438
 
426
439
  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:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bahmutov/cy-grep",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Filter Cypress tests using title or tags",
5
5
  "main": "src/support.js",
6
6
  "scripts": {
@@ -16,11 +16,11 @@
16
16
  "globby": "^11.0.4"
17
17
  },
18
18
  "devDependencies": {
19
- "cypress": "12.3.0",
19
+ "cypress": "12.4.0",
20
20
  "cypress-each": "^1.11.0",
21
- "cypress-expect": "^2.5.3",
21
+ "cypress-expect": "^3.1.0",
22
22
  "prettier": "^2.8.1",
23
- "semantic-release": "^20.0.3",
23
+ "semantic-release": "^20.0.4",
24
24
  "typescript": "^4.7.4"
25
25
  },
26
26
  "peerDependencies": {
package/src/plugin.js CHANGED
@@ -124,7 +124,14 @@ function cypressGrepPlugin(config) {
124
124
  debug('effective test tags %o', testTags)
125
125
  return Object.keys(testTags).some((testTitle) => {
126
126
  const effectiveTags = testTags[testTitle].effectiveTags
127
- return shouldTestRun(parsedGrep, null, effectiveTags)
127
+ const requiredTags = testTags[testTitle].requiredTags
128
+ return shouldTestRun(
129
+ parsedGrep,
130
+ null,
131
+ effectiveTags,
132
+ false,
133
+ requiredTags,
134
+ )
128
135
  })
129
136
  } catch (err) {
130
137
  console.error('Could not determine test names in file: %s', specFile)
@@ -136,6 +143,33 @@ function cypressGrepPlugin(config) {
136
143
 
137
144
  debug('found grep tags "%s" in %d specs', grepTags, greppedSpecs.length)
138
145
  debug('%o', greppedSpecs)
146
+ } else {
147
+ // we have no tags to grep
148
+ debug('will try eliminating specs with required tags')
149
+
150
+ greppedSpecs = specFiles.filter((specFile) => {
151
+ const text = fs.readFileSync(specFile, { encoding: 'utf8' })
152
+
153
+ try {
154
+ const testTags = findEffectiveTestTags(text)
155
+ debug('spec file %s', specFile)
156
+ debug('effective test tags %o', testTags)
157
+ // eliminate all tests with required tags, since we have no tags right now
158
+ const testsWithoutRequiredTags = Object.keys(testTags).filter(
159
+ (testTitle) => {
160
+ return testTags[testTitle].requiredTags.length === 0
161
+ },
162
+ )
163
+ // if there are any tests remaining, we should run this spec
164
+ // (we should not run empty specs where all tests have required tags)
165
+ return testsWithoutRequiredTags.length
166
+ } catch (err) {
167
+ console.error('Could not determine test names in file: %s', specFile)
168
+ console.error('Will run it to let the grep filter the tests')
169
+
170
+ return true
171
+ }
172
+ })
139
173
  }
140
174
 
141
175
  if (greppedSpecs.length) {