'Jest RTL tests passing but getting Process finished with exit code 1 intermittently
My tests are all passing successfully, at least in practise. I ran the tests 20 times and 19 times it passed. Once it failed, the code had not changed. I have still not been able to narrow this down, however as soon as I remove the async/wait test, it will pass 100% of the time: it('displays a validation error on missing email address', async () => { .... expect(await screen.findByText(emailRequired)).toBeInTheDocument()
I am asking, how can I have the tests consistently pass when the tests should always pass?
package.json
"test": "react-scripts test --watchAll=false --no-cache --runInBand",
cmd
hutber@hutber:/var/www/target/broker-dashboard$ yarn test src/pages/ForgotPassword/ForgotPassword.test.tsx
yarn run v1.22.17
$ react-scripts test --watchAll=false --no-cache --runInBand src/pages/ForgotPassword/ForgotPassword.test.tsx
PASS src/pages/ForgotPassword/ForgotPassword.test.tsx
Test the ForgotPassword page
✓ renders without error (115 ms)
✓ displays an email input (30 ms)
✓ displays a validation error on missing email address (62 ms)
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 3.236 s
Ran all test suites matching /src\/pages\/ForgotPassword\/ForgotPassword.test.tsx/i.
Done in 3.74s.
hutber@hutber:/var/www/target/broker-dashboard$ yarn test src/pages/ForgotPassword/ForgotPassword.test.tsx
yarn run v1.22.17
$ react-scripts test --watchAll=false --no-cache --runInBand src/pages/ForgotPassword/ForgotPassword.test.tsx
PASS src/pages/ForgotPassword/ForgotPassword.test.tsx
Test the ForgotPassword page
✓ renders without error (194 ms)
✓ displays an email input (50 ms)
✓ displays a validation error on missing email address (106 ms)
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 3.551 s
Ran all test suites matching /src\/pages\/ForgotPassword\/ForgotPassword.test.tsx/i.
Done in 4.06s.
hutber@hutber:/var/www/target/broker-dashboard$ yarn test src/pages/ForgotPassword/ForgotPassword.test.tsx
yarn run v1.22.17
$ react-scripts test --watchAll=false --no-cache --runInBand src/pages/ForgotPassword/ForgotPassword.test.tsx
RUNS src/pages/ForgotPassword/ForgotPassword.test.tsx
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
You will see that tests are marked as "RUNS" i.e they don't actually fail, but possible timeout and then get reported as failing.
Test
import { render, fireEvent, act, waitFor, screen } from '@testing-library/react'
import { MemoryRouter as Router } from 'react-router-dom'
import { StoreProvider } from 'easy-peasy'
import store from 'store'
import { SnackbarProvider } from 'notistack'
import { SnackbarUtilsConfigurator } from 'appContext/SnackbarUtils'
import { ThemeProvider } from '@mui/material'
import { theme } from 'themes'
import ForgotPassword from '.'
const Component = () => {
return (
<ThemeProvider theme={theme}>
<StoreProvider store={store}>
<Router>
<SnackbarProvider anchorOrigin={{ vertical: 'top', horizontal: 'right' }}>
<ForgotPassword />
<SnackbarUtilsConfigurator />
</SnackbarProvider>
</Router>
</StoreProvider>
</ThemeProvider>
)
}
describe('Test the ForgotPassword page', () => {
it('renders without error', () => {
render(<Component />)
expect(screen.getByText('forgot_password_header')).toBeTruthy()
})
/*
** NOTE: the use of i18n.t in UNIT TESTS with Yup doesn't work - so use the
** translation instead of the key, but only in the unit tests, it's ok in the
** schema. It's currently unknown what happens if there are multiple languages.
*/
it('displays an email input', () => {
render(<Component />)
const expected = screen.getByText('appHeader.lbl_noAccount')
expect(expected).toBeInTheDocument()
})
const emailRequired = /email is required/i
const emailInvalid = /enter a valid email/i
it('displays a validation error on missing email address', async () => {
render(<Component />)
expect(screen.queryByText(emailRequired)).not.toBeInTheDocument()
const emailFormInput = screen.getByTestId('email-form-input')
fireEvent.blur(emailFormInput)
expect(await screen.findByText(emailRequired)).toBeInTheDocument()
})
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
