As you already mentioned there is expect().toThrow()
🙂
So in your case:
test("should throw error when not wrapped inside `UserProvider`", () => {
expect(() => render(<TestComponent />))
.toThrow("Cannot use `useUser` outside of `UserProvider`");
});
Regarding the console.error
:
Currently there is by design no way to turn off the default error logs.
If you want to hide errors, you still need to mock console.error
.
When you mock functions like console.error
you want to restore them in a afterEach
callback so that they are also restored if the test fails.