Skip to content

Commit

Permalink
Fix onFailedAttempt and shouldRetry options being undefined (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
omril1 authored Nov 15, 2024
1 parent 21a22dd commit b400af6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
10 changes: 4 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ const decorateErrorWithCounts = (error, attemptNumber, options) => {

export default async function pRetry(input, options) {
return new Promise((resolve, reject) => {
options = {
onFailedAttempt() {},
retries: 10,
shouldRetry: () => true,
...options,
};
options = {...options};
options.onFailedAttempt ??= () => {};
options.shouldRetry ??= () => true;
options.retries ??= 10;

const operation = retry.operation(options);

Expand Down
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,32 @@ test('onFailedAttempt can throw, causing all retries to be aborted', async t =>
}
});

test('onFailedAttempt can be undefined', async t => {
const error = new Error('thrown from onFailedAttempt');

await t.throwsAsync(pRetry(() => {
throw error;
}, {
onFailedAttempt: undefined,
retries: 1,
}), {
is: error,
});
});

test('shouldRetry can be undefined', async t => {
const error = new Error('thrown from onFailedAttempt');

await t.throwsAsync(pRetry(() => {
throw error;
}, {
shouldRetry: undefined,
retries: 1,
}), {
is: error,
});
});

test('throws useful error message when non-error is thrown', async t => {
await t.throwsAsync(pRetry(() => {
throw 'foo'; // eslint-disable-line no-throw-literal
Expand Down

0 comments on commit b400af6

Please sign in to comment.