@@ -7,7 +7,7 @@ const store = require('../store')
7
7
const Container = require ( '../container' )
8
8
9
9
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
11
11
const configFile = options . config
12
12
let codecept
13
13
@@ -60,35 +60,40 @@ function printTests(files) {
60
60
let numOfTests = 0
61
61
let numOfSuites = 0
62
62
let outputString = ''
63
- const filterBy = process . env . grep ? process . env . grep . toLowerCase ( ) : undefined
63
+ const filterBy = process . env . grep
64
64
65
+ let filterRegex
65
66
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 )
80
72
}
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
87
82
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
+ }
88
94
89
- for ( test of suite . tests ) {
90
95
numOfTests ++
91
- output . print ( ` ${ output . styles . scenario ( figures . checkboxOff ) } ${ test . title } ` )
96
+ outputString += ` ${ output . styles . scenario ( figures . checkboxOff ) } ${ test . title } \n`
92
97
}
93
98
}
94
99
}
@@ -108,15 +113,5 @@ function printFooter() {
108
113
function removeDuplicates ( inputString ) {
109
114
const array = inputString . split ( '\n' )
110
115
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' )
122
117
}
0 commit comments