Skip to content

Commit

Permalink
Docs: Flesh out test.each() description and examples more
Browse files Browse the repository at this point in the history
* Use "data provider" once or twice for better search and discovery.

* Refer to the input as `dataset` and each item as `data`.
  The `args` name, I think, was less obvious in meaning since it is
  leaning very specifically towards 'data as array' and the
  destructuring approach (or variadic argument, from an earlier
  iteration of #1569).

* Add verbose form of all only/skip/todo signatures to the intro.

* Move the helpful use cases and advice from param table into
  the description paragraphs.

* Split "basic", "array", and "promise" examples up into three
  separate headings.

* Add async function example.

Follows-up 07de4b1.
  • Loading branch information
Krinkle committed May 23, 2021
1 parent 6e60112 commit b3521d0
Showing 1 changed file with 59 additions and 24 deletions.
83 changes: 59 additions & 24 deletions docs/QUnit/test.each.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,108 @@
---
layout: default
title: QUnit.test.each()
excerpt: Add a parameterized test to run.
excerpt: Add tests using a data provider.
categories:
- main
version_added: "unreleased"
---

`QUnit.test.each( name, data, callback )`
`QUnit.test.each( name, dataset, callback )`<br>
`QUnit.test.only.each( name, dataset, callback )`<br>
`QUnit.test.skip.each( name, dataset, callback )`<br>
`QUnit.test.todo.each( name, dataset, callback )`

Add a parameterized test to run.
Add tests using a data provider.

| parameter | description |
|-----------|-------------|
| `name` (string) | Title of unit being tested |
| `data` (Array) | Array of arrays of parameters to be passed as input to each test. This can also be specified as a 1D array of primitives |
| `dataset` (array) | List of data values passed to each test case |
| `callback` (function) | Function to close over assertions |

#### Callback parameters: `callback( assert, args )`:
#### Callback parameters: `callback( assert, data )`:

| parameter | description |
|-----------|-------------|
| `assert` (object) | A new instance object with the [assertion methods](../assert/index.md) |
| `args` (any) | All input parameters. The original array is passed. Array destructuring can be used to unpack input values |
| `data` (any) | Data value |

### Description

Add a parameterized test to run using `QUnit.test.each()`. `QUnit.test.each()` generates multiple calls to `QUnit.test()` so `then`-able behavior is maintained.
Use this method to add multiple tests that are similar, but with different data passed in.

`QUnit.test.each()` generates multiple calls to [`QUnit.test()`](./test.md) internally, and has all the same capabilities such support for async functions, returning a Promise, and the `assert` argument.

The `assert` argument to the callback contains all of QUnit's [assertion methods](../assert/index.md). Use this argument to call your test assertions.
`QUnit.test.only.each`, `QUnit.test.skip.each` and `QUnit.test.todo.each` are also available.

See also:
* [`QUnit.test.only()`](./test.only.md)
* [`QUnit.test.skip()`](./test.skip.md)
* [`QUnit.test.todo()`](./test.todo.md)
Each test case is passed one value of your dataset.

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

### Examples

A practical example, using the assert argument and no globals.
##### Example: Basic data provider

```js
function isEven( x ) {
return x % 2 === 0;
}

QUnit.test.each( "isEven()", [ 2, 4, 6 ], ( assert, data ) => {
assert.true( isEven( data ), `${data} is even` );
});
```

##### Example: Array data provider

The original array is passed to your callback. [Array destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) can be used to unpack the data array, directly from the callback signature.


```js
function square( x ) {
return x * x;
}

QUnit.test.each( "square()", [
[ 2, 4 ],
[ 3, 9 ]
], ( assert, [ value, expected ] ) => {
assert.equal( square( value ), expected, `${value} squared` );
});
```

##### Example: Async functions with `each()`

```js
function isEven( x ) {
return x % 2 === 0;
}

function isAsyncEven( x ) {
async function isAsyncEven( x ) {
return new Promise( resolve => {
return resolve( isEven( x ) );
resolve( isEven( x ) );
} );
}

QUnit.test.each( "square()", [ [ 2, 4 ], [ 3, 9 ] ], ( assert, [ value, expected ] ) => {
assert.equal( square( value ), expected, `square(${value})` );
QUnit.test.each( "isAsyncEven()", [ 2, 4 ], async ( assert, data ) => {
assert.true( await isAsyncEven( data ), `${data} is even` );
});
```

QUnit.test.each( "isEven()", [ 2, 4 ], ( assert, value ) => {
assert.true( isEven( value ), `isEven(${value})` );
});
Return a Promise from each callback, using classic ES5 syntax:

```js
function isEven( x ) {
return x % 2 === 0;
}

function isAsyncEven( x ) {
return new Promise( function ( resolve ) {
resolve( isEven( x ) );
} );
}

QUnit.test.each( "isAsyncEven()", [ 2, 4 ], ( assert, value ) => {
return isAsyncEven( value ).then( ( value ) => {
assert.true( isAsyncEven( value ), `isAsyncEven(${value})` );
return isAsyncEven( value ).then( ( result ) => {
assert.true( result, `${value} is even` );
} );
});
```

0 comments on commit b3521d0

Please sign in to comment.