Newer
Older
import { ReactWrapper } from 'enzyme'
import { act } from 'react-dom/test-utils'
export const waitForComponentToPaint = async <TP extends any = {}>(
wrapper: ReactWrapper<TP>
): Promise<void> => {
await act(async () => {
await new Promise(resolve => setTimeout(resolve, 0))
wrapper.update()
})
}
class NoErrorThrownError extends Error {}
/**
* @issue Jest only considers a test to have failed if it throws an error, meaning if calls to assertion functions like expect occur in conditional code such as a catch statement, tests can end up passing but not actually test anything.
* @solution A better way to handle this situation is to introduce a wrapper to handle the catching, and otherwise return a specific "no error thrown" error if nothing is thrown by the wrapped function
* @docs https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/no-conditional-expect.md#disallow-calling-expect-conditionally-no-conditional-expect
*/
export const getError = async <TError>(
call: () => unknown
): Promise<TError> => {
try {
await call()
throw new NoErrorThrownError('No error thrown')
} catch (error: unknown) {
return error as TError
}
}