Skip to content

Commit 1273fcb

Browse files
authored
Merge pull request #20 from mrloop/before-retry-hook
feat: accept beforeRetry hook
2 parents 1e8dd58 + 332961b commit 1273fcb

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ Blog post about `qunit-retry` available [here](https://blog.mrloop.com/javascrip
3131

3232
### Set Max Runs
3333

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:
34+
The default maximum number of retries is 2 (one attempt and one retry). To change it globally, pass the `maxRuns` option to the `setup` function:
3535

3636
```js
3737
const setup = require('qunit-retry');
3838

39-
const retry = setup(QUnit.test, 3); // sets maxRuns to 3
39+
const retry = setup(QUnit.test, { maxRuns: 3 });
4040
```
4141

4242
To change it for a single test, pass the number of retries as the third argument:
@@ -54,6 +54,16 @@ retry("a test relying on 3rd party service that occasionally fails", async funct
5454

5555
**Note:** It is generally advised to use the retry sparingly and this advice extends to setting a large number of retries.
5656

57+
### Resetting environment between retries
58+
59+
If you need to reset the environment between retries, you can pass a `beforeRetry` function to the `setup` function:
60+
61+
```js
62+
const setup = require('qunit-retry');
63+
64+
const retry = setup(QUnit.test, { beforeRetry: () => resetEnvironment() });
65+
```
66+
5767
Install
5868
------------------------------------------------------------------------------
5969

main.js

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

3-
export default function setup (testFn, defaultMaxRuns = 2) {
3+
export default function setup (testFn, { maxRuns: defaultMaxRuns = 2, beforeRetry } = {}) {
44
const retry = function (name, callback, maxRuns = defaultMaxRuns) {
5-
return new Retry([name], callback, maxRuns, testFn)
5+
return new Retry([name], callback, maxRuns, testFn, beforeRetry)
66
}
77
retry.todo = function (name, callback, maxRuns = defaultMaxRuns) {
8-
return new Retry([name], callback, maxRuns, testFn.todo)
8+
return new Retry([name], callback, maxRuns, testFn.todo, beforeRetry)
99
}
1010
retry.skip = function (name, callback, maxRuns = defaultMaxRuns) {
11-
return new Retry([name], callback, maxRuns, testFn.skip)
11+
return new Retry([name], callback, maxRuns, testFn.skip, beforeRetry)
1212
}
1313
retry.if = function (name, condition, callback, maxRuns = defaultMaxRuns) {
14-
return new Retry([name, condition], callback, maxRuns, testFn.if)
14+
return new Retry([name, condition], callback, maxRuns, testFn.if, beforeRetry)
1515
}
1616
retry.only = function (name, callback, maxRuns = defaultMaxRuns) {
17-
return new Retry([name], callback, maxRuns, testFn.only)
17+
return new Retry([name], callback, maxRuns, testFn.only, beforeRetry)
1818
}
1919

2020
retry.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
21-
return new Retry([name, dataset], callback, maxRuns, testFn.each)
21+
return new Retry([name, dataset], callback, maxRuns, testFn.each, beforeRetry)
2222
}
2323
retry.todo.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
24-
return new Retry([name, dataset], callback, maxRuns, testFn.todo.each)
24+
return new Retry([name, dataset], callback, maxRuns, testFn.todo.each, beforeRetry)
2525
}
2626
retry.skip.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
27-
return new Retry([name, dataset], callback, maxRuns, testFn.skip.each)
27+
return new Retry([name, dataset], callback, maxRuns, testFn.skip.each, beforeRetry)
2828
}
2929
retry.if.each = function (name, condition, dataset, callback, maxRuns = defaultMaxRuns) {
30-
return new Retry([name, condition, dataset], callback, maxRuns, testFn.if.each)
30+
return new Retry([name, condition, dataset], callback, maxRuns, testFn.if.each, beforeRetry)
3131
}
3232
retry.only.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
33-
return new Retry([name, dataset], callback, maxRuns, testFn.only.each)
33+
return new Retry([name, dataset], callback, maxRuns, testFn.only.each, beforeRetry)
3434
}
3535
return retry
3636
}

src/retry.js

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

44
export default class Retry {
5-
constructor (args, callback, maxRuns = 2, testFn) {
5+
constructor (args, callback, maxRuns = 2, testFn, beforeRetry) {
66
this.callback = callback
77
this.maxRuns = maxRuns
8+
this.beforeRetry = beforeRetry
89
this.assertResultHandler = new AssertResultHandler(this)
910
this.isSuccess = true
1011

@@ -60,6 +61,9 @@ export default class Retry {
6061

6162
async runTest () {
6263
if (this.notFirstRun) {
64+
if (this.beforeRetry) {
65+
await this.beforeRetry.call(this.testEnvironment, this.assertProxy, this.currentRun)
66+
}
6367
this.isSuccess = true
6468
this.test.testEnvironment = extend({}, this.test.module.testEnvironment, false, true)
6569
await this.runHooks(this.beforeEachHooks)

test/retry.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ QUnit.module('hooks count', function () {
179179

180180
QUnit.module('default max runs', function () {
181181
const calls = []
182-
const retryThrice = setup(QUnit.test, 3)
182+
const retryThrice = setup(QUnit.test, { maxRuns: 3 })
183183

184184
retryThrice('count retries', function (assert, currentRun) {
185185
calls.push(currentRun)
@@ -192,6 +192,25 @@ QUnit.module('default max runs', function () {
192192
})
193193
})
194194

195+
QUnit.module('beforeRetry hook', function () {
196+
const calls = []
197+
const retryWithHook = setup(QUnit.test, {
198+
beforeRetry: function (assert, currentRun) {
199+
calls.push(['hook', currentRun])
200+
}
201+
})
202+
203+
retryWithHook('count retries', function (assert, currentRun) {
204+
calls.push(['test', currentRun])
205+
206+
assert.equal(currentRun, 2)
207+
}, 2)
208+
209+
QUnit.test('verify calls', function (assert) {
210+
assert.deepEqual(calls, [['test', 1], ['hook', 2], ['test', 2]])
211+
})
212+
})
213+
195214
QUnit.module('assert.expect', function () {
196215
const calls = []
197216

0 commit comments

Comments
 (0)