---
# System prepended metadata

title: 'Use FindBy* more, test failure due to mocks - Mon 02 Nov, 2020'
tags: [factory, act, programmingjournal, testing, mocks, '2020', jest]

---

# Use FindBy* more, test failure due to mocks - Mon 02 Nov, 2020

Odd mock issue causing a component test to fail today.

I've been using [factory.ts](https://github.com/willryan/factory.ts) along with Faker to generate mocks based on my TypeScript types for queries and mutations.

Not only does this make it possible to build an app (almost) completely with mocks, but these mocks are useful in testing.

Doing this with one key was causing the test to fail randomly:

```typescript=
reactions: Feed_ReactionsMock.buildList(3)
```

I had to hardcode it instead:

```typescript=
reactions: [{ count: 1, currentUserReacted: false, reactionId: 1 }],
```

I'm still not sure if it was a bug with my mocks or with the library itself.

### Use `findBy*` more in async tests

Getting that pesky `act()` error with Jest and React Testing Library? Try using `findBy` with async/await:

```typescript=
test('it should show the thing', async () => {
    const { findByTestId } = render(<MyComponent {...props} />)
    expect(await findByTestId('thing')).toBeDefined()
})
```

###### tags: `programmingjournal` `2020` `testing` `jest` `act` `mocks` `factory`