-
Notifications
You must be signed in to change notification settings - Fork 781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core: Add test.each. #1569
Merged
Merged
Core: Add test.each. #1569
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
3333757
Core: Add test.each.
ventuno 68ff879
Merge branch 'main' into ftr-1568
ventuno 0360909
Core: update test.each API.
ventuno f3aecd8
Merge branch 'main' into ftr-1568
ventuno 153786a
Core: add support for 1D arrays in test.each.
ventuno 5d725ab
Core: add support for each.todo and additional assertions.
ventuno 45c7175
Test: update page title and docs.
ventuno 603c88b
Test: add test with then-able result.
ventuno 03aa8d1
Docs: update docs with then-able example.
ventuno 998c349
Docs: add sample implementation.
ventuno 9d3721b
Core: correct error message.
ventuno 7460dd3
Core: simplify skip.
ventuno a335a59
Core: refactor test and each into addTestWithData.
ventuno 07ee1a8
Merge branch 'main' into ftr-1568
ventuno 5502356
Merge branch 'main' into ftr-1568
ventuno 820922f
Merge branch 'main' into ftr-1568
ventuno 551de8f
Merge branch 'main' into ftr-1568
ventuno 44c26f7
Merge branch 'main' into ftr-1568
ventuno cc91756
Core: remove special handling for 1d arrays in test.each.
ventuno 34c3f0e
Core: update test function names.
ventuno 080dbb7
Merge branch 'main' into ftr-1568
ventuno e525dd1
Core: update key formatting to use [].
ventuno 74ab9f0
Core: update doc version_added and categories.
ventuno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
layout: default | ||
title: QUnit.test.each() | ||
excerpt: Add a parameterized test to run. | ||
categories: | ||
- main | ||
version_added: "unreleased" | ||
--- | ||
|
||
`QUnit.test.each( name, data, callback )` | ||
|
||
Add a parameterized test to run. | ||
|
||
| 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 | | ||
| `callback` (function) | Function to close over assertions | | ||
|
||
#### Callback parameters: `callback( assert, args )`: | ||
|
||
| 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 | | ||
|
||
### 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. | ||
|
||
|
||
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) | ||
|
||
|
||
### Examples | ||
|
||
A practical example, using the assert argument and no globals. | ||
|
||
```js | ||
function square( x ) { | ||
return x * x; | ||
} | ||
|
||
function isEven( x ) { | ||
return x % 2 === 0; | ||
} | ||
|
||
function isAsyncEven( x ) { | ||
return new Promise( resolve => { | ||
return resolve( isEven( x ) ); | ||
} ); | ||
} | ||
|
||
QUnit.test.each( "square()", [ [ 2, 4 ], [ 3, 9 ] ], ( assert, [ value, expected ] ) => { | ||
assert.equal( square( value ), expected, `square(${value})` ); | ||
}); | ||
|
||
QUnit.test.each( "isEven()", [ 2, 4 ], ( assert, value ) => { | ||
assert.true( isEven( value ), `isEven(${value})` ); | ||
}); | ||
|
||
QUnit.test.each( "isAsyncEven()", [ 2, 4 ], ( assert, value ) => { | ||
return isAsyncEven( value ).then( ( value ) => { | ||
assert.true( isAsyncEven( value ), `isAsyncEven(${value})` ); | ||
} ); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>QUnit.test.each Suite</title> | ||
<link rel="stylesheet" href="../qunit/qunit.css"> | ||
<script src="../qunit/qunit.js"></script> | ||
<script src="main/each.js"></script> | ||
</head> | ||
<body> | ||
<div id="qunit"></div> | ||
<div id="qunit-fixture">test markup</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
QUnit.module( "test.each", function() { | ||
QUnit.test.each( "test.each", [ [ 1, 2, 3 ], [ 1, 1, 2 ] ], function( assert, data ) { | ||
assert.strictEqual( data[ 0 ] + data[ 1 ], data[ 2 ] ); | ||
} ); | ||
QUnit.test.each( "test.each returning a Promise", [ [ 1, 2, 3 ], [ 1, 1, 2 ] ], function( assert, data ) { | ||
function sum( a, b ) { | ||
return new Promise( function( resolve ) { | ||
resolve( a + b ); | ||
} ); | ||
} | ||
return sum( data[ 0 ], data[ 1 ] ).then( function( result ) { assert.strictEqual( result, data[ 2 ] ); } ); | ||
} ); | ||
QUnit.test.each( "test.each 1D", [ 1, [], "some" ], function( assert, value ) { | ||
assert.true( Boolean( value ) ); | ||
} ); | ||
QUnit.test.each( "test.each fails with non-array input", [ "something", 1, undefined, null, {} ], function( assert, value ) { | ||
assert.throws( function() { | ||
QUnit.test.each( "test.each 1D", value, function() { } ); | ||
} ); | ||
} ); | ||
} ); | ||
QUnit.module( "test.skip.each", function() { | ||
QUnit.test( "do run", function( assert ) { assert.true( true ); } ); | ||
QUnit.test.skip.each( "test.skip.each", [ [ 1, 2, 3 ], [ 1, 1, 2 ] ], function( assert ) { | ||
assert.true( false ); | ||
} ); | ||
} ); | ||
QUnit.module( "test.todo.each", function() { | ||
QUnit.test.todo.each( "test.todo.each", [ [ 1, 2, 3 ], [ 1, 1, 2 ] ], function( assert ) { | ||
assert.true( false ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
QUnit.module.only( "test.each.only", function() { | ||
QUnit.test( "don't run", function( assert ) { assert.true( false ); } ); | ||
QUnit.test.only.each( "test.each.only", [ [ 1, 2, 3 ], [ 1, 1, 2 ] ], function( assert, data ) { | ||
assert.strictEqual( data[ 0 ] + data[ 1 ], data[ 2 ] ); | ||
} ); | ||
QUnit.test.only.each( "test.each.only 1D", [ 1, [], "some" ], function( assert, value ) { | ||
assert.true( Boolean( value ) ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>QUnit.test.each.only Suite</title> | ||
<link rel="stylesheet" href="../qunit/qunit.css"> | ||
<script src="../qunit/qunit.js"></script> | ||
<script src="main/only-each.js"></script> | ||
</head> | ||
<body> | ||
<div id="qunit"></div> | ||
<div id="qunit-fixture">test markup</div> | ||
</body> | ||
</html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this abstraction a lot, did you mean to make more use of it? I went ahead and did so as part of #1620.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually came from @smcclure15's recommendation. Glad it's useful!