Skip to content
This repository was archived by the owner on Mar 29, 2023. It is now read-only.

Commit c2eb157

Browse files
joshuajtwardAdam Murray
authored andcommitted
feat: add Cypress 10 support (setupNodeEvents only)
BREAKING CHANGE: this version will only support config from <= Cypress 10
1 parent 1a2bd34 commit c2eb157

File tree

3 files changed

+87
-71
lines changed

3 files changed

+87
-71
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ cypress-grep: tests with "hello" in their names
7373

7474
Installing the plugin in the project's plugin file is also required to enable the [grepFilterSpecs](#grepfilterspecs) feature.
7575

76+
### setupNodeEvents
77+
78+
To use this module with [Cypress v10.0.0](https://docs.cypress.io/guides/references/migration-guide#Migrating-to-Cypress-version-10-0) and above, add the following to your [config file](https://docs.cypress.io/guides/references/configuration):
79+
80+
```js
81+
{
82+
env: { grepNewConfig: true },
83+
e2e: {
84+
setupNodeEvents(on, config) {
85+
require('cypress-grep/src/plugin')(config);
86+
87+
return config;
88+
},
89+
}
90+
}
91+
```
92+
7693
## Use
7794

7895
Start grepping by title and tags:

src/plugin.js

Lines changed: 48 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { getTestNames } = require('find-test-names')
44
const fs = require('fs')
55
const path = require('path')
66
const { version } = require('../package.json')
7-
const { parseGrep, shouldTestRun } = require('./utils')
7+
const { resolveConfig, parseGrep, shouldTestRun } = require('./utils')
88

99
/**
1010
* Prints the cypress-grep environment values if any.
@@ -15,64 +15,63 @@ function cypressGrepPlugin(config) {
1515
return config
1616
}
1717

18-
debug('Cypress config env object: %o', config.env)
19-
debug('plugin version %s', version)
20-
const grep = config.env.grep ? String(config.env.grep) : undefined
18+
const { env } = config
19+
20+
debug(
21+
'Config type: %s',
22+
env.grepNewConfig
23+
? 'new (Cypress version >= v10.0.0)'
24+
: 'legacy (Cypress version < v10.0.0)',
25+
)
26+
debug('cypress-grep plugin version %s', version)
27+
debug('Cypress config env object: %o', env)
28+
29+
const grep = env.grep ? String(env.grep) : undefined
2130
if (grep) {
2231
console.log('cypress-grep: tests with "%s" in their names', grep.trim())
2332
}
2433

25-
const grepTags = config.env.grepTags || config.env['grep-tags']
34+
const grepTags = env.grepTags || env['grep-tags']
2635
if (grepTags) {
2736
console.log('cypress-grep: filtering using tag(s) "%s"', grepTags)
2837
const parsedGrep = parseGrep(null, grepTags)
2938
debug('parsed grep tags %o', parsedGrep.tags)
3039
}
3140

32-
const grepBurn =
33-
config.env.grepBurn || config.env['grep-burn'] || config.env.burn
41+
const grepBurn = env.grepBurn || env['grep-burn'] || env.burn
3442
if (grepBurn) {
3543
console.log('cypress-grep: running filtered tests %d times', grepBurn)
3644
}
3745

38-
const grepUntagged = config.env.grepUntagged || config.env['grep-untagged']
46+
const grepUntagged = env.grepUntagged || env['grep-untagged']
3947
if (grepUntagged) {
4048
console.log('cypress-grep: running untagged tests')
4149
}
4250

43-
const omitFiltered =
44-
config.env.grepOmitFiltered || config.env['grep-omit-filtered']
51+
const omitFiltered = env.grepOmitFiltered || env['grep-omit-filtered']
4552
if (omitFiltered) {
4653
console.log('cypress-grep: will omit filtered tests')
4754
}
4855

49-
const grepFilterSpecs = config.env.grepFilterSpecs === true
56+
const { resolvedConfig } = resolveConfig(config)
57+
const { specPattern, excludeSpecPattern, integrationFolder } = resolvedConfig
58+
59+
const grepFilterSpecs = env.grepFilterSpecs === true
5060
if (grepFilterSpecs) {
61+
debug(resolvedConfig)
62+
const specFiles = globby.sync(specPattern, {
63+
ignore: excludeSpecPattern,
64+
absolute: false,
65+
})
66+
debug('found %d spec files', specFiles.length)
67+
debug('%o', specFiles)
68+
let greppedSpecs = []
5169
if (grep) {
5270
console.log('cypress-grep: filtering specs using "%s" in the title', grep)
53-
54-
debug({
55-
integrationFolder: config.integrationFolder,
56-
testFiles: config.testFiles,
57-
ignoreTestFiles: config.ignoreTestFiles,
58-
})
59-
60-
const specFiles = globby.sync(config.testFiles, {
61-
cwd: config.integrationFolder,
62-
ignore: config.ignoreTestFiles,
63-
absolute: false,
64-
})
65-
debug('found %d spec files', specFiles.length)
66-
debug('%o', specFiles)
67-
6871
const parsedGrep = parseGrep(grep)
6972
debug('parsed grep %o', parsedGrep)
70-
71-
const specsWithText = specFiles.filter((specFile) => {
72-
const text = fs.readFileSync(
73-
path.join(config.integrationFolder, specFile),
74-
'utf8',
75-
)
73+
greppedSpecs = specFiles.filter((specFile) => {
74+
const text = fs.readFileSync(specFile, { encoding: 'utf8' })
7675
try {
7776
const names = getTestNames(text)
7877
const testAndSuiteNames = names.suiteNames.concat(names.testNames)
@@ -91,36 +90,13 @@ function cypressGrepPlugin(config) {
9190
return true
9291
}
9392
})
94-
95-
debug('found grep "%s" in %d specs', grep, specsWithText.length)
96-
debug('%o', specsWithText)
97-
98-
config.testFiles = specsWithText
93+
debug('found grep "%s" in %d specs', grep, greppedSpecs.length)
94+
debug('%o', greppedSpecs)
9995
} else if (grepTags) {
100-
console.log('cypress-grep: filtering specs using tag "%s"', grepTags)
101-
102-
debug({
103-
integrationFolder: config.integrationFolder,
104-
testFiles: config.testFiles,
105-
ignoreTestFiles: config.ignoreTestFiles,
106-
})
107-
108-
const specFiles = globby.sync(config.testFiles, {
109-
cwd: config.integrationFolder,
110-
ignore: config.ignoreTestFiles,
111-
absolute: false,
112-
})
113-
debug('found %d spec files', specFiles.length)
114-
debug('%o', specFiles)
115-
11696
const parsedGrep = parseGrep(null, grepTags)
11797
debug('parsed grep tags %o', parsedGrep)
118-
119-
const specsWithText = specFiles.filter((specFile) => {
120-
const text = fs.readFileSync(
121-
path.join(config.integrationFolder, specFile),
122-
'utf8',
123-
)
98+
greppedSpecs = specFiles.filter((specFile) => {
99+
const text = fs.readFileSync(specFile, { encoding: 'utf8' })
124100
try {
125101
const testInfo = getTestNames(text)
126102
debug('spec file %s', specFile)
@@ -136,17 +112,19 @@ function cypressGrepPlugin(config) {
136112
return true
137113
}
138114
})
139-
140-
debug('found grep tags "%s" in %d specs', grepTags, specsWithText.length)
141-
debug('%o', specsWithText)
142-
143-
if (specsWithText.length) {
144-
config.testFiles = specsWithText
145-
} else {
146-
// hmm, we filtered out all specs, probably something is wrong
147-
console.warn('Grep "%s" has eliminated all specs', grep)
148-
console.warn('Will leave all specs to run to filter at run-time')
149-
}
115+
debug('found grep tags "%s" in %d specs', grepTags, greppedSpecs.length)
116+
debug('%o', greppedSpecs)
117+
}
118+
if (greppedSpecs.length) {
119+
env.grepNewConfig
120+
? (config.specPattern = greppedSpecs)
121+
: (config.testFiles = greppedSpecs)
122+
} else {
123+
// hmm, we filtered out all specs, probably something is wrong
124+
console.warn('grep and/or grepTags has eliminated all specs')
125+
grep ? console.warn('grep: %s', grep) : null
126+
grepTags ? console.warn('grepTags: %s', grepTags) : null
127+
console.warn('Will leave all specs to run to filter at run-time')
150128
}
151129
}
152130

src/utils.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function parseTitleGrep(s) {
1515
s = s.trim()
1616
if (s.startsWith('-')) {
1717
return {
18-
title: s.substr(1),
18+
title: s.substring(1),
1919
invert: true,
2020
}
2121
}
@@ -166,11 +166,32 @@ function parseGrep(titlePart, tags) {
166166
}
167167
}
168168

169+
function resolveConfig(config) {
170+
const specPattern = config.testFiles || config.specPattern || ''
171+
let excludeSpecPattern =
172+
config.ignoreTestFiles || config.excludeSpecPattern || ''
173+
if (typeof excludeSpecPattern === 'string')
174+
excludeSpecPattern = [excludeSpecPattern]
175+
const integrationFolder =
176+
config.integrationFolder ||
177+
config.env.grepIntegrationFolder ||
178+
process.cwd()
179+
180+
return {
181+
resolvedConfig: {
182+
specPattern,
183+
excludeSpecPattern,
184+
integrationFolder,
185+
},
186+
}
187+
}
188+
169189
module.exports = {
170190
parseGrep,
171191
parseTitleGrep,
172192
parseFullTitleGrep,
173193
parseTagsGrep,
194+
resolveConfig,
174195
shouldTestRun,
175196
shouldTestRunTags,
176197
shouldTestRunTitle,

0 commit comments

Comments
 (0)