@bahmutov/cy-grep 1.4.2 → 1.5.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.
Files changed (3) hide show
  1. package/README.md +16 -4
  2. package/package.json +7 -5
  3. package/src/plugin.js +42 -28
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # @bahmutov/cy-grep ![cypress version](https://img.shields.io/badge/cypress-12.4.1-brightgreen)
1
+ # @bahmutov/cy-grep ![cypress version](https://img.shields.io/badge/cypress-12.7.0-brightgreen)
2
2
 
3
3
  > Filter tests using substring or tag
4
4
 
@@ -19,13 +19,25 @@ All other tests will be marked pending, see why in the [Cypress test statuses](h
19
19
 
20
20
  If you have multiple spec files, all specs will be loaded, and every test will be filtered the same way, since the grep is run-time operation and cannot eliminate the spec files without loading them. If you want to run only specific tests, use the built-in [--spec](https://on.cypress.io/command-line#cypress-run-spec-lt-spec-gt) CLI argument.
21
21
 
22
- Watch the video [intro to cypress-grep plugin](https://www.youtube.com/watch?v=HS-Px-Sghd8) and study my 🎓 Cypress course [Cypress Plugins](https://cypress.tips/courses/cypress-plugins).
22
+ ## Training
23
23
 
24
- Table of Contents
24
+ Watch the video [intro to cypress-grep plugin](https://www.youtube.com/watch?v=HS-Px-Sghd8) and study my 🎓 Cypress course [Cypress Plugins](https://cypress.tips/courses/cypress-plugins):
25
+
26
+ - [Lesson k1: Set up the test grep plugin](https://cypress.tips/courses/cypress-plugins/lessons/k1)
27
+ - [Lesson k2: Filter the tests using test and suite tags](https://cypress.tips/courses/cypress-plugins/lessons/k2)
28
+ - [Lesson k3: Filter the specs without the tag we want to run](https://cypress.tips/courses/cypress-plugins/lessons/k3)
29
+ - [Lesson k4: Filter the tests to run using several tags](https://cypress.tips/courses/cypress-plugins/lessons/k4)
30
+ - [Lesson k5: Filter the tests to run using OR of several tags](https://cypress.tips/courses/cypress-plugins/lessons/k5)
31
+ - [Lesson k6: Repeat selected tests N times](https://cypress.tips/courses/cypress-plugins/lessons/k6)
32
+ - [Lesson k7: Pick the tests to run in the interactive mode](https://cypress.tips/courses/cypress-plugins/lessons/k7)
33
+
34
+ ## Table of Contents
25
35
 
26
36
  <!-- MarkdownTOC autolink="true" -->
27
37
 
28
38
  - [@bahmutov/cy-grep ](#bahmutovcy-grep-)
39
+ - [Training](#training)
40
+ - [Table of Contents](#table-of-contents)
29
41
  - [Install](#install)
30
42
  - [Support file](#support-file)
31
43
  - [Config file](#config-file)
@@ -72,7 +84,7 @@ npm i -D @bahmutov/cy-grep
72
84
  yarn add -D @bahmutov/cy-grep
73
85
  ```
74
86
 
75
- **Note**: @bahmutov/cy-grep only works with Cypress version >= 10.
87
+ **Note**: @bahmutov/cy-grep should work with all Cypress versions, but I mostly test it on the newest versions.
76
88
 
77
89
  ### Support file
78
90
 
package/package.json CHANGED
@@ -1,26 +1,28 @@
1
1
  {
2
2
  "name": "@bahmutov/cy-grep",
3
- "version": "1.4.2",
3
+ "version": "1.5.0",
4
4
  "description": "Filter Cypress tests using title or tags",
5
5
  "main": "src/support.js",
6
6
  "scripts": {
7
7
  "cy:run": "cypress run --config specPattern='**/unit.js'",
8
8
  "cy:open": "cypress open --e2e -b electron --config specPattern='**/unit.js'",
9
9
  "badges": "npx -p dependency-version-badge update-badge cypress",
10
- "semantic-release": "semantic-release"
10
+ "semantic-release": "semantic-release",
11
+ "deps": "npm audit --report --omit dev"
11
12
  },
12
13
  "dependencies": {
13
14
  "cypress-plugin-config": "^1.2.0",
14
15
  "debug": "^4.3.2",
15
- "find-test-names": "1.26.0",
16
+ "find-cypress-specs": "^1.29.4",
17
+ "find-test-names": "1.28.1",
16
18
  "globby": "^11.0.4"
17
19
  },
18
20
  "devDependencies": {
19
- "cypress": "12.4.1",
21
+ "cypress": "12.7.0",
20
22
  "cypress-each": "^1.11.0",
21
23
  "cypress-expect": "^3.1.0",
22
24
  "prettier": "^2.8.1",
23
- "semantic-release": "^20.1.0",
25
+ "semantic-release": "^20.1.1",
24
26
  "typescript": "^4.7.4"
25
27
  },
26
28
  "peerDependencies": {
package/src/plugin.js CHANGED
@@ -1,27 +1,19 @@
1
+ // @ts-check
1
2
  const debug = require('debug')('cy-grep')
2
3
  const globby = require('globby')
4
+
5
+ const { getSpecs } = require('find-cypress-specs')
3
6
  const { getTestNames, findEffectiveTestTags } = require('find-test-names')
4
7
  const fs = require('fs')
8
+ const path = require('path')
5
9
  const { version } = require('../package.json')
6
10
  const { parseGrep, shouldTestRun } = require('./utils')
7
11
 
8
- /**
9
- * Prints the cy-grep environment values if any.
10
- * @param {Cypress.ConfigOptions} config
11
- */
12
- function cypressGrepPlugin(config) {
13
- if (!config || !config.env) {
14
- return config
15
- }
12
+ const isCypressV9 = (config) => !('specPattern' in config)
16
13
 
14
+ function getGrepSettings(config) {
17
15
  const { env } = config
18
16
 
19
- if (!config.specPattern) {
20
- throw new Error(
21
- 'Incompatible versions detected, cy-grep requires Cypress 10.0.0+',
22
- )
23
- }
24
-
25
17
  debug('cy-grep plugin version %s', version)
26
18
  debug('Cypress config env object: %o', env)
27
19
 
@@ -58,20 +50,24 @@ function cypressGrepPlugin(config) {
58
50
  console.log('cy-grep: will omit filtered tests')
59
51
  }
60
52
 
61
- const { specPattern, excludeSpecPattern } = config
62
- const integrationFolder = env.grepIntegrationFolder || process.cwd()
63
-
64
53
  const grepFilterSpecs = env.grepFilterSpecs === true
65
54
 
55
+ return { grep, grepTags, grepFilterSpecs }
56
+ }
57
+
58
+ /**
59
+ * Prints the cy-grep environment values if any.
60
+ * @param {Cypress.ConfigOptions} config
61
+ */
62
+ function cypressGrepPlugin(config) {
63
+ if (!config || !config.env) {
64
+ return config
65
+ }
66
+
67
+ const { grep, grepTags, grepFilterSpecs } = getGrepSettings(config)
68
+
66
69
  if (grepFilterSpecs) {
67
- debug('specPattern', specPattern)
68
- debug('excludeSpecPattern', excludeSpecPattern)
69
- debug('integrationFolder', integrationFolder)
70
- const specFiles = globby.sync(specPattern, {
71
- cwd: integrationFolder,
72
- ignore: excludeSpecPattern,
73
- absolute: true,
74
- })
70
+ const specFiles = getSpecs(config)
75
71
 
76
72
  debug('found %d spec files', specFiles.length)
77
73
  debug('%o', specFiles)
@@ -86,7 +82,7 @@ function cypressGrepPlugin(config) {
86
82
  const text = fs.readFileSync(specFile, { encoding: 'utf8' })
87
83
 
88
84
  try {
89
- const names = getTestNames(text)
85
+ const names = getTestNames(text, false)
90
86
  const testAndSuiteNames = names.suiteNames.concat(names.testNames)
91
87
 
92
88
  debug('spec file %s', specFile)
@@ -127,7 +123,7 @@ function cypressGrepPlugin(config) {
127
123
  const requiredTags = testTags[testTitle].requiredTags
128
124
  return shouldTestRun(
129
125
  parsedGrep,
130
- null,
126
+ undefined,
131
127
  effectiveTags,
132
128
  false,
133
129
  requiredTags,
@@ -173,7 +169,25 @@ function cypressGrepPlugin(config) {
173
169
  }
174
170
 
175
171
  if (greppedSpecs.length) {
176
- config.specPattern = greppedSpecs
172
+ if (isCypressV9(config)) {
173
+ debug('setting selected %d specs (< v10)', greppedSpecs.length)
174
+ // @ts-ignore
175
+ const integrationFolder = config.integrationFolder
176
+ const relativeNames = greppedSpecs.map((filename) =>
177
+ path.relative(integrationFolder, filename),
178
+ )
179
+ debug(
180
+ 'specs in the integration folder %s %s',
181
+ integrationFolder,
182
+ relativeNames.join(', '),
183
+ )
184
+ // @ts-ignore
185
+ config.testFiles = relativeNames.join(',')
186
+ } else {
187
+ debug('setting selected %d specs (>= v10)', greppedSpecs.length)
188
+ // @ts-ignore
189
+ config.specPattern = greppedSpecs
190
+ }
177
191
  } else {
178
192
  // hmm, we filtered out all specs, probably something is wrong
179
193
  console.warn('grep and/or grepTags has eliminated all specs')