You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/QUnit/test.each.md
+70-2Lines changed: 70 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -30,14 +30,82 @@ Add tests using a data provider.
30
30
|`assert` (object) | A new instance object with the [assertion methods](../assert/index.md)|
31
31
|`data` (any) | Data item |
32
32
33
-
Use this method to add multiple tests that are similar, but with different data passed in.
33
+
Use this method to define many similar tests, with different data passed in.
34
34
35
-
`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.
35
+
`QUnit.test.each()` generates multiple calls to [`QUnit.test()`](./test.md), and provides the same features, such as support for async functionsand test context.
36
36
37
37
Each test case is passed one item from your dataset.
38
38
39
39
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.
40
40
41
+
### Reduce code duplication
42
+
43
+
You can use `QUnit.test.each()` to write a single test, run once for each item in a dataset. This avoids code duplication and prevents unintentional drift over time between similar tests.
44
+
45
+
This tends to cut unintentional, unimportant, or undocumented differences. That in turn improves readability and comprehension to future contributors. When multiple tests do similar things, there can be _intentional_ and _unintentional_ differences. The intentional difference is motivated by what you want to cover (e.g. input A and input B). The _unintentional_ difference may be due to authors having their own coding style, or changing habits over time. As a contributor it can be confusing when your patch causes tests to fail. These unintentional differences make it difficult to judge if a test is "safe" to change. For example, if test A fails but B passes, and A would pass if it was written more like B, is A okay to change because it was only that way by accident? Or was this an intended alternative approach covered by that test?
46
+
47
+
By writing your tests with `QUnit.test.each()`, you write shared code only once. Any intentional differences are clearly visible through a declarative dataset. By writing it only once, it also encourages writing of code comments as you won’t have to duplicate and maintain these across multiple tests.
48
+
49
+
```js
50
+
// Without QUnit.test.each()
51
+
52
+
QUnit.test('example', function (assert) {
53
+
constmockPage1= {
54
+
title:'Example',
55
+
lastModified:'2011-04-01T12:00:00Z',
56
+
content:'Foo bar.'
57
+
};
58
+
constmockUser1= {
59
+
name:'Admin',
60
+
registered:'1991-10-18T12:00:00Z',
61
+
role:'administrator'
62
+
};
63
+
APP.appendToPage(mockPage1, mockUser1, 'Added text here.');
64
+
assert.equal(mockPage1.content, 'Foo bar.\n\nAdded text here.');
65
+
66
+
constmockUser2= {
67
+
name:'root',
68
+
registered:'1963-06-09T03:00:00Z',
69
+
role:'administrator'
70
+
};
71
+
constmockPage2= {
72
+
title:'Example',
73
+
lastModified:'2011-04-01T12:00:00Z',
74
+
content:''
75
+
};
76
+
APP.appendToPage(mockPage2, mockUser2, 'Added text here.');
77
+
assert.equal(mockPage2.content, 'Added text here.');
78
+
});
79
+
```
80
+
81
+
Compared to:
82
+
83
+
```js
84
+
QUnit.test.each('example', {
85
+
// Expect an empty line between existing content and appendage.
0 commit comments