Giter Club home page Giter Club logo

Comments (5)

bradleyjkemp avatar bradleyjkemp commented on May 18, 2024

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.

cloudlena avatar cloudlena commented on May 18, 2024

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.

bradleyjkemp avatar bradleyjkemp commented on May 18, 2024

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.

cloudlena avatar cloudlena commented on May 18, 2024

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.

bradleyjkemp avatar bradleyjkemp commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.