From 2d68830411ead95c37631b73cd88d8a32b08dd38 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Tue, 21 Apr 2020 16:47:38 -0700 Subject: [PATCH] feat(testrunner): support `--file` to filter tests by test file name. (#1903) --- test/test.js | 39 ++++++++++++++++++++++++++------------- utils/testrunner/index.js | 19 +++++++++++++++++-- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/test/test.js b/test/test.js index 2b40ee55f4..b115d9834e 100644 --- a/test/test.js +++ b/test/test.js @@ -190,19 +190,6 @@ function collect(browserNames) { delete global[key]; } - const filterArgIndex = process.argv.indexOf('--filter'); - if (filterArgIndex !== -1) { - const filter = process.argv[filterArgIndex + 1]; - testRunner.focusMatchingTests(new RegExp(filter, 'i')); - } - - const repeatArgIndex = process.argv.indexOf('--repeat'); - if (repeatArgIndex !== -1) { - const repeat = parseInt(process.argv[repeatArgIndex + 1], 10); - if (!isNaN(repeat)) - testRunner.repeatAll(repeat); - } - return testRunner; } @@ -214,5 +201,31 @@ if (require.main === module) { return process.env.BROWSER === name || process.env.BROWSER === 'all'; }); const testRunner = collect(browserNames); + + const filterArgIndex = process.argv.indexOf('--filter'); + if (filterArgIndex !== -1) { + const filter = process.argv[filterArgIndex + 1]; + if (!testRunner.focusMatchingNameTests(new RegExp(filter, 'i')).length) { + console.log('ERROR: no tests matched given `--filter` regex.'); + process.exit(1); + } + } + + const fileArgIndex = process.argv.indexOf('--file'); + if (fileArgIndex !== -1) { + const filter = process.argv[fileArgIndex + 1]; + if (!testRunner.focusMatchingFilePath(new RegExp(filter, 'i')).length) { + console.log('ERROR: no files matched given `--file` regex.'); + process.exit(1); + } + } + + const repeatArgIndex = process.argv.indexOf('--repeat'); + if (repeatArgIndex !== -1) { + const repeat = parseInt(process.argv[repeatArgIndex + 1], 10); + if (!isNaN(repeat)) + testRunner.repeatAll(repeat); + } + testRunner.run().then(() => { delete global.expect; }); } diff --git a/utils/testrunner/index.js b/utils/testrunner/index.js index fec946e103..6f16752a83 100644 --- a/utils/testrunner/index.js +++ b/utils/testrunner/index.js @@ -82,11 +82,26 @@ class DefaultTestRunner { return this._api; } - focusMatchingTests(fullNameRegex) { + focusMatchingNameTests(fullNameRegex) { + const focusedTests = []; for (const test of this._collector.tests()) { - if (fullNameRegex.test(test.fullName())) + if (fullNameRegex.test(test.fullName())) { this._filter.markFocused(test); + focusedTests.push(test); + } } + return focusedTests; + } + + focusMatchingFilePath(filepathRegex) { + const focusedTests = []; + for (const test of this._collector.tests()) { + if (filepathRegex.test(test.location().filePath())) { + this._filter.markFocused(test); + focusedTests.push(test); + } + } + return focusedTests; } repeatAll(repeatCount) {