facebook / facebook/relay

Testing error boundaries using relay-test-utils

Open
#3,442 5 comments 0 reactions 0 assignees View on GitHub
wontfix
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

I am finding that no matter how I throw an error in a test it is never thrown. I have tried the below. The suspense and data ready states work without issues.

```javascript
// as per docs
environment.mock.queueOperationResolver(() => new Error("Uh-Oh"))

// using spy
jest.spyOn(reactRelay, "usePreloadedQuery").mockImplementation(() => { throw new Error("Uh-Oh") })
jest.spyOn(reactRelay, "usePreloadedQuery").mockReturnValue(new Error("Uh-Oh"))

// throwing in resolver
const customMockResolvers = { Entry: () => new Error("Uh-Oh") }
environment.queueOperationResolver(operation => MockPayloadGenerator.generate(operation, customMockResolvers))

// using the rejectMostRecentOperation always fails (I believe this is due to the component using useQueryLoader)
environment.mock.rejectMostRecentOperation(new Error("Uh-oh"))
// Invariant Violation: MockEnvironment: Cannot respond to request, it has not been requested yet.
```

I have also found that I have to pass add `mock.queuePendingOperation(EntryListQuery, {})` otherwise it remains in a suspending state. The docs it implies that this isn't required.

The component is really basic, so I can't see what is going wrong.

```javascript
const Entries = () => {
const [queryReference, loadQuery] = useQueryLoader(EntryListQuery)

useEffect(() => {
if (!queryReference) loadQuery()
}, [loadQuery, queryReference])

if (!queryReference) return

return (

}>



)
}
```

```javascript
// the child component

const EntryList = ({ queryReference }) => {
const { queryEntry } = usePreloadedQuery(EntryListQuery, queryReference)

return (


Entry List


New Entry


    {queryEntry.map(entry => {
    if (entry) {
    return (

  • {data.title}

  • )
    }
    return null
    })}


)
}
export { EntryList, EntryListQuery }

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.