Skip to content

Commit 9f54560

Browse files
committed
fetchActions tests: Test retry logic with a more representative error.
The fact that this test (before this commit) was passing isn't good -- it means a basically arbitrary, unexpected kind of error will let the retry loop continue without propagating to `tryFetch`'s caller. Instead, if something unexpected like that happens, we should fail early (and stop wasting the user's time) by breaking out of the retry loop and having the error propagate to the caller. We plan to fix that in a later series of commits, and add a test case with an error like that. But for now, test that we get the right behavior with representative inputs.
1 parent c7c696e commit 9f54560

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/message/__tests__/fetchActions-test.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,22 @@ describe('fetchActions', () => {
116116
jest.runAllTimers();
117117
});
118118

119-
test('retries a call, if there is an exception', async () => {
119+
// TODO: test more errors, like regular `new Error()`s. Unexpected
120+
// errors should actually cause the retry loop to break; we'll fix
121+
// that soon.
122+
test('retries a call if there is a non-client error', async () => {
123+
const serverError = new ApiError(500, {
124+
code: 'SOME_ERROR_CODE',
125+
msg: 'Internal Server Error',
126+
result: 'error',
127+
});
128+
120129
// fail on first call, succeed second time
121130
let callCount = 0;
122131
const thrower = jest.fn(() => {
123132
callCount++;
124133
if (callCount === 1) {
125-
throw new Error('First run exception');
134+
throw serverError;
126135
}
127136
return 'hello';
128137
});
@@ -135,7 +144,7 @@ describe('fetchActions', () => {
135144
await expect(tryFetch(tryFetchFunc)).resolves.toBe('hello');
136145

137146
expect(tryFetchFunc).toHaveBeenCalledTimes(2);
138-
await expect(tryFetchFunc.mock.results[0].value).rejects.toThrow('First run exception');
147+
await expect(tryFetchFunc.mock.results[0].value).rejects.toThrow(serverError);
139148
await expect(tryFetchFunc.mock.results[1].value).resolves.toBe('hello');
140149

141150
jest.runAllTimers();

src/message/fetchActions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ export async function tryFetch<T>(func: () => Promise<T>): Promise<T> {
265265
try {
266266
return await func();
267267
} catch (e) {
268+
// TODO: This should be narrowed to `!isServerError(e)`; we
269+
// should fail early if we encounter unrecognized / unexpected
270+
// errors.
268271
if (isClientError(e)) {
269272
throw e;
270273
}

0 commit comments

Comments
 (0)