Skip to content

Conversation

@lsminter
Copy link
Contributor

@lsminter lsminter commented Sep 8, 2022

When working on lesson 41 for Test React Components with Jest and React Testing Library, if you create a test that Rejects the value once, the test will fail with an error of TypeError: Cannot read property 'message' of undefined.

Screen Shot 2022-09-08 at 3 33 33 PM

This is the test:

test('Show error if exist', async () => { mockSubmitForm.mockRejectedValueOnce({message: 'testError'}) const testData = {food: 'test food', drink: 'test drink'} render(<App />) userEvent.click(await screen.findByText(/fill.*form/i)) userEvent.type(await screen.findByLabelText(/food/i), testData.food) userEvent.click(await screen.findByText(/next/i)) userEvent.type(await screen.findByLabelText(/drink/i), testData.drink) userEvent.click(await screen.findByText(/review/i)) expect(await screen.findByLabelText(/food/i)).toHaveTextContent(testData.food) expect(await screen.findByLabelText(/drink/i)).toHaveTextContent( testData.drink, ) userEvent.click(await screen.findByText(/confirm/i, {selector: 'button'})) expect(mockSubmitForm).toHaveBeenCalledWith(testData) expect(mockSubmitForm).toHaveBeenCalledTimes(1) userEvent.click(await screen.findByText(/home/i)) expect(await screen.findByText(/welcome home/i)).toBeInTheDocument() })

If you update history.push('/error', {state: {error}}) to history.push('/error', {error}) on line 96 in app.js, it fixes this error.

@kentcdodds kentcdodds merged commit ebd9e74 into kentcdodds:main Sep 25, 2022
@kentcdodds
Copy link
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants