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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(jest): opt-in to shorter snapshot format #4843

Merged
merged 8 commits into from Aug 31, 2021

Conversation

Haroenv
Copy link
Contributor

@Haroenv Haroenv commented Aug 23, 2021

Summary

Changing from snapshot to pure expect is useful sometimes if you know the assertion will be pretty hard to write, but before this PR requires a lot of editing. Since we use this shorter format for snapshots, no more "Array" or "Object" will be printed in snapshots.

Result

  • no more Object or Array in snapshots 馃檶
  • latest jest (setImmediate no longer exists in browser tasks, causing removal of runAllMicroTasks)

This PR is easier to review as their individual commits

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 23, 2021

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 3f5ec1d:

Sandbox Source
InstantSearch.js Configuration

@Haroenv Haroenv marked this pull request as ready for review August 30, 2021 12:46
src/lib/__tests__/InstantSearch-test.tsx Outdated Show resolved Hide resolved
@@ -417,7 +417,7 @@ See https://www.algolia.com/doc/api-reference/widgets/configure/js/`);

search.start();

await runAllMicroTasks();
await Promise.resolve();
Copy link
Contributor

Choose a reason for hiding this comment

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

What if we use wait(0) here too?
Or does it make sense to bring runAllMicroTasks back and replace its implementation with wait()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we definitely can use wait(0), but I'm not sure if it really means the same, this code happens before a timeout runs

Copy link
Contributor

Choose a reason for hiding this comment

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

While I understand what you mean, I fear this subtleness that might not make any real life difference could complicate writing tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense, I'll check whether those tests can be done with wait too

@@ -1047,7 +1047,8 @@ describe('dispose', () => {

search.start();

await runAllMicroTasks();
await Promise.resolve();
await Promise.resolve();
Copy link
Contributor

Choose a reason for hiding this comment

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

Calling it once didn't pass the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, there's two micro-tasks that need to happen, this implementation makes that clearer than wait(0) IMO

Copy link
Contributor

Choose a reason for hiding this comment

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

oh I see, but I feel like this is testing too much detail here because what we want to know is if onRender is called after rendering happens. We do not want to test if onRender is called two micro-tasks after start().

So I'm wondering what if we

  • const runAllMicroTasks = () => wait(0) and use it normally
  • use await Promise.resolve() when we need to test specific timing issue?

// Wait for the `render`
await Promise.resolve();
await Promise.resolve();
await wait(search._stalledSearchDelay);
Copy link
Contributor

Choose a reason for hiding this comment

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

nice touch

@Haroenv Haroenv merged commit 9f1831c into master Aug 31, 2021
@Haroenv Haroenv deleted the chore/jest-snapshot-smaller branch August 31, 2021 08:52
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.

None yet

2 participants