Comments (5)
SnapshotT gets the name for the snapshot from calling t.Name()
. So as long as the testName
passed to t.Run
is unique for each of your test cases then SnapshotT(t, result)
should work just fine.
Do you have an example of where this doesn't work? (I'm hoping this is just a misunderstanding because the documentation is still a bit lacking...)
from cupaloy.
The problem is not just the name but also that t.Fail()
gets called on the wrong *testing.T
when the snapshots don't match. The example above should illustrate that. For now, I have solved it in the following way but, to me, that's not very pretty:
var testCases = map[string][]string{
"TestCaseOne": []string{......},
"AnotherTestCase": []string{......},
....
}
for testName, args := range testCases {
t.Run(testName, func(t *testing.T) {
var tcInput string
myMock := MyMock{
MockFunc: function (input string) {
tcInput = input
},
}
result := functionUnderTest(myMock, args...)
cupaloy.SnapshotT(t, tcInput, result)
})
}
However, this works for me. So if you think it's not worth increasing the API surface for my edge case, that's also fine with me. Just let me know =) The upside of this approach is that it summarizes all the assertions of one test case into one snapshot file.
from cupaloy.
I'm a bit confused. A tried to reproduce your issue using this code:
package cupaloy_test
import (
"github.com/bradleyjkemp/cupaloy"
"testing"
)
func Test_Repro(t *testing.T) {
var testCases = map[string]string{
"TestCaseOne": "testOneResult",
"AnotherTestCase": "anotherResult",
}
for testInput, testResult := range testCases {
t.Run(testInput, func(t *testing.T) {
cupaloy.SnapshotT(t, testInput, testResult)
})
}
}
When running normally I get the output:
=== RUN Test_Repro
--- PASS: Test_Repro (0.00s)
=== RUN Test_Repro/TestCaseOne
--- PASS: Test_Repro/TestCaseOne (0.00s)
=== RUN Test_Repro/AnotherTestCase
--- PASS: Test_Repro/AnotherTestCase (0.00s)
PASS
If I change "anotherResult" -> "anotherResultChanged"
Then I get the output:
=== RUN Test_Repro
--- FAIL: Test_Repro (0.00s)
=== RUN Test_Repro/TestCaseOne
--- PASS: Test_Repro/TestCaseOne (0.00s)
=== RUN Test_Repro/AnotherTestCase
--- FAIL: Test_Repro/AnotherTestCase (0.00s)
repro_test.go:16: snapshot not equal:
--- Previous
+++ Current
@@ -1,3 +1,3 @@
AnotherTestCase
-anotherResult
+anotherResultChanged
FAIL
Which looks correct (only the one test case has been marked failed)
from cupaloy.
What you have tested works as expected. I just thought it wasn't possible to have multiple SnapshotT
statements in one test case. Passing multiple values into the same SnapshotT
call is a workaround that works for me now. So for me, the issue can be closed.
Thanks for looking into it, @bradleyjkemp!
from cupaloy.
Ah I understand, yeah I think that's a pretty big hole in the documentation that needs to be filled. So thanks for helping identify this 🙂
from cupaloy.
Related Issues (20)
- Control whether snapshot mismatches are t.Errors or t.Fatals
- Fix Sourcegraph badge HOT 2
- Explain cupaloy better in README
- Be able to identify and clean up unused snapshots HOT 1
- Clarify WithOptions semantics
- Include snapshot diff in log/error when snapshots are updated/created
- Do not spew results using Stringer methods? HOT 1
- Automate adding .gitattributes tagging snapshots as generated files
- Make Cupaloy into a Go module HOT 1
- Fix broken Sourcegraph badge HOT 1
- Sort map keys when spewing HOT 4
- Use of internal package not allowed HOT 2
- Dependabot can't resolve your Go dependency files
- Single vs double spaced output HOT 6
- Add an option to write failed/changed snapshots to disk
- can't install HOT 2
- Feature request: support Property Matchers like Jest HOT 1
- Support for JSON Snapshots HOT 1
- Snapshot filename special character scrub needed - semicolon
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cupaloy.