Skip to content

Automatically cleanup if afterEach is detected #77

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

Closed
afontcu opened this issue Aug 10, 2019 · 12 comments · Fixed by #80
Closed

Automatically cleanup if afterEach is detected #77

afontcu opened this issue Aug 10, 2019 · 12 comments · Fixed by #80
Labels
enhancement New feature or request good first issue Good for newcomers has PR There's an open PR to solve/close the issue released

Comments

@afontcu
Copy link
Member

afontcu commented Aug 10, 2019

Right now, users need to call afterEach(cleanup) or configure Jest to do so. This makes sure test run in isolation, but requires manual action.


As React Testing Library just did, we should probably automatically call cleanup if afterEach method exists.

RTL ended up offering two opt-out mechanisms (via custom import or using an env variable), but I'm not sure this is totally needed. I would keep the env variable (VTL_SKIP_AUTO_CLEANUP), but still have a single import point (instead of creating that "pure" version on VTL). Not a big deal, though.

So a PR (or several PRs) should:

  • Automatically call afterEach(cleanup) if afterEach exists.
  • Add an opt-out mechanism by using VTL_SKIP_AUTO_CLEANUP.
  • Add tests asserting the behavior outlined above.
  • Remove cleanup references from tests and docs.
  • Replace cleanup-after-each with a warning (reference).
  • State that the change is a breaking change (so that we take it into account when merging!).

After that, we'd need to update the official docs.

I'm marking this as a good first issue since details are outlined and the referenced PR from RTL makes a great starting point :)

@afontcu afontcu added the good first issue Good for newcomers label Aug 10, 2019
@Oluwasetemi
Copy link
Contributor

Oluwasetemi commented Aug 10, 2019

Can I jump on this

@afontcu
Copy link
Member Author

afontcu commented Aug 10, 2019 via email

@Oluwasetemi
Copy link
Contributor

@afontcu can you please help me clarify some things.

  1. I will call afterEach(cleanup) in the main vue-testing-library.js?
  2. I don't know how to check if afterEach is called in a test?

@afontcu
Copy link
Member Author

afontcu commented Aug 10, 2019

Hi! Sure!

The idea is to follow React Testing Library steps, so their Pull Request is gonna be really helpful.

  1. Exactly, that's the main goal.
  2. It's not about afterEach being called in a test, it's about checking if "afterEach" exists at all. Some test runners, such as Ava, do not have this function. Check the index file for React Testing Library.

I'd suggest going over that merged PR and trying to understand how are they handling the auto-cleanup.

@Oluwasetemi
Copy link
Contributor

Currently going through the merged PR. I hope to send a PR when I get it all figure.

@afontcu
Copy link
Member Author

afontcu commented Aug 10, 2019

great :) feel free to create a draft PR so we can discuss things over code.

@Oluwasetemi
Copy link
Contributor

#80 have solved the first two concerns I think?

@afontcu
Copy link
Member Author

afontcu commented Aug 11, 2019

@Oluwasetemi yes, looks great! :)

Next step is testing it out, and also I would add some comments explaining the feature. Something like the following:

"if the test runner supports afterEach, then we'll automatically run cleanup after each test. This ensures that tests run in isolation from each other. You can opt out of that behavior by setting VTL_SKIP_AUTO_CLEANUP env varible to 'true'."

@afontcu afontcu added the has PR There's an open PR to solve/close the issue label Aug 11, 2019
@Oluwasetemi
Copy link
Contributor

I just created 2 test files to check the auto-cleanup and the auto-cleanup-skip.

cc @dfcook @afontcu

@afontcu
Copy link
Member Author

afontcu commented Aug 14, 2019

@Oluwasetemi perfect! I've added a few comments.

Next steps:

  1. "Remove cleanup references from tests". It means that all tests provided in Vue Testing Library should not use cleanup() themselves.
  2. "Replace cleanup-after-each with a warning". If someone is still importing cleanup-after-each.js after updating VTL, we should warn them that the file is deprecated and we might be removing it in future major releases. Similar to this.

Thank you! 🤗 It's looking really good.

@Oluwasetemi
Copy link
Contributor

I have updated the PR

@afontcu afontcu added the enhancement New feature or request label Aug 14, 2019
afontcu pushed a commit that referenced this issue Aug 19, 2019
You can disable this with the VTL_SKIP_CLEANUP environment variable, but it's recommended to have cleanup work this way.

Closes #77 

BREAKING CHANGE: If your tests were not isolated before (and you referenced the same component between tests) then this change will break your tests. You should keep your tests isolated, but if you're unable/unwilling to do that right away, then you can run your tests with the environment variable VTL_SKIP_AUTO_CLEANUP set to true.
@afontcu
Copy link
Member Author

afontcu commented Aug 19, 2019

🎉 This issue has been resolved in version 3.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers has PR There's an open PR to solve/close the issue released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants