Skip to content

Commit

Permalink
Core: Add automatic labels in test.each() for primitive values in arrays
Browse files Browse the repository at this point in the history
Fixes #1733.
  • Loading branch information
Krinkle committed Sep 5, 2024
1 parent 0231379 commit aab5d75
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 5 deletions.
13 changes: 13 additions & 0 deletions docs/api/QUnit/test.each.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ Each test case is passed one value of your dataset.

The [`only`](./test.only.md), [`todo`](./test.todo.md), [`skip`](./test.skip.md), and [`if`](./test.if.md) variants are also available, as `QUnit.test.only.each`, `QUnit.test.todo.each`, `QUnit.test.skip.each`, and `QUnit.test.if.each` respectively.

## Changelog

| UNRELEASED | Add [automatic labels](https://github.com/qunitjs/qunit/issues/1733) for primitive values in arrays.
| [QUnit 2.16.0](https://github.com/qunitjs/qunit/releases/tag/2.16.0) | Introduce `QUnit.test.each()`.

## Examples

### Basic data provider
Expand All @@ -50,6 +55,14 @@ function isEven (x) {
QUnit.test.each('isEven()', [2, 4, 6], (assert, data) => {
assert.true(isEven(data), `${data} is even`);
});

QUnit.test.each('truthy', ['a', 42, true, Infinity], (assert, data) => {
assert.true(!!data);
});

QUnit.test.each('falsy', [false, null], (assert, data) => {
assert.false(!!data);
});
```

### Array data provider
Expand Down
14 changes: 13 additions & 1 deletion src/core/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,19 @@ function makeEachTestName (testName, argument) {
function runEach (data, eachFn) {
if (Array.isArray(data)) {
for (let i = 0; i < data.length; i++) {
eachFn(data[i], i);
const value = data[i];
const valueType = typeof value;
let testKey;
if (valueType === 'string' || valueType === 'number' || valueType === 'boolean' || valueType === 'undefined' || value === null) {
const valueForName = String(value);
testKey = i + ': ' + (valueForName.length <= 30
? valueForName
: valueForName.slice(0, 29) + '…'
);
} else {
testKey = i;
}
eachFn(value, testKey);
}
} else if (typeof data === 'object' && data !== null) {
for (let key in data) {
Expand Down
44 changes: 44 additions & 0 deletions test/cli/fixtures/each-array-labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Automatic labels for test.each() array data where possible
// https://github.com/qunitjs/qunit/issues/1733

QUnit.test.each('array of arrays', [[1, 2, 3], [1, 1, 2]], function (assert, data) {
assert.true(true);
});

QUnit.test.each('array of strings', [
'foo',
'bar',
'',
' ',
'_',
'+',
'^',
'$',
'x'.repeat(30),
'y'.repeat(100)
], function (assert, data) {
assert.true(true);
});

QUnit.test.each('array of mixed', [
undefined,
null,
false,
true,
0,
1,
-10,
10/3,
10e42,
Infinity,
NaN,
[],
{},
'some'
], function (assert, value) {
assert.true(true);
});

QUnit.test.each('keyed objects', { caseFoo: [1, 2, 3], caseBar: [1, 1, 2] }, function (assert, data) {
assert.true(true);
});
36 changes: 36 additions & 0 deletions test/cli/fixtures/each-array-labels.tap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# command: ["qunit", "each-array-labels.js"]

TAP version 13
ok 1 array of arrays [0]
ok 2 array of arrays [1]
ok 3 array of strings [0: foo]
ok 4 array of strings [1: bar]
ok 5 array of strings [2: ]
ok 6 array of strings [3: ]
ok 7 array of strings [4: _]
ok 8 array of strings [5: +]
ok 9 array of strings [6: ^]
ok 10 array of strings [7: $]
ok 11 array of strings [8: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]
ok 12 array of strings [9: yyyyyyyyyyyyyyyyyyyyyyyyyyyyy…]
ok 13 array of mixed [0: undefined]
ok 14 array of mixed [1: null]
ok 15 array of mixed [2: false]
ok 16 array of mixed [3: true]
ok 17 array of mixed [4: 0]
ok 18 array of mixed [5: 1]
ok 19 array of mixed [6: -10]
ok 20 array of mixed [7: 3.3333333333333335]
ok 21 array of mixed [8: 1e+43]
ok 22 array of mixed [9: Infinity]
ok 23 array of mixed [10: NaN]
ok 24 array of mixed [11]
ok 25 array of mixed [12]
ok 26 array of mixed [13: some]
ok 27 keyed objects [caseFoo]
ok 28 keyed objects [caseBar]
1..28
# pass 28
# skip 0
# todo 0
# fail 0
8 changes: 4 additions & 4 deletions test/cli/fixtures/test-if.tap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ 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 4 # SKIP skip dataset [0: a]
ok 5 # SKIP skip dataset [1: b]
ok 6 keep dataset [0: a]
ok 7 keep dataset [1: b]
ok 8 # SKIP skip group > skipper
ok 9 keep group > keeper
1..9
Expand Down

0 comments on commit aab5d75

Please sign in to comment.