test: add mutation testing setup #12090
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
I had some fun doing mutation testing in the Gatsby code base this weekend and came up with a mutation testing script that I think could benefit this project in general. It could be a useful tool for investigating/improving test cases, making sure they are efficient. This PR adds a mutation testing setup based on Stryker.
TL;DR
What is mutation testing?
Mutation testing is basically testing the quality of your tests to see if they are effective in catching bugs. Don’t get me wrong, measuring test coverage is great — it can help you find untested parts of your code. Test coverage does however not tell you much about the actual quality of your tests or give any sort of indication when they are good enough. Even if you reach 100% test coverage, that basically only tells you that all lines were executed during the test run — not that they actually work as expected. Mutation testing helps you to verify that your assertions are good and points out if you have missed anything important
See an example here
Use cases in the Gatsby project
Mutating Gatsby packages
Mutation testing is a somewhat tedious process (takes a lot of time to run). The script, therefore, takes some useful arguments to make this easier to work with.
Mutate one/several packages
Mutate a single file
Mutate all packages
Note 1: Reports are outputted to /mutation
Note 2: The mutation testing process will only run unit tests in the package folder (no e2e/integration).
Gatsby mutation reports
The setup includes a HTML reporter which outputs useful HTML-files showing detailed information about each mutated file.
The reports for most Gatsby packages can be found in this repo:
ollelauribostrom/gatsby-mutation-score.
(Only packages containing unit tests are included in the repo above and unfortunately the
gatsby
package is not included since I didn't have time to wait for stryker to check all 7500 mutants that were generated) 😀