Skip to content

Commit 7abb02b

Browse files
committed
feat: introduce beforeRetry hook
1 parent 77d2779 commit 7abb02b

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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, { maxRuns: 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
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@
6262
"release": true,
6363
"tokenRef": "GITHUB_AUTH"
6464
}
65-
}
65+
},
66+
"packageManager": "[email protected]+sha512.22721b3a11f81661ae1ec68ce1a7b879425a1ca5b991c975b074ac220b187ce56c708fe5db69f4c962c989452eee76c82877f4ee80f474cebd61ee13461b6228"
6667
}

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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,25 @@ QUnit.module('default max runs', function () {
167167
})
168168
})
169169

170+
QUnit.module('beforeRetry hook', function () {
171+
const calls = []
172+
const retryWithHook = setup(QUnit.test, {
173+
beforeRetry: function (assert, currentRun) {
174+
calls.push(['hook', currentRun])
175+
}
176+
})
177+
178+
retryWithHook('count retries', function (assert, currentRun) {
179+
calls.push(['test', currentRun])
180+
181+
assert.equal(currentRun, 2)
182+
}, 2)
183+
184+
QUnit.test('verify calls', function (assert) {
185+
assert.deepEqual(calls, [['test', 1], ['hook', 2], ['test', 2]])
186+
})
187+
})
188+
170189
QUnit.module('assert.expect', function () {
171190
const calls = []
172191

0 commit comments

Comments
 (0)