Skip to content
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

refactor: use TestSWRConfig for error-test #1404

Merged

Conversation

koba04
Copy link
Collaborator

@koba04 koba04 commented Aug 29, 2021

This is a PR to propose to make unit tests more reliable.
Currently, most of the tests depend on a global cache store, which means that those tests share the same cache. This is fragile because the tests might start failing when we use the same key in between them.

SWR has createKey as a utility function for testing to generate a random key, which would solve the problem, but I think running tests on independent environments is important and makes tests more reliable.

To fix this, I've added <TestSWRConfig />, which creates its own cache provider, and wrapped target components in it. We don't have to care about duplicating keys while using <TestSWRConfig />.
I used the name because I expect to be added more configurations for testing in the component. More explicit names like <SWRCacheBoundary /> might be better.

What do you think? If this looks good to you, I'll refactor other tests as well.

I've also come up with wrapping the render function that testing-library provides, but I didn't adopt the approach because it's less clear what the function does and introduces additional abstraction. It's also hard to cover tests for concurrent rendering because it doesn't use the testing-library's render function.

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 15490e8:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration
SWR-SSR Configuration

createResponse(new Error('error!'))
)
if (error) return <div>{error.message}</div>
return <div>hello, {data}</div>
}

render(<Page />)
render(
<TestSWRConfig>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we should replace this with wrapping context provider. Most of the tests are also to make sure they're good to run without context provider. We shouldn't replace unique key with a standalone cache in context.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback! That's a good point.
Is the point solved by adding tests with a global cache or should we confirm to pass existing test cases with a global cache?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the point solved by adding tests with a global cache or should we confirm to pass existing test cases with a global cache?

Yeah that's better 👍

@koba04
Copy link
Collaborator Author

koba04 commented Aug 29, 2021

Another alternative way is to clear the global cache in the jest's setup file.

@huozhi
Copy link
Member

huozhi commented Aug 29, 2021

Another alternative way is to clear the global cache in the jest's setup file.

clearing cache for each test case sgtm.

@huozhi
Copy link
Member

huozhi commented Aug 29, 2021

Updating branch with latest change should be able to unblock the checks 🙏

@shuding
Copy link
Member

shuding commented Aug 30, 2021

I like the idea of creating a wrapper/helper util to make writing tests easier!

@shuding shuding merged commit 822f07d into vercel:master Sep 1, 2021
@shuding
Copy link
Member

shuding commented Sep 1, 2021

Makes sense to merge this for now! Thank you!

@koba04 koba04 deleted the refactor-use-swr-test-config-for-error branch September 1, 2021 11:46
@koba04
Copy link
Collaborator Author

koba04 commented Sep 1, 2021

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants