Skip to content

Commit bb7eb94

Browse files
gbudjeakpKrinkle
authored andcommitted
Core: Add QUnit.config.testFilter to programmatically filter tests
1 parent 9de4ec6 commit bb7eb94

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

src/core/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const config = {
2929
// Select by pattern or case-insensitive substring match against "moduleName: testName"
3030
filter: undefined,
3131

32+
testFilter: null,
33+
3234
// TODO: Make explicit in QUnit 3.
3335
// fixture: undefined,
3436

src/test.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -881,15 +881,34 @@ Test.prototype = {
881881
}
882882

883883
const filter = config.filter;
884-
if (!filter) {
885-
return true;
884+
if (filter) {
885+
const regexFilter = /^(!?)\/([\w\W]*)\/(i?$)/.exec(filter);
886+
const fullName = (this.module.name + ': ' + this.testName);
887+
if (regexFilter) {
888+
if (!this.regexFilter(!!regexFilter[1], regexFilter[2], regexFilter[3], fullName)) {
889+
return false;
890+
}
891+
} else if (!this.stringFilter(filter, fullName)) {
892+
return false;
893+
}
894+
}
895+
896+
if (typeof config.testFilter === 'function') {
897+
const testInfo = {
898+
testId: this.testId,
899+
testName: this.testName,
900+
module: this.module.name,
901+
skip: !!this.skip
902+
};
903+
try {
904+
return !!config.testFilter(testInfo);
905+
} catch (error) {
906+
Logger.warn('Error in QUnit.config.testFilter callback: ' + (error.message || error));
907+
return false;
908+
}
886909
}
887910

888-
const regexFilter = /^(!?)\/([\w\W]*)\/(i?$)/.exec(filter);
889-
const fullName = (this.module.name + ': ' + this.testName);
890-
return regexFilter
891-
? this.regexFilter(!!regexFilter[1], regexFilter[2], regexFilter[3], fullName)
892-
: this.stringFilter(filter, fullName);
911+
return true;
893912
},
894913

895914
regexFilter: function (exclude, pattern, flags, fullName) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const quarantineList = ['flaky test', 'broken test'];
2+
3+
QUnit.config.testFilter = function (testInfo) {
4+
return !quarantineList.some(function (pattern) {
5+
return testInfo.testName.indexOf(pattern) !== -1;
6+
});
7+
};
8+
9+
QUnit.module('testFilter demo');
10+
11+
QUnit.test('stable test', function (assert) {
12+
assert.true(true, 'this test should run');
13+
});
14+
15+
QUnit.test('flaky test', function (assert) {
16+
assert.true(false, 'this test should be filtered out');
17+
});
18+
19+
QUnit.test('broken test', function (assert) {
20+
assert.true(false, 'this test should also be filtered out');
21+
});
22+
23+
QUnit.test('another stable test', function (assert) {
24+
assert.true(true, 'this test should also run');
25+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# name: config.testFilter
2+
# command: ["qunit", "config-testFilter.js"]
3+
4+
TAP version 13
5+
ok 1 testFilter demo > stable test
6+
ok 2 testFilter demo > another stable test
7+
1..2
8+
# pass 2
9+
# skip 0
10+
# todo 0
11+
# fail 0

0 commit comments

Comments
 (0)