@bahmutov/cy-grep 1.4.3 → 1.5.1

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 +34 -2
  2. package/package.json +5 -3
  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.5.0-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
 
@@ -41,6 +41,7 @@ Watch the video [intro to cypress-grep plugin](https://www.youtube.com/watch?v=H
41
41
  - [Install](#install)
42
42
  - [Support file](#support-file)
43
43
  - [Config file](#config-file)
44
+ - [Install in Cypress versions before 10](#install-in-cypress-versions-before-10)
44
45
  - [Usage Overview](#usage-overview)
45
46
  - [Filter by test title](#filter-by-test-title)
46
47
  - [OR substring matching](#or-substring-matching)
@@ -84,7 +85,7 @@ npm i -D @bahmutov/cy-grep
84
85
  yarn add -D @bahmutov/cy-grep
85
86
  ```
86
87
 
87
- **Note**: @bahmutov/cy-grep only works with Cypress version >= 10.
88
+ **Note**: @bahmutov/cy-grep should work with all Cypress versions, but I mostly test it on the newest versions.
88
89
 
89
90
  ### Support file
90
91
 
@@ -140,6 +141,37 @@ module.exports = defineConfig({
140
141
  })
141
142
  ```
142
143
 
144
+ ### Install in Cypress versions before 10
145
+
146
+ See [test-cy-v9](./test-cy-v9/) for example
147
+
148
+ ```js
149
+ // cypress/plugins/index.js
150
+ module.exports = (on, config) => {
151
+ // `on` is used to hook into various events Cypress emits
152
+ // `config` is the resolved Cypress config
153
+ require('@bahmutov/cy-grep/src/plugin')(config)
154
+ // IMPORTANT: return the config object
155
+ return config
156
+ }
157
+ ```
158
+
159
+ ```js
160
+ // cypress/support/index.js
161
+ require('@bahmutov/cy-grep')()
162
+ ```
163
+
164
+ Put the common settings into `cypress.json`
165
+
166
+ ```json
167
+ {
168
+ "env": {
169
+ "grepOmitFiltered": true,
170
+ "grepFilterSpecs": true
171
+ }
172
+ }
173
+ ```
174
+
143
175
  ## Usage Overview
144
176
 
145
177
  You can filter tests to run using part of their title via `grep`, and via explicit tags via `grepTags` Cypress environment variables.
package/package.json CHANGED
@@ -1,17 +1,19 @@
1
1
  {
2
2
  "name": "@bahmutov/cy-grep",
3
- "version": "1.4.3",
3
+ "version": "1.5.1",
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",
16
+ "find-cypress-specs": "^1.29.4",
15
17
  "find-test-names": "1.28.1",
16
18
  "globby": "^11.0.4"
17
19
  },
@@ -24,7 +26,7 @@
24
26
  "typescript": "^4.7.4"
25
27
  },
26
28
  "peerDependencies": {
27
- "cypress": ">=10"
29
+ "cypress": ">=8"
28
30
  },
29
31
  "files": [
30
32
  "src"
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')