Skip to content

Commit 6b5a785

Browse files
craigtaubboneskull
authored andcommitted
Fix messages for file lookup issues - error scenario (#3654); see #3646
Scenario where multiple files (all) are not found only show a single message. I feel its useful for debugging to keep the message if only 1 file is not found. Happy to remove if others don't agree. No changes where at least 1 is found (still prints msg). See PR #3650 for full details ## Scenarios ### Nothing given Command: `mocha` Output: ``` Error: No test files found: "test/" ``` ### Nothing found Command: `mocha -R json-stream --no-config "test/integration/fixtures/glob/**/*-none.js"` Output: ``` Error: No test files found: "test/integration/fixtures/glob/**/*-none.js" ``` ### Multiple not found Command: `mocha -R json-stream --no-config "test/integration/fixtures/glob/**/*-none.js" "test/integration/fixtures/glob/**/*-none-again.js"` New output ``` Error: No test files found ``` Previous output ``` Error: Cannot find any files matching pattern "test/integration/fixtures/glob/**/*-none.js" Error: Cannot find any files matching pattern "test/integration/fixtures/glob/**/*-none-again.js" ``` ## Applicable issues (semver-patch)
1 parent fb59d98 commit 6b5a785

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

lib/cli/run-helpers.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,14 @@ exports.handleFiles = ({
137137
spec = []
138138
} = {}) => {
139139
let files = [];
140-
const errors = [];
140+
const unmatched = [];
141141
spec.forEach(arg => {
142142
let newFiles;
143143
try {
144144
newFiles = utils.lookupFiles(arg, extension, recursive);
145145
} catch (err) {
146146
if (err.code === 'ERR_MOCHA_NO_FILES_MATCH_PATTERN') {
147-
errors.push(err.message);
147+
unmatched.push({message: err.message, pattern: err.pattern});
148148
return;
149149
}
150150

@@ -164,15 +164,17 @@ exports.handleFiles = ({
164164
});
165165

166166
if (!files.length) {
167-
// print messages as an error
168-
errors.forEach(message => {
169-
console.error(ansi.red(`Error: ${message}`));
170-
});
167+
// give full message details when only 1 file is missing
168+
const noneFoundMsg =
169+
unmatched.length === 1
170+
? `Error: No test files found: ${JSON.stringify(unmatched[0].pattern)}` // stringify to print escaped characters raw
171+
: 'Error: No test files found';
172+
console.error(ansi.red(noneFoundMsg));
171173
process.exit(1);
172174
} else {
173175
// print messages as an warning
174-
errors.forEach(message => {
175-
console.warn(ansi.yellow(`Warning: ${message}`));
176+
unmatched.forEach(warning => {
177+
console.warn(ansi.yellow(`Warning: ${warning.message}`));
176178
});
177179
}
178180

test/integration/glob.spec.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,24 @@ describe('globbing', function() {
2828
expect(
2929
results.stderr,
3030
'to contain',
31-
'Error: Cannot find any files matching pattern "./*-none.js"'
31+
'Error: No test files found: "./*-none.js"'
3232
);
3333
},
3434
done
3535
);
3636
});
3737

38+
it('should handle multiple non-matching patterns', function(done) {
39+
testGlob.shouldFail(
40+
'./*-none.js ./*-none-twice.js',
41+
function(results) {
42+
expect(results.stderr, 'to contain', 'Error: No test files found');
43+
expect(results.stderr, 'not to contain', '*-none');
44+
},
45+
done
46+
);
47+
});
48+
3849
it('should handle both matching and non-matching patterns in the same command', function(done) {
3950
testGlob.shouldSucceed(
4051
'./*.js ./*-none.js',
@@ -77,13 +88,23 @@ describe('globbing', function() {
7788
expect(
7889
results.stderr,
7990
'to contain',
80-
'Error: Cannot find any files matching pattern "./*-none.js"'
91+
'Error: No test files found: "./*-none.js"'
8192
);
8293
},
8394
done
8495
);
8596
});
8697

98+
it('should handle multiple non-matching patterns', function(done) {
99+
testGlob.shouldFail(
100+
'"./*-none.js" "./*-none-twice.js"',
101+
function(results) {
102+
expect(results.stderr, 'to contain', 'Error: No test files found');
103+
},
104+
done
105+
);
106+
});
107+
87108
it('should handle both matching and non-matching patterns in the same command', function(done) {
88109
testGlob.shouldSucceed(
89110
'"./*.js" "./*-none.js"',
@@ -125,7 +146,7 @@ describe('globbing', function() {
125146
expect(
126147
results.stderr,
127148
'to contain',
128-
'Error: Cannot find any files matching pattern "./**/*-none.js"'
149+
'Error: No test files found: "./**/*-none.js"'
129150
);
130151
},
131152
done

0 commit comments

Comments
 (0)