Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support test.each, test.todo etc. #16

Merged
merged 8 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
import Retry from './src/retry.js'

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

retry.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
return new Retry([name, dataset], callback, maxRuns, testFn.each)
}
retry.todo.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
return new Retry([name, dataset], callback, maxRuns, testFn.todo.each)
}
retry.skip.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
return new Retry([name, dataset], callback, maxRuns, testFn.skip.each)
}
retry.if.each = function (name, condition, dataset, callback, maxRuns = defaultMaxRuns) {
return new Retry([name, condition, dataset], callback, maxRuns, testFn.if.each)
}
retry.only.each = function (name, dataset, callback, maxRuns = defaultMaxRuns) {
return new Retry([name, dataset], callback, maxRuns, testFn.only.each)
}
return retry
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"scripts": {
"test": "pnpm test:smoke lint && pnpm test:js && pnpm test:smoke",
"test:smoke": "testem ci",
"test:js": "qunit",
"test:js": "qunit test/retry.js && qunit test/retry-only.js",
"dev": "qunit --watch",
"lint": "eslint index.js"
},
Expand Down
10 changes: 5 additions & 5 deletions src/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import AssertResultHandler from './assert-result-handler.js'
import extend from './extend.js'

export default class Retry {
constructor (name, callback, maxRuns = 2, testFn) {
this.name = name
constructor (args, callback, maxRuns = 2, testFn) {
this.callback = callback
this.maxRuns = maxRuns
this.currentRun = 1
this.assertResultHandler = new AssertResultHandler(this)

testFn(name, async (assert) => {
testFn(...args, async (assert, ...callbackArgs) => {
this.assertProxy = new Proxy(assert, this.assertResultHandler)
this.callbackArgs = callbackArgs
this.currentRun = 1
await this.retry(this.currentRun)
})
}
Expand Down Expand Up @@ -71,7 +71,7 @@ export default class Retry {

async tryTest () {
try {
await this.callback.call(this.testEnvironment, this.assertProxy, this.currentRun)
await this.callback.call(this.testEnvironment, this.assertProxy, ...this.callbackArgs, this.currentRun)
} catch (err) {
if (!this.shouldRetry) {
throw err
Expand Down
44 changes: 44 additions & 0 deletions test/retry-only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const setup = require('../index.js')
const QUnit = require('qunit')

const retry = setup(QUnit.test)

QUnit.module('retry.only', function () {
const calls = []

retry.only('count only retries', function (assert, currentRun) {
calls.push(['only', currentRun])

assert.equal(currentRun, 2)
})

retry('count non-only retries', function (assert, currentRun) {
calls.push(['non-only', currentRun])

assert.equal(currentRun, 2)
})

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

QUnit.module('retry.only.each', function () {
const calls = []

retry.only.each('count only retries', ['A', 'B'], function (assert, data, currentRun) {
calls.push(['only', data, currentRun])

assert.equal(currentRun, 2)
})

retry.each('count non-only retries', ['A', 'B'], function (assert, data, currentRun) {
calls.push(['non-only', data, currentRun])

assert.equal(currentRun, 2)
})

QUnit.test('verify calls', function (assert) {
assert.deepEqual(calls, [['only', 'A', 1], ['only', 'A', 2], ['only', 'B', 1], ['only', 'B', 2]])
})
})
125 changes: 125 additions & 0 deletions test/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,128 @@ QUnit.module('hooks count', function () {
assert.equal(afterCount, 5)
})
})

QUnit.module('default max runs', function () {
const calls = []
const retryThrice = setup(QUnit.test, 3)

retryThrice('count retries', function (assert, currentRun) {
calls.push(currentRun)

assert.equal(currentRun, 3)
})

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

QUnit.module('retry.todo', function () {
const calls = []

retry.todo('count retries', function (assert, currentRun) {
calls.push(currentRun)

assert.ok(false)
})

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

QUnit.module('retry.skip', function () {
const calls = []

retry.skip('count retries', function (assert, currentRun) {
calls.push(currentRun)

assert.equal(currentRun, 2)
})

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

QUnit.module('retry.if', function () {
const calls = []

retry.if('count true retries', true, function (assert, currentRun) {
calls.push([true, currentRun])

assert.equal(currentRun, 2)
})

retry.if('count false retries', false, function (assert, currentRun) {
calls.push([false, currentRun])

assert.equal(currentRun, 2)
})

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

QUnit.module('retry.each', function () {
const calls = []

retry.each('count retries', ['A', 'B', 'C'], function (assert, data, currentRun) {
calls.push([data, currentRun])

assert.equal(currentRun, 2)
})

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

QUnit.module('retry.todo.each', function () {
const calls = []

retry.todo.each('count retries', ['A', 'B', 'C'], function (assert, data, currentRun) {
calls.push([data, currentRun])

assert.ok(false)
})

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

QUnit.module('retry.skip.each', function () {
const calls = []

retry.skip.each('count retries', ['A', 'B', 'C'], function (assert, data, currentRun) {
calls.push([data, currentRun])

assert.equal(currentRun, 2)
})

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

QUnit.module('retry.if.each', function () {
const calls = []

retry.if.each('count true retries', true, ['A', 'B'], function (assert, data, currentRun) {
calls.push([true, data, currentRun])

assert.equal(currentRun, 2)
})

retry.if.each('count false retries', false, ['A', 'B'], function (assert, data, currentRun) {
calls.push([false, data, currentRun])

assert.equal(currentRun, 2)
})

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