Welcome to the Redwood community, and solid question.
The issue here is that the test isn’t waiting for current user to be populated - lets break it down into two problems:
1. Component always assumes current user is present
The way Redwood’s auth system works, is that it first tries to render your page, and if you use currentUser in an unauthenticated page, its advisable to always check if currentUser exists, before trying to access properties in your component.
e.g.
<p>{currentUser?.name}</p>
2. You need to waitFor currentUser to become available
For the above reason, and quoting the docs on this:
The async behavior here is important. Even after setting the user with mockCurrentUser(), currentUser may be null during the initial render because it’s being resolved. Waiting for a render update before passing/failing the exception gives the resolver a chance to execute and populate currentUser.
So to make your test wait till the render has finished:
I have a similar issue except the component does not render anything I can test against to check if the current user is populated. My example is a <FollowButton /> and if the user is not logged in, it is redirected to login. That case is easy to test. But how do I test with mockCurrentUser and make sure the user is logged in? I can pause for 100ms, but there must be a better way?
Then in our tests, we call await mockUser({ id: 12345, roles: ['admin'] }) after render(). This is helpful when you’re using hasRole() but not rendering anything about the current user.