Skip to content

Commit 344aacf

Browse files
authored
fix: dry run returns no tests when using a regex grep (#4608)
1 parent 446d8e3 commit 344aacf

File tree

2 files changed

+49
-35
lines changed

2 files changed

+49
-35
lines changed

lib/command/dryRun.js

+30-35
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const store = require('../store')
77
const Container = require('../container')
88

99
module.exports = async function (test, options) {
10-
if (options.grep) process.env.grep = options.grep.toLowerCase()
10+
if (options.grep) process.env.grep = options.grep
1111
const configFile = options.config
1212
let codecept
1313

@@ -60,35 +60,40 @@ function printTests(files) {
6060
let numOfTests = 0
6161
let numOfSuites = 0
6262
let outputString = ''
63-
const filterBy = process.env.grep ? process.env.grep.toLowerCase() : undefined
63+
const filterBy = process.env.grep
6464

65+
let filterRegex
6566
if (filterBy) {
66-
for (const suite of mocha.suite.suites) {
67-
const currentSuite = suite.title
68-
if (suite.title.toLowerCase().includes(filterBy)) {
69-
outputString += `${colors.white.bold(suite.title)} -- ${output.styles.log(suite.file || '')} -- ${mocha.suite.suites.length} tests\n`
70-
numOfSuites++
71-
}
72-
73-
for (test of suite.tests) {
74-
if (test.title.toLowerCase().includes(filterBy)) {
75-
numOfTests++
76-
outputString += `${colors.white.bold(test.parent.title)} -- ${output.styles.log(test.parent.file || '')} -- ${mocha.suite.suites.length} tests\n`
77-
outputString += ` ${output.styles.scenario(figures.checkboxOff)} ${test.title}\n`
78-
}
79-
}
67+
try {
68+
filterRegex = new RegExp(filterBy, 'i') // Case-insensitive matching
69+
} catch (err) {
70+
console.error(`Invalid grep pattern: ${filterBy}`)
71+
process.exit(1)
8072
}
81-
numOfSuites = countSuites(outputString)
82-
} else {
83-
for (const suite of mocha.suite.suites) {
84-
output.print(
85-
`${colors.white.bold(suite.title)} -- ${output.styles.log(suite.file || '')} -- ${mocha.suite.suites.length} tests`,
86-
)
73+
}
74+
75+
for (const suite of mocha.suite.suites) {
76+
const suiteMatches = filterRegex ? filterRegex.test(suite.title) : true
77+
let suiteHasMatchingTests = false
78+
79+
if (suiteMatches) {
80+
outputString += `${colors.white.bold(suite.title)} -- ${output.styles.log(suite.file || '')}\n`
81+
suiteHasMatchingTests = true
8782
numOfSuites++
83+
}
84+
85+
for (const test of suite.tests) {
86+
const testMatches = filterRegex ? filterRegex.test(test.title) : true
87+
88+
if (testMatches) {
89+
if (!suiteMatches && !suiteHasMatchingTests) {
90+
outputString += `${colors.white.bold(suite.title)} -- ${output.styles.log(suite.file || '')}\n`
91+
suiteHasMatchingTests = true
92+
numOfSuites++
93+
}
8894

89-
for (test of suite.tests) {
9095
numOfTests++
91-
output.print(` ${output.styles.scenario(figures.checkboxOff)} ${test.title}`)
96+
outputString += ` ${output.styles.scenario(figures.checkboxOff)} ${test.title}\n`
9297
}
9398
}
9499
}
@@ -108,15 +113,5 @@ function printFooter() {
108113
function removeDuplicates(inputString) {
109114
const array = inputString.split('\n')
110115
const uniqueLines = [...new Set(array)]
111-
const resultString = uniqueLines.join('\n')
112-
113-
return resultString
114-
}
115-
116-
function countSuites(inputString) {
117-
const array = inputString.split('\n')
118-
119-
const uniqueLines = [...new Set(array)]
120-
const res = uniqueLines.filter((item) => item.includes('-- '))
121-
return res.length
116+
return uniqueLines.join('\n')
122117
}

test/runner/dry_run_test.js

+19
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ describe('dry-run command', () => {
117117
})
118118
})
119119

120+
it('should run feature files with regex grep', (done) => {
121+
exec(codecept_run_config('codecept.bdd.js') + ' --steps --grep "(?=.*Checkout process)"', (err, stdout) => {
122+
//eslint-disable-line
123+
expect(stdout).toContain('Checkout process') // feature
124+
expect(stdout).toContain('-- before checkout --')
125+
expect(stdout).toContain('-- after checkout --')
126+
// expect(stdout).toContain('In order to buy products'); // test name
127+
expect(stdout).toContain('Given I have product with $600 price')
128+
expect(stdout).toContain('And I have product with $1000 price')
129+
expect(stdout).toContain('Then I should see that total number of products is 2')
130+
expect(stdout).toContain('And my order amount is $1600')
131+
expect(stdout).not.toContain('I add item 600') // 'Given' actor's non-gherkin step check
132+
expect(stdout).not.toContain('I see sum 1600') // 'And' actor's non-gherkin step check
133+
expect(stdout).toContain('No tests were executed')
134+
expect(err).toBeFalsy()
135+
done()
136+
})
137+
})
138+
120139
it('should print substeps in debug mode', (done) => {
121140
exec(codecept_run_config('codecept.bdd.js') + ' --debug --grep "Checkout process @important"', (err, stdout) => {
122141
//eslint-disable-line

0 commit comments

Comments
 (0)