Skip to content

Commit

Permalink
feat: introduce beforeRetry hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jembezmamy committed Nov 11, 2024
1 parent 77d2779 commit 7abb02b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ retry("a test relying on 3rd party service that occasionally fails", async funct

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

### Resetting environment between retries

If you need to reset the environment between retries, you can pass a `beforeRetry` function to the `setup` function:

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

const retry = setup(QUnit.test, { beforeRetry: () => resetEnvironment() });
```

Install
------------------------------------------------------------------------------

Expand Down
22 changes: 11 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import Retry from './src/retry.js'

export default function setup (testFn, { maxRuns: defaultMaxRuns = 2 } = {}) {
export default function setup (testFn, { maxRuns: defaultMaxRuns = 2, beforeRetry } = {}) {
const retry = function (name, callback, maxRuns = defaultMaxRuns) {
return new Retry([name], callback, maxRuns, testFn)
return new Retry([name], callback, maxRuns, testFn, beforeRetry)
}
retry.todo = function (name, callback, maxRuns = defaultMaxRuns) {
return new Retry([name], callback, maxRuns, testFn.todo)
return new Retry([name], callback, maxRuns, testFn.todo, beforeRetry)
}
retry.skip = function (name, callback, maxRuns = defaultMaxRuns) {
return new Retry([name], callback, maxRuns, testFn.skip)
return new Retry([name], callback, maxRuns, testFn.skip, beforeRetry)
}
retry.if = function (name, condition, callback, maxRuns = defaultMaxRuns) {
return new Retry([name, condition], callback, maxRuns, testFn.if)
return new Retry([name, condition], callback, maxRuns, testFn.if, beforeRetry)
}
retry.only = function (name, callback, maxRuns = defaultMaxRuns) {
return new Retry([name], callback, maxRuns, testFn.only)
return new Retry([name], callback, maxRuns, testFn.only, beforeRetry)
}

retry.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
return new Retry([name, dataset], callback, maxRuns, testFn.each)
return new Retry([name, dataset], callback, maxRuns, testFn.each, beforeRetry)
}
retry.todo.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
return new Retry([name, dataset], callback, maxRuns, testFn.todo.each)
return new Retry([name, dataset], callback, maxRuns, testFn.todo.each, beforeRetry)
}
retry.skip.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
return new Retry([name, dataset], callback, maxRuns, testFn.skip.each)
return new Retry([name, dataset], callback, maxRuns, testFn.skip.each, beforeRetry)
}
retry.if.each = function (name, condition, dataset, callback, maxRuns = defaultMaxRuns) {
return new Retry([name, condition, dataset], callback, maxRuns, testFn.if.each)
return new Retry([name, condition, dataset], callback, maxRuns, testFn.if.each, beforeRetry)
}
retry.only.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
return new Retry([name, dataset], callback, maxRuns, testFn.only.each)
return new Retry([name, dataset], callback, maxRuns, testFn.only.each, beforeRetry)
}
return retry
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@
"release": true,
"tokenRef": "GITHUB_AUTH"
}
}
},
"packageManager": "[email protected]+sha512.22721b3a11f81661ae1ec68ce1a7b879425a1ca5b991c975b074ac220b187ce56c708fe5db69f4c962c989452eee76c82877f4ee80f474cebd61ee13461b6228"
}
6 changes: 5 additions & 1 deletion src/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import AssertResultHandler from './assert-result-handler.js'
import extend from './extend.js'

export default class Retry {
constructor (args, callback, maxRuns = 2, testFn) {
constructor (args, callback, maxRuns = 2, testFn, beforeRetry) {
this.callback = callback
this.maxRuns = maxRuns
this.beforeRetry = beforeRetry
this.assertResultHandler = new AssertResultHandler(this)
this.isSuccess = true

Expand Down Expand Up @@ -60,6 +61,9 @@ export default class Retry {

async runTest () {
if (this.notFirstRun) {
if (this.beforeRetry) {
await this.beforeRetry.call(this.testEnvironment, this.assertProxy, this.currentRun)
}
this.isSuccess = true
this.test.testEnvironment = extend({}, this.test.module.testEnvironment, false, true)
await this.runHooks(this.beforeEachHooks)
Expand Down
19 changes: 19 additions & 0 deletions test/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ QUnit.module('default max runs', function () {
})
})

QUnit.module('beforeRetry hook', function () {
const calls = []
const retryWithHook = setup(QUnit.test, {
beforeRetry: function (assert, currentRun) {
calls.push(['hook', currentRun])
}
})

retryWithHook('count retries', function (assert, currentRun) {
calls.push(['test', currentRun])

assert.equal(currentRun, 2)
}, 2)

QUnit.test('verify calls', function (assert) {
assert.deepEqual(calls, [['test', 1], ['hook', 2], ['test', 2]])
})
})

QUnit.module('assert.expect', function () {
const calls = []

Expand Down

0 comments on commit 7abb02b

Please sign in to comment.