Skip to content

Commit 2d68830

Browse files
authored
feat(testrunner): support --file to filter tests by test file name. (#1903)
1 parent 89b2fe5 commit 2d68830

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

test/test.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,34 @@ function collect(browserNames) {
190190
delete global[key];
191191
}
192192

193+
return testRunner;
194+
}
195+
196+
module.exports = collect;
197+
198+
if (require.main === module) {
199+
console.log('Testing on Node', process.version);
200+
const browserNames = ['chromium', 'firefox', 'webkit'].filter(name => {
201+
return process.env.BROWSER === name || process.env.BROWSER === 'all';
202+
});
203+
const testRunner = collect(browserNames);
204+
193205
const filterArgIndex = process.argv.indexOf('--filter');
194206
if (filterArgIndex !== -1) {
195207
const filter = process.argv[filterArgIndex + 1];
196-
testRunner.focusMatchingTests(new RegExp(filter, 'i'));
208+
if (!testRunner.focusMatchingNameTests(new RegExp(filter, 'i')).length) {
209+
console.log('ERROR: no tests matched given `--filter` regex.');
210+
process.exit(1);
211+
}
212+
}
213+
214+
const fileArgIndex = process.argv.indexOf('--file');
215+
if (fileArgIndex !== -1) {
216+
const filter = process.argv[fileArgIndex + 1];
217+
if (!testRunner.focusMatchingFilePath(new RegExp(filter, 'i')).length) {
218+
console.log('ERROR: no files matched given `--file` regex.');
219+
process.exit(1);
220+
}
197221
}
198222

199223
const repeatArgIndex = process.argv.indexOf('--repeat');
@@ -203,16 +227,5 @@ function collect(browserNames) {
203227
testRunner.repeatAll(repeat);
204228
}
205229

206-
return testRunner;
207-
}
208-
209-
module.exports = collect;
210-
211-
if (require.main === module) {
212-
console.log('Testing on Node', process.version);
213-
const browserNames = ['chromium', 'firefox', 'webkit'].filter(name => {
214-
return process.env.BROWSER === name || process.env.BROWSER === 'all';
215-
});
216-
const testRunner = collect(browserNames);
217230
testRunner.run().then(() => { delete global.expect; });
218231
}

utils/testrunner/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,26 @@ class DefaultTestRunner {
8282
return this._api;
8383
}
8484

85-
focusMatchingTests(fullNameRegex) {
85+
focusMatchingNameTests(fullNameRegex) {
86+
const focusedTests = [];
8687
for (const test of this._collector.tests()) {
87-
if (fullNameRegex.test(test.fullName()))
88+
if (fullNameRegex.test(test.fullName())) {
8889
this._filter.markFocused(test);
90+
focusedTests.push(test);
91+
}
8992
}
93+
return focusedTests;
94+
}
95+
96+
focusMatchingFilePath(filepathRegex) {
97+
const focusedTests = [];
98+
for (const test of this._collector.tests()) {
99+
if (filepathRegex.test(test.location().filePath())) {
100+
this._filter.markFocused(test);
101+
focusedTests.push(test);
102+
}
103+
}
104+
return focusedTests;
90105
}
91106

92107
repeatAll(repeatCount) {

0 commit comments

Comments
 (0)