-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix auth unit tests + Enable wrapping options when rendering in jest #6058
Conversation
☁️ Nx Cloud ReportCI is running/has finished running commands for commit 2000771. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this branch
✅ Successfully ran 9 targetsSent with 💌 from NxCloud. |
✅ Deploy Preview for redwoodjs-docs canceled.
|
const [rwClient, setRwClient] = useState<AuthClient>() | ||
const isMounted = useRef(true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually already have a hook for this inside the router package. If we agree on these changes I can either shuffle some code around so we can use the same hook in both packages. Or I just copy it over here (I prefer copying it over)
packages/auth/src/AuthProvider.tsx
Outdated
setRwClient(client) | ||
// This timeout lets default tests that only test that a component renders | ||
// pass | ||
setTimeout(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit hacky... But it does make it so we don't have to make any updates to already existing RW Apps out there.
If we want we could wrap this in process.env.NODE_ENV === 'test'
and skip the timeout in prod
It's a tradeoff if the extra code complexity the env check would bring is worth it or not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this some more I really don't like the "wrap in env check" idea. If we had that we'd no longer be testing the same code as we run in prod.
Slippery slope feels slippery! I’m worried this is introducing code with side effects we don’t understand, it seems like we’re forcing the code to run on next tick. I’d be more inclined to stick to stick to standard react and testing patterns so we don’t get untraceable bugs later. |
You're right. I did want to push this execution to the next tick. This lets those most basic tests finish before any auth state changes, thus not needing the |
Taking the above PR into consideration I've come up with a new solution. I've introduced a RW-specific it('renders Success successfully', async () => {
- expect(() => {
- render(<Success ${camelName}={standard().${camelName}} />)
- }).not.toThrow()
+ await expectToRender(<Success ${camelName}={standard().${camelName}} />)
}) So instead of doing that expect...not.toThrow thing, we now use this new WDYT? |
I just realized this whole problem will go away when/if we get this in: #5985. It no longer has the async behavior of the AuthProvider that's the root cause here. |
So... maybe we should HODL the clerk PR until we decouple auth. It's going to be a while though. I'm also wondering.... what's the reason we needed to move to a functional component anyway? Apart from "functional components are better" - is there an actual reason why we needed to do such a big refactor for one auth client? |
For me this is a step backwards - IMHO. Not only are we asking people to learn yet another method to test with, but we're also taking away the "implicit" learning that in a real test, not just the generated one - you should be using |
Those are all valid points. I want to involve Rob in this conversation too. Let's see if we can discuss on the next Core Team meeting |
We discussed this on a meeting. We decided that the best option for now was to roll back the Clerk PR and try to find a good solution for testing auth that works both with Clerk, and with the work we're doing on trying to decouple auth. The second best option was to start generating boilerplate testcases with |
We had a few error-printouts in our CI
And this was also affecting RW apps (so not just the framework)
Do note that these aren't test failures. The tests still pass. It just looks alarming.
This PR fixes those warnings, and also potential actual bugs where updates could happen to state in
<AuthProvider>
after it had been unmountedTodos
render
function from rwjs/testing/web does and how its different