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

fix: avoid false positives when the failed assertion is not the last one #17

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 1 addition & 5 deletions src/assert-result-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ export default class AssertResultHandler {
return `${message}(Retried ${retryNum} times)`
}

get isSuccess () {
return this.lastResult && this.lastResult.result
}

pushResultFn (target) {
return (result) => {
this.lastResult = result
this.retry.isSuccess = this.retry.isSuccess && result.result
if (!this.retry.shouldRetry) {
result.message = this.retryMessage(result.message, this.retry.currentRun)
target.pushResult(result)
Expand Down
11 changes: 7 additions & 4 deletions src/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class Retry {
this.callback = callback
this.maxRuns = maxRuns
this.assertResultHandler = new AssertResultHandler(this)
this.isSuccess = true

testFn(...args, async (assert, ...callbackArgs) => {
this.assertProxy = new Proxy(assert, this.assertResultHandler)
Expand All @@ -16,7 +17,7 @@ export default class Retry {
}

get shouldRetry () {
return this.currentRun < this.maxRuns && !this.assertResultHandler.isSuccess
return this.currentRun < this.maxRuns && !this.isSuccess
}

get test () {
Expand Down Expand Up @@ -59,6 +60,7 @@ export default class Retry {

async runTest () {
if (this.notFirstRun) {
this.isSuccess = true
this.test.testEnvironment = extend({}, this.test.module.testEnvironment, false, true)
await this.runHooks(this.beforeEachHooks)
}
Expand All @@ -73,9 +75,10 @@ export default class Retry {
try {
await this.callback.call(this.testEnvironment, this.assertProxy, ...this.callbackArgs, this.currentRun)
} catch (err) {
if (!this.shouldRetry) {
throw err
}
this.assertProxy.pushResult({
result: false,
message: err.message
})
}
}

Expand Down
45 changes: 39 additions & 6 deletions test/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,45 @@ retry('test retry async', async function (assert, currentRun) {
assert.equal(currentRun, 4)
}, 4)

retry('promise reject', async function (assert, currentRun) {
if (currentRun === 2) {
await Promise.reject(new Error('should be handled'))
}
assert.equal(currentRun, 5)
}, 5)
QUnit.module('error handling', function () {
retry('rejected promise is handled', async function (assert, currentRun) {
if (currentRun === 2) {
await Promise.reject(new Error('should be handled'))
}
assert.equal(currentRun, 5)
}, 5)

retry.todo('rejected promise on the last try is is not handled', async function (assert, currentRun) {
if (currentRun === 5) {
await Promise.reject(new Error('should not be handled'))
}
assert.equal(currentRun, 5)
}, 5)

retry('error is handled', async function (assert, currentRun) {
if (currentRun === 2) {
throw new Error('should be handled')
}
assert.equal(currentRun, 5)
}, 5)

retry.todo('error on the last try is not handled', async function (assert, currentRun) {
if (currentRun === 5) {
throw new Error('should not be handled')
}
assert.equal(currentRun, 5)
}, 5)

retry('failed assertion is handled', async function (assert, currentRun) {
assert.equal(currentRun, 5)
assert.ok(true)
}, 5)

retry.todo('failed assertion on the last try is not handled', async function (assert, currentRun) {
assert.ok(false)
assert.ok(true)
}, 5)
})

QUnit.module('hook context', function (hooks) {
hooks.beforeEach(function () {
Expand Down