From e61f4cc18ba2f338099feb1e9645b2fde2462e3f Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sun, 18 Aug 2024 20:07:53 +0100 Subject: [PATCH] Core: Add `QUnit.test.if()` and `QUnit.module.if()` Cherry-picked from a165ab8205aa9547c6b8a79bbdbc328ff7f4689c (3.0.0-dev): > Closes https://github.com/qunitjs/qunit/pull/1772. Co-authored-by: Steve McClure --- src/module.js | 8 +++++++ src/test.js | 18 +++++++++++++-- test/cli/fixtures/test-if.js | 37 +++++++++++++++++++++++++++++++ test/cli/fixtures/test-if.tap.txt | 18 +++++++++++++++ test/main/deepEqual.js | 6 ++--- test/main/stacktrace.js | 4 ++-- test/main/test.js | 2 +- test/reorderError1.js | 3 ++- test/reorderError2.js | 3 ++- 9 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 test/cli/fixtures/test-if.js create mode 100644 test/cli/fixtures/test-if.tap.txt diff --git a/src/module.js b/src/module.js index a6c610bc4..34ed22315 100644 --- a/src/module.js +++ b/src/module.js @@ -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; diff --git a/src/test.js b/src/test.js index bef2db735..7dfe9b5b7 100644 --- a/src/test.js +++ b/src/test.js @@ -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; @@ -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 }); }, @@ -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({ diff --git a/test/cli/fixtures/test-if.js b/test/cli/fixtures/test-if.js new file mode 100644 index 000000000..dad46a95a --- /dev/null +++ b/test/cli/fixtures/test-if.js @@ -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']); + }); +}); diff --git a/test/cli/fixtures/test-if.tap.txt b/test/cli/fixtures/test-if.tap.txt new file mode 100644 index 000000000..fc4ead929 --- /dev/null +++ b/test/cli/fixtures/test-if.tap.txt @@ -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 diff --git a/test/main/deepEqual.js b/test/main/deepEqual.js index 8b14a6385..5cf50451d 100644 --- a/test/main/deepEqual.js +++ b/test/main/deepEqual.js @@ -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 @@ -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 @@ -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); diff --git a/test/main/stacktrace.js b/test/main/stacktrace.js index 6767f29b7..b3b779cd9 100644 --- a/test/main/stacktrace.js +++ b/test/main/stacktrace.js @@ -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(); } @@ -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]; diff --git a/test/main/test.js b/test/main/test.js index eb4e455ba..86204958d 100644 --- a/test/main/test.js +++ b/test/main/test.js @@ -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 = [ diff --git a/test/reorderError1.js b/test/reorderError1.js index f9020d282..8d7e51e58 100644 --- a/test/reorderError1.js +++ b/test/reorderError1.js @@ -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); } diff --git a/test/reorderError2.js b/test/reorderError2.js index d56a85c0c..953a6e6c6 100644 --- a/test/reorderError2.js +++ b/test/reorderError2.js @@ -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); }