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

Prevent unhandled promises in createAsyncThunk #570

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
setupFilesAfterEnv: ['./jest.setup.js']
}
7 changes: 7 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
process.on('unhandledRejection', error => {
console.error(
`We throw this specifically to break tests if a promise is not handled`,
error
)
throw error
})
15 changes: 15 additions & 0 deletions src/createAsyncThunk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,21 @@ describe('conditional skipping of asyncThunks', () => {
expect(dispatch).toHaveBeenCalledTimes(0)
})

test('does not throw when attempting to abort a canceled promise', async () => {
const asyncPayloadCreator = jest.fn(async (x: typeof arg) => {
await new Promise(resolve => setTimeout(resolve, 2000))
return 10
})

const asyncThunk = createAsyncThunk('test', asyncPayloadCreator, {
condition
})
const promise = asyncThunk(arg)(dispatch, getState, extra)
promise.abort(
`If we didn't abortedPromise.catch(), this would crash the tests`
)
})

test('rejected action can be dispatched via option', async () => {
const asyncThunk = createAsyncThunk('test', payloadCreator, {
condition,
Expand Down
1 change: 1 addition & 0 deletions src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ If you want to use the AbortController to react to \`abort\` events, please cons
options.condition &&
options.condition(arg, { getState, extra }) === false
) {
abortedPromise.catch(() => {})
throw {
name: 'ConditionError',
message: 'Aborted due to condition callback returning false.'
Expand Down