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

feat: snapshot testing (1/*) #995

Merged
merged 6 commits into from
Jun 17, 2024
Merged

feat: snapshot testing (1/*) #995

merged 6 commits into from
Jun 17, 2024

Conversation

ematipico
Copy link
Member

Changes

I realised some time ago that testing inside the compiler can be a bit difficult due to the fact that sometimes we don't have "the whole picture": when we want to assert something, we just check that a portion of the text contains something. However, this isn't always sufficient because we can't see and understand the emitted artefact of the compiler.

This PR proposes to attempt to add snapshot testing inside our compiler infra.

In this PR, as a start, I implemented snapshot testing on top of our internal testing suite. I modified the snapshot testing library to emit a new snapshot file for each testing case to avoid tarnishing the same snapshot every time we add a new one.

What do you think?

Testing

The tests should pass

Docs

Later, if and once snapshot testing is implemented throughout the codebase.

Copy link

changeset-bot bot commented Apr 3, 2024

⚠️ No Changeset found

Latest commit: 7279642

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@matthewp
Copy link
Contributor

matthewp commented Apr 3, 2024

I like the idea of migrating to snapshots here. However it looks like from the code that you still need to add the manual want{}, so I think we should get rid of that if we're going to go with snapshots instead.

The bigger issue with testing, imo, is that we have this one giant test for every scenario in the print. This makes it hard to run a single test. There's a convention in go to test a single test: go test -run "Name of Test", but you can't do that with our tests because they are all inside of a single func TestPrinter. So if we're going to improve our testing, I'd like to see the metaprogramming stuff go away and just have a list of test functions so it's easier to run them in isolation.

@ematipico
Copy link
Member Author

However it looks like from the code that you still need to add the manual want{}, so I think we should get rid of that if we're going to go with snapshots instead.

That's done on purpose, I find it useful to use both approaches: check for a string and still printing the snapshot, mainly because when creating a new test, sometimes there isn't a clear understanding of when a snapshot is valid, so the want{} should be available (maybe optional) when needed.

The bigger issue with testing, imo, is that we have this one giant test for every scenario in the print. This makes it hard to run a single test.

This is already possible in our infra:

go test -v ./internal/... -run TestPrintToJSON/Fragment_Literal

This command line will run only one test

@ematipico ematipico force-pushed the chore/snapshot-testing branch 2 times, most recently from 39a14c9 to 9bfdccb Compare June 14, 2024 15:29
@ematipico ematipico force-pushed the chore/snapshot-testing branch from 9bfdccb to 7279642 Compare June 14, 2024 15:36
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

LGTM. But I have a question: Is there a command to update the snapshots or a command to not update them to assert it's matching? Is it possible with the go command?

@ematipico
Copy link
Member Author

LGTM. But I have a question: Is there a command to update the snapshots or a command to not update them to assert it's matching? Is it possible with the go command?

Yes, I plan to update the contribution guide after merging this PR.

The library freezes the snapshots by default, and you use an env var to update them.

@bluwy
Copy link
Member

bluwy commented Jun 17, 2024

👍 Alright thanks! The state of the PR is fine by me in this case.

@ematipico ematipico merged commit 4e7cc82 into main Jun 17, 2024
5 checks passed
@ematipico ematipico deleted the chore/snapshot-testing branch June 17, 2024 12:05
@ematipico ematipico mentioned this pull request Jul 1, 2024
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