Skip to content

Commit 94d6333

Browse files
committed
Add tests for error preservation and IS_REACT_ACT_ENVIRONMENT restauration
1 parent b2e8200 commit 94d6333

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/__tests__/act.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react'
2-
import {render, fireEvent, screen} from '../'
2+
import {act, render, fireEvent, screen} from '../'
33

44
test('render calls useEffect immediately', () => {
55
const effectCb = jest.fn()
@@ -43,3 +43,27 @@ test('calls to hydrate will run useEffects', () => {
4343
render(<MyUselessComponent />, {hydrate: true})
4444
expect(effectCb).toHaveBeenCalledTimes(1)
4545
})
46+
47+
test('cleans up IS_REACT_ACT_ENVIRONMENT if its callback throws', () => {
48+
global.IS_REACT_ACT_ENVIRONMENT = false
49+
50+
expect(() =>
51+
act(() => {
52+
throw new Error('threw')
53+
}),
54+
).toThrow('threw')
55+
56+
expect(global.IS_REACT_ACT_ENVIRONMENT).toEqual(false)
57+
})
58+
59+
test('cleans up IS_REACT_ACT_ENVIRONMENT if its async callback throws', async () => {
60+
global.IS_REACT_ACT_ENVIRONMENT = false
61+
62+
await expect(() =>
63+
act(async () => {
64+
throw new Error('thenable threw')
65+
}),
66+
).rejects.toThrow('thenable threw')
67+
68+
expect(global.IS_REACT_ACT_ENVIRONMENT).toEqual(false)
69+
})

src/act-compat.js

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ function withGlobalActEnvironment(actImplementation) {
7979
return actResult
8080
}
8181
} catch (error) {
82+
// Can't be a `finally {}` block since we don't know if we have to immediately restore IS_REACT_ACT_ENVIRONMENT
83+
// or if we have to await the callback first.
8284
setReactActEnvironment(previousActEnvironment)
8385
throw error
8486
}

0 commit comments

Comments
 (0)