Skip to content

Commit

Permalink
Core: Add QUnit.test.if() and QUnit.module.if()
Browse files Browse the repository at this point in the history
Cherry-picked from a165ab8 (3.0.0-dev):
> Closes #1772.

Co-authored-by: Steve McClure <[email protected]>
  • Loading branch information
Krinkle and smcclure15 committed Aug 18, 2024
1 parent a136726 commit e61f4cc
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ module.skip = function (name, options, scope) {
processModule(name, options, scope, { skip: true });
};

module.if = function (name, condition, options, scope) {
if (focused) {
return;
}

processModule(name, options, scope, { skip: !condition });
};

module.todo = function (name, options, scope) {
if (focused) {
return;
Expand Down
18 changes: 16 additions & 2 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default function Test (settings) {
});

if (this.skip) {
// Skipped tests will fully ignore any sent callback
// Skipped tests will fully ignore (and dereference for garbage collect) any sent callback
this.callback = function () {};
this.async = false;
this.expected = 0;
Expand Down Expand Up @@ -1021,6 +1021,9 @@ extend(test, {
skip: function (testName) {
addTest({ testName, skip: true });
},
if: function (testName, condition, callback) {
addTest({ testName, callback, skip: !condition });
},
only: function (testName, callback) {
addOnlyTest({ testName, callback });
},
Expand Down Expand Up @@ -1058,7 +1061,18 @@ test.skip.each = function (testName, dataset) {
});
});
};

test.if.each = function (testName, condition, dataset, callback) {
runEach(dataset, (data, testKey) => {
addTest({
testName: makeEachTestName(testName, testKey),
callback,
withData: true,
stackOffset: 5,
skip: !condition,
data: condition ? data : undefined
});
});
};
test.only.each = function (testName, dataset, callback) {
runEach(dataset, (data, testKey) => {
addOnlyTest({
Expand Down
37 changes: 37 additions & 0 deletions test/cli/fixtures/test-if.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
QUnit.test.if('skip me', false, function (assert) {
assert.true(false);
});

QUnit.test.if('keep me', true, function (assert) {
assert.true(true);
});

QUnit.test('regular', function (assert) {
assert.true(true);
});

QUnit.test.if.each('skip dataset', false, ['a', 'b'], function (assert, _data) {
assert.true(false);
});

QUnit.test.if.each('keep dataset', true, ['a', 'b'], function (assert, data) {
assert.true(true);
assert.equal(typeof data, 'string');
});

QUnit.module.if('skip group', false, function () {
QUnit.test('skipper', function (assert) {
assert.true(false);
});
});

QUnit.module.if('keep group', true, function (hooks) {
let list = [];
hooks.beforeEach(function () {
list.push('x');
});
QUnit.test('keeper', function (assert) {
assert.true(true);
assert.deepEqual(list, ['x']);
});
});
18 changes: 18 additions & 0 deletions test/cli/fixtures/test-if.tap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# name: no tests
# command: ["qunit", "test-if.js"]

TAP version 13
ok 1 # SKIP skip me
ok 2 keep me
ok 3 regular
ok 4 # SKIP skip dataset [0]
ok 5 # SKIP skip dataset [1]
ok 6 keep dataset [0]
ok 7 keep dataset [1]
ok 8 # SKIP skip group > skipper
ok 9 keep group > keeper
1..9
# pass 5
# skip 4
# todo 0
# fail 0
6 changes: 3 additions & 3 deletions test/main/deepEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ var hasES6Map = (function () {
}
}());

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

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

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

// Empty maps
Expand Down Expand Up @@ -2016,7 +2016,7 @@ var hasES6Symbol = (function () {
return typeof Symbol === 'function';
}());

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

Expand Down
4 changes: 2 additions & 2 deletions test/main/stacktrace.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Skip in environments without Error#stack support
(QUnit.stack() ? QUnit.module : QUnit.module.skip)('stacktrace', function () {
QUnit.module.if('stacktrace', !!QUnit.stack(), function () {
function fooCurrent () {
return QUnit.stack();
}
Expand Down Expand Up @@ -65,7 +65,7 @@
// We do that for failed assertions, but for passing tests we omit
// source details in these older browsers.
var supportsUnthrownStack = !!(new Error().stack);
(supportsUnthrownStack ? QUnit.module : QUnit.module.skip)('source details', function () {
QUnit.module.if('source details', supportsUnthrownStack, function () {
QUnit.test('QUnit.test()', function (assert) {
var stack = norm(QUnit.config.current.stack);
var line = stack.split('\n')[0];
Expand Down
2 changes: 1 addition & 1 deletion test/main/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ QUnit.module('test', function () {
assert.true(true);
});

(typeof document !== 'undefined' ? QUnit.module : QUnit.module.skip)('fixture management', function (hooks) {
QUnit.module.if('fixture management', typeof document !== 'undefined', function (hooks) {
/* global document */
var failure = false;
var values = [
Expand Down
3 changes: 2 additions & 1 deletion test/reorderError1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-env browser */
QUnit.module('Test call count - first case');
QUnit[window.sessionStorage ? 'test' : 'skip'](
QUnit.test.if(
'does not skip tests after reordering',
!!window.sessionStorage,
function (assert) {
assert.equal(window.totalCount, 3);
}
Expand Down
3 changes: 2 additions & 1 deletion test/reorderError2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-env browser */
QUnit.module('Test call count - second case');
QUnit[window.sessionStorage ? 'test' : 'skip'](
QUnit.test.if(
'does not skip tests after reordering',
!!window.sessionStorage,
function (assert) {
assert.equal(window.totalCount, 2);
}
Expand Down

0 comments on commit e61f4cc

Please sign in to comment.