Skip to content

Commit 4616e45

Browse files
fix(test-renderer): safely access mock root in getInstance (#2278)
1 parent a11b7b2 commit 4616e45

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

packages/test-renderer/src/index.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,19 @@ const create = async (element: React.ReactNode, options?: Partial<CreateOptions>
5656
getInstance: () => {
5757
// this is our root
5858
const fiber = mockRoots.get(_fiber)?.fiber
59-
const root = {
60-
/**
61-
* we wrap our child in a Provider component
62-
* and context.Provider, so do a little
63-
* artificial dive to get round this and
64-
* pass context.Provider as if it was the
65-
* actual react root
66-
*/
67-
current: fiber.current.child.child,
68-
}
69-
if (fiber.current.child.child) {
59+
const current = fiber?.current.child.child
60+
if (current) {
61+
const root = {
62+
/**
63+
* we wrap our child in a Provider component
64+
* and context.Provider, so do a little
65+
* artificial dive to get round this and
66+
* pass context.Provider as if it was the
67+
* actual react root
68+
*/
69+
current,
70+
}
71+
7072
/**
7173
* so this actually returns the instance
7274
* the user has passed through as a Fiber

0 commit comments

Comments
 (0)