Skip to content

[RFC] test: one snapshot per fixture test #1152

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
wants to merge 7 commits into from
Closed

Conversation

bradzacher
Copy link
Member

@bradzacher bradzacher commented Oct 28, 2019

Fixes #1142

With my recent 3.7 changes to the parser, I found it near impossible to use the snapshot test output, because the file was huge, vscode didn't display changes well. Additionally, because the snapshots were sorted alphabetically, adding a new snapshot with a similar filename / structure caused git to generate a messy diff which made it impossible to review.

I did some research into jest, and there's no way to generate multiple snapshot files for a single test file.

So there were two choices:

  1. write our own snapshot tests by writing the json results to disk individually, and comparing it ourselves.
  2. codegen a test file per fixture file.

I didn't want to rewrite the snapshot algorithm, so I decided to codegen.
It worked really well - a single little script generates one test per fixture for both parser and typescript-estree, then it's just the standard test pipeline per package.
Scary PR due to the changes, but I think it's worth it.

There is a tradeoff to this approach over having them all in one file - a complete test run takes a decent chunk longer (~12s to ~120s for parser, and ~40s to ~120s for typescript-estree). I would hazard a guess that this is due to the extra disk activity from writing more snaps, and jest probably spends some time setting up and tearing down an isolated environment for each test.

I don't see this as a huge problem, as we will only really run the tests on CI, and locally it's easy to exclude them from your test run because they're all within a folder called generated.

Draft to test the pipeline and gather feedback.


How does it work?

  • Add a test case to shared-fixtures/fixtures.
  • Run yarn generate-tests within the shared-fixtures root.
    • This will run this script
    • The script simply gathers all of the fixture files, and writes a test file to both the parser and typescript-estree folders.
    • The generated files are as minimal as possible, and simply call into the test helper functions with the correct fixture path: parser, typescript-estree.
  • cd into both the parser and typescript-estree roots, and run yarn test, which will pick up the generated tests, and create snapshots.

That's it! Because there is one file per fixture, jest outputs one snapshot per fixture.

I was thinking of extending this in future when #704 is ready, so we can make reviews even easier by including a more focused snapshot as well (one without the noise of range/loc).

@bradzacher bradzacher added the tests anything to do with testing label Oct 28, 2019
@typescript-eslint
Copy link
Contributor

Thanks for the PR, @bradzacher!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

@codecov
Copy link

codecov bot commented Nov 1, 2019

Codecov Report

Merging #1152 into master will decrease coverage by 0.01%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #1152      +/-   ##
==========================================
- Coverage   94.04%   94.02%   -0.02%     
==========================================
  Files         120      120              
  Lines        5220     5220              
  Branches     1445     1445              
==========================================
- Hits         4909     4908       -1     
  Misses        177      177              
- Partials      134      135       +1
Impacted Files Coverage Δ
packages/typescript-estree/src/parser.ts 93.96% <0%> (-0.87%) ⬇️
packages/typescript-estree/src/convert.ts 98.99% <0%> (ø) ⬆️
packages/typescript-estree/src/convert-comments.ts 96.29% <0%> (ø) ⬆️

@bradzacher bradzacher added the DO NOT MERGE PRs which should not be merged yet label Jan 21, 2020
@bradzacher bradzacher added the RFC label Jan 22, 2020
@bradzacher bradzacher closed this May 25, 2020
@bradzacher
Copy link
Member Author

Closing in favour of #1491

@bradzacher bradzacher deleted the separate-tests branch May 25, 2020 20:48
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
DO NOT MERGE PRs which should not be merged yet tests anything to do with testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor fixture tests so they don't generate a single giant snapshot file
1 participant