Open nicoespeon opened 5 years ago
I think this is brilliant (I actually came here to open this issue after seeing the same issue raised on the Jest GH that seems to have been ignored).
@nicoespeon (the OP) has already written an implementation and published it, but having it contained within a repository that is regularly maintained and accessible would be of great value.
Feature Request
Hi there 👋
Description
I'd like to propose a new
Function
matcher:.toVerifyAllCombinations([args])
.It would test all combinations of given parameters to the function under test, and match against previous snapshot.
The main usage would be a test-after scenario (it's a snapshot test), to quickly set up a safety net over legacy code, before refactoring.
📖 Context: "Approval testing" of legacy code
Sometimes you don’t know what a piece of legacy code precisely does. But you do know it works in production. If you want to touch this piece of code, you'd better put some tests on it.
One approach to use in this situation is called "Approval testing". It can get you test coverage quickly without having to understand the code.
The recipe is the following:
In the end, you got a snapshot of what your code does. And you can start refactoring with confidence.
Further information:
📽 Usage examples
myFunction
would be the function to test, or a wrapper around it which returns a value we can snapshot.[args]
would be list of values to combine, for each argument ofmyFunction
.UX would be the one of using Jest's
.toMatchSnapshot()
.A theoretical example
It will test all following combinations:
myFunction(1, "random")
=>random #1
myFunction(1, "foo")
=>foo #1
myFunction(-1, "random")
=>This is twisted…
myFunction(-1, "foo")
=>-1 bar
And produce a snapshot of this, so we can ensure every path is tested.
Another, concrete example
Using Emily Bache's example of testing Gilded Rose behaviour, final test would look like that:
And the produced snapshot:
Possible solution
Implementation would look like this:
I already implemented the matcher and was about to make a package so I could use it. But I thought it might worth proposing to enhance the list of jest-extended matchers 💡
What are your feelings around this?
If you think it's a good idea, I can push a PR to implement.