Skip to content

Commit 465da13

Browse files
Krinklesmcclure15
andcommitted
Core: Add QUnit.test.if() and QUnit.module.if()
Cherry-picked from a165ab8 (3.0.0-dev): > Closes #1772. Co-authored-by: Steve McClure <[email protected]>
1 parent 2fb6b9f commit 465da13

File tree

9 files changed

+89
-10
lines changed

9 files changed

+89
-10
lines changed

src/module.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ module.skip = function (name, options, scope) {
167167
processModule(name, options, scope, { skip: true });
168168
};
169169

170+
module.if = function (name, condition, options, scope) {
171+
if (focused) {
172+
return;
173+
}
174+
175+
processModule(name, options, scope, { skip: !condition });
176+
};
177+
170178
module.todo = function (name, options, scope) {
171179
if (focused) {
172180
return;

src/test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export default function Test (settings) {
111111
});
112112

113113
if (this.skip) {
114-
// Skipped tests will fully ignore any sent callback
114+
// Skipped tests will fully ignore (and dereference for garbage collect) any sent callback
115115
this.callback = function () {};
116116
this.async = false;
117117
this.expected = 0;
@@ -1021,6 +1021,9 @@ extend(test, {
10211021
skip: function (testName) {
10221022
addTest({ testName, skip: true });
10231023
},
1024+
if: function (testName, condition, callback) {
1025+
addTest({ testName, callback, skip: !condition });
1026+
},
10241027
only: function (testName, callback) {
10251028
addOnlyTest({ testName, callback });
10261029
},
@@ -1058,7 +1061,18 @@ test.skip.each = function (testName, dataset) {
10581061
});
10591062
});
10601063
};
1061-
1064+
test.if.each = function (testName, condition, dataset, callback) {
1065+
runEach(dataset, (data, testKey) => {
1066+
addTest({
1067+
testName: makeEachTestName(testName, testKey),
1068+
callback,
1069+
withData: true,
1070+
stackOffset: 5,
1071+
skip: !condition,
1072+
data: condition ? data : undefined
1073+
});
1074+
});
1075+
};
10621076
test.only.each = function (testName, dataset, callback) {
10631077
runEach(dataset, (data, testKey) => {
10641078
addOnlyTest({

test/cli/fixtures/test-if.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
QUnit.test.if('skip me', false, function (assert) {
2+
assert.true(false);
3+
});
4+
5+
QUnit.test.if('keep me', true, function (assert) {
6+
assert.true(true);
7+
});
8+
9+
QUnit.test('regular', function (assert) {
10+
assert.true(true);
11+
});
12+
13+
QUnit.test.if.each('skip dataset', false, ['a', 'b'], function (assert, _data) {
14+
assert.true(false);
15+
});
16+
17+
QUnit.test.if.each('keep dataset', true, ['a', 'b'], function (assert, data) {
18+
assert.true(true);
19+
assert.equal(typeof data, 'string');
20+
});
21+
22+
QUnit.module.if('skip group', false, function () {
23+
QUnit.test('skipper', function (assert) {
24+
assert.true(false);
25+
});
26+
});
27+
28+
QUnit.module.if('keep group', true, function (hooks) {
29+
let list = [];
30+
hooks.beforeEach(function () {
31+
list.push('x');
32+
});
33+
QUnit.test('keeper', function (assert) {
34+
assert.true(true);
35+
assert.deepEqual(list, ['x']);
36+
});
37+
});

test/cli/fixtures/test-if.tap.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# name: no tests
2+
# command: ["qunit", "test-if.js"]
3+
4+
TAP version 13
5+
ok 1 # SKIP skip me
6+
ok 2 keep me
7+
ok 3 regular
8+
ok 4 # SKIP skip dataset [0]
9+
ok 5 # SKIP skip dataset [1]
10+
ok 6 keep dataset [0]
11+
ok 7 keep dataset [1]
12+
ok 8 # SKIP skip group > skipper
13+
ok 9 keep group > keeper
14+
1..9
15+
# pass 5
16+
# skip 4
17+
# todo 0
18+
# fail 0

test/main/deepEqual.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ var hasES6Map = (function () {
18251825
}
18261826
}());
18271827

1828-
QUnit[hasES6Set ? 'test' : 'skip']('Sets', function (assert) {
1828+
QUnit.test.if('Sets', hasES6Set, function (assert) {
18291829
var s1, s2, s3, s4, o1, o2, o3, o4, m1, m2, m3;
18301830

18311831
// Empty sets
@@ -1898,7 +1898,7 @@ QUnit[hasES6Set ? 'test' : 'skip']('Sets', function (assert) {
18981898
assert.equal(QUnit.equiv(s1, s2), true, 'Sets with different insertion orders');
18991899
});
19001900

1901-
QUnit[hasES6Map ? 'test' : 'skip']('Maps', function (assert) {
1901+
QUnit.test.if('Maps', hasES6Map, function (assert) {
19021902
var m1, m2, m3, m4, o1, o2, o3, o4, s1, s2, s3;
19031903

19041904
// Empty maps
@@ -2016,7 +2016,7 @@ var hasES6Symbol = (function () {
20162016
return typeof Symbol === 'function';
20172017
}());
20182018

2019-
QUnit[hasES6Symbol ? 'test' : 'skip']('Symbols', function (assert) {
2019+
QUnit.test.if('Symbols', hasES6Symbol, function (assert) {
20202020
var a = Symbol(1);
20212021
var b = Symbol(1);
20222022

test/main/stacktrace.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Skip in environments without Error#stack support
2-
(QUnit.stack() ? QUnit.module : QUnit.module.skip)('stacktrace', function () {
2+
QUnit.module.if('stacktrace', !!QUnit.stack(), function () {
33
function fooCurrent () {
44
return QUnit.stack();
55
}
@@ -65,7 +65,7 @@
6565
// We do that for failed assertions, but for passing tests we omit
6666
// source details in these older browsers.
6767
var supportsUnthrownStack = !!(new Error().stack);
68-
(supportsUnthrownStack ? QUnit.module : QUnit.module.skip)('source details', function () {
68+
QUnit.module.if('source details', supportsUnthrownStack, function () {
6969
QUnit.test('QUnit.test()', function (assert) {
7070
var stack = norm(QUnit.config.current.stack);
7171
var line = stack.split('\n')[0];

test/main/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ QUnit.module('test', function () {
88
assert.true(true);
99
});
1010

11-
(typeof document !== 'undefined' ? QUnit.module : QUnit.module.skip)('fixture management', function (hooks) {
11+
QUnit.module.if('fixture management', typeof document !== 'undefined', function (hooks) {
1212
/* global document */
1313
var failure = false;
1414
var values = [

test/reorderError1.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint-env browser */
22
QUnit.module('Test call count - first case');
3-
QUnit[window.sessionStorage ? 'test' : 'skip'](
3+
QUnit.test.if(
44
'does not skip tests after reordering',
5+
!!window.sessionStorage,
56
function (assert) {
67
assert.equal(window.totalCount, 3);
78
}

test/reorderError2.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint-env browser */
22
QUnit.module('Test call count - second case');
3-
QUnit[window.sessionStorage ? 'test' : 'skip'](
3+
QUnit.test.if(
44
'does not skip tests after reordering',
5+
!!window.sessionStorage,
56
function (assert) {
67
assert.equal(window.totalCount, 2);
78
}

0 commit comments

Comments
 (0)