Skip to content

Commit 92034c4

Browse files
authored
Merge pull request #16 from mrloop/support-test-each
feat: support test.each, test.todo etc.
2 parents 54360e4 + 1bafbc2 commit 92034c4

File tree

6 files changed

+222
-10
lines changed

6 files changed

+222
-10
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ qunit-retry
1010
Drop in replacement for [QUnit](https://qunitjs.com/) [test](https://api.qunitjs.com/QUnit/test) to `retry` test upon failure.
1111

1212
```js
13+
const setup = require('qunit-retry');
14+
15+
const retry = setup(QUnit.test);
16+
1317
// retry this test on failure as third party service occasionally fails
1418
// we need to test against third party service
1519
// we can live with occasional third party service failure
@@ -19,13 +23,23 @@ retry("a test relying on 3rd party service that occasionally fails", async funct
1923
});
2024
```
2125

26+
It provides the same API as `QUnit.test`, including `test.each`, `test.only` etc. The only difference is that the test will be retried upon failure.
27+
2228
Use very sparingly, for a suite of 2024 tests, using this for a single acceptance test.
2329

2430
Blog post about `qunit-retry` available [here](https://blog.mrloop.com/javascript/2019/02/26/qunit-retry.html).
2531

2632
### Set Max Runs
2733

28-
To change the number of retries, set a value in the third parameter (`maxRuns`). The default value for `maxRuns` is `2` (one attempt and one retry):
34+
The default maximum number of retries is 2 (one attempt and one retry). To change it globally, pass an additional argument to the `setup` function:
35+
36+
```js
37+
const setup = require('qunit-retry');
38+
39+
const retry = setup(QUnit.test, 3); // sets maxRuns to 3
40+
```
41+
42+
To change it for a single test, pass the number of retries as the third argument:
2943

3044
```js
3145
// retry this test **two times** (in addition to one initial attempt)

main.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
11
import Retry from './src/retry.js'
22

3-
export default function setup (testFn) {
4-
return function retry (name, callback, maxRuns = 2) {
5-
return new Retry(name, callback, maxRuns, testFn)
3+
export default function setup (testFn, defaultMaxRuns = 2) {
4+
const retry = function (name, callback, maxRuns = defaultMaxRuns) {
5+
return new Retry([name], callback, maxRuns, testFn)
66
}
7+
retry.todo = function (name, callback, maxRuns = defaultMaxRuns) {
8+
return new Retry([name], callback, maxRuns, testFn.todo)
9+
}
10+
retry.skip = function (name, callback, maxRuns = defaultMaxRuns) {
11+
return new Retry([name], callback, maxRuns, testFn.skip)
12+
}
13+
retry.if = function (name, condition, callback, maxRuns = defaultMaxRuns) {
14+
return new Retry([name, condition], callback, maxRuns, testFn.if)
15+
}
16+
retry.only = function (name, callback, maxRuns = defaultMaxRuns) {
17+
return new Retry([name], callback, maxRuns, testFn.only)
18+
}
19+
20+
retry.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
21+
return new Retry([name, dataset], callback, maxRuns, testFn.each)
22+
}
23+
retry.todo.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
24+
return new Retry([name, dataset], callback, maxRuns, testFn.todo.each)
25+
}
26+
retry.skip.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
27+
return new Retry([name, dataset], callback, maxRuns, testFn.skip.each)
28+
}
29+
retry.if.each = function (name, condition, dataset, callback, maxRuns = defaultMaxRuns) {
30+
return new Retry([name, condition, dataset], callback, maxRuns, testFn.if.each)
31+
}
32+
retry.only.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
33+
return new Retry([name, dataset], callback, maxRuns, testFn.only.each)
34+
}
35+
return retry
736
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"scripts": {
2727
"test": "pnpm test:smoke lint && pnpm test:js && pnpm test:smoke",
2828
"test:smoke": "testem ci",
29-
"test:js": "qunit",
29+
"test:js": "qunit test/retry.js && qunit test/retry-only.js",
3030
"dev": "qunit --watch",
3131
"lint": "eslint index.js"
3232
},

src/retry.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import AssertResultHandler from './assert-result-handler.js'
22
import extend from './extend.js'
33

44
export default class Retry {
5-
constructor (name, callback, maxRuns = 2, testFn) {
6-
this.name = name
5+
constructor (args, callback, maxRuns = 2, testFn) {
76
this.callback = callback
87
this.maxRuns = maxRuns
9-
this.currentRun = 1
108
this.assertResultHandler = new AssertResultHandler(this)
119

12-
testFn(name, async (assert) => {
10+
testFn(...args, async (assert, ...callbackArgs) => {
1311
this.assertProxy = new Proxy(assert, this.assertResultHandler)
12+
this.callbackArgs = callbackArgs
13+
this.currentRun = 1
1414
await this.retry(this.currentRun)
1515
})
1616
}
@@ -71,7 +71,7 @@ export default class Retry {
7171

7272
async tryTest () {
7373
try {
74-
await this.callback.call(this.testEnvironment, this.assertProxy, this.currentRun)
74+
await this.callback.call(this.testEnvironment, this.assertProxy, ...this.callbackArgs, this.currentRun)
7575
} catch (err) {
7676
if (!this.shouldRetry) {
7777
throw err

test/retry-only.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const setup = require('../index.js')
2+
const QUnit = require('qunit')
3+
4+
const retry = setup(QUnit.test)
5+
6+
QUnit.module('retry.only', function () {
7+
const calls = []
8+
9+
retry.only('count only retries', function (assert, currentRun) {
10+
calls.push(['only', currentRun])
11+
12+
assert.equal(currentRun, 2)
13+
})
14+
15+
retry('count non-only retries', function (assert, currentRun) {
16+
calls.push(['non-only', currentRun])
17+
18+
assert.equal(currentRun, 2)
19+
})
20+
21+
QUnit.test('verify calls', function (assert) {
22+
assert.deepEqual(calls, [['only', 1], ['only', 2]])
23+
})
24+
})
25+
26+
QUnit.module('retry.only.each', function () {
27+
const calls = []
28+
29+
retry.only.each('count only retries', ['A', 'B'], function (assert, data, currentRun) {
30+
calls.push(['only', data, currentRun])
31+
32+
assert.equal(currentRun, 2)
33+
})
34+
35+
retry.each('count non-only retries', ['A', 'B'], function (assert, data, currentRun) {
36+
calls.push(['non-only', data, currentRun])
37+
38+
assert.equal(currentRun, 2)
39+
})
40+
41+
QUnit.test('verify calls', function (assert) {
42+
assert.deepEqual(calls, [['only', 'A', 1], ['only', 'A', 2], ['only', 'B', 1], ['only', 'B', 2]])
43+
})
44+
})

test/retry.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,128 @@ QUnit.module('hooks count', function () {
118118
assert.equal(afterCount, 5)
119119
})
120120
})
121+
122+
QUnit.module('default max runs', function () {
123+
const calls = []
124+
const retryThrice = setup(QUnit.test, 3)
125+
126+
retryThrice('count retries', function (assert, currentRun) {
127+
calls.push(currentRun)
128+
129+
assert.equal(currentRun, 3)
130+
})
131+
132+
QUnit.test('verify calls', function (assert) {
133+
assert.deepEqual(calls, [1, 2, 3])
134+
})
135+
})
136+
137+
QUnit.module('retry.todo', function () {
138+
const calls = []
139+
140+
retry.todo('count retries', function (assert, currentRun) {
141+
calls.push(currentRun)
142+
143+
assert.ok(false)
144+
})
145+
146+
QUnit.test('verify calls', function (assert) {
147+
assert.deepEqual(calls, [1, 2])
148+
})
149+
})
150+
151+
QUnit.module('retry.skip', function () {
152+
const calls = []
153+
154+
retry.skip('count retries', function (assert, currentRun) {
155+
calls.push(currentRun)
156+
157+
assert.equal(currentRun, 2)
158+
})
159+
160+
QUnit.test('verify calls', function (assert) {
161+
assert.deepEqual(calls, [])
162+
})
163+
})
164+
165+
QUnit.module('retry.if', function () {
166+
const calls = []
167+
168+
retry.if('count true retries', true, function (assert, currentRun) {
169+
calls.push([true, currentRun])
170+
171+
assert.equal(currentRun, 2)
172+
})
173+
174+
retry.if('count false retries', false, function (assert, currentRun) {
175+
calls.push([false, currentRun])
176+
177+
assert.equal(currentRun, 2)
178+
})
179+
180+
QUnit.test('verify calls', function (assert) {
181+
assert.deepEqual(calls, [[true, 1], [true, 2]])
182+
})
183+
})
184+
185+
QUnit.module('retry.each', function () {
186+
const calls = []
187+
188+
retry.each('count retries', ['A', 'B', 'C'], function (assert, data, currentRun) {
189+
calls.push([data, currentRun])
190+
191+
assert.equal(currentRun, 2)
192+
})
193+
194+
QUnit.test('verify calls', function (assert) {
195+
assert.deepEqual(calls, [['A', 1], ['A', 2], ['B', 1], ['B', 2], ['C', 1], ['C', 2]])
196+
})
197+
})
198+
199+
QUnit.module('retry.todo.each', function () {
200+
const calls = []
201+
202+
retry.todo.each('count retries', ['A', 'B', 'C'], function (assert, data, currentRun) {
203+
calls.push([data, currentRun])
204+
205+
assert.ok(false)
206+
})
207+
208+
QUnit.test('verify calls', function (assert) {
209+
assert.deepEqual(calls, [['A', 1], ['A', 2], ['B', 1], ['B', 2], ['C', 1], ['C', 2]])
210+
})
211+
})
212+
213+
QUnit.module('retry.skip.each', function () {
214+
const calls = []
215+
216+
retry.skip.each('count retries', ['A', 'B', 'C'], function (assert, data, currentRun) {
217+
calls.push([data, currentRun])
218+
219+
assert.equal(currentRun, 2)
220+
})
221+
222+
QUnit.test('verify calls', function (assert) {
223+
assert.deepEqual(calls, [])
224+
})
225+
})
226+
227+
QUnit.module('retry.if.each', function () {
228+
const calls = []
229+
230+
retry.if.each('count true retries', true, ['A', 'B'], function (assert, data, currentRun) {
231+
calls.push([true, data, currentRun])
232+
233+
assert.equal(currentRun, 2)
234+
})
235+
236+
retry.if.each('count false retries', false, ['A', 'B'], function (assert, data, currentRun) {
237+
calls.push([false, data, currentRun])
238+
239+
assert.equal(currentRun, 2)
240+
})
241+
242+
QUnit.test('verify calls', function (assert) {
243+
assert.deepEqual(calls, [[true, 'A', 1], [true, 'A', 2], [true, 'B', 1], [true, 'B', 2]])
244+
})
245+
})

0 commit comments

Comments
 (0)