Skip to content

Commit

Permalink
chore: clean up test mocks and change getTestID param order (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis authored Jan 22, 2024
1 parent 080bbd2 commit c904be0
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 254 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ jobs:
with:
go-version: 1.21.x
- name: Run Tests
run: make test-verbose
run: make test
65 changes: 59 additions & 6 deletions internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,96 @@ type MockTestingT struct {
MockError func(...any)
MockLog func(...any)
MockCleanup func(func())
t *testing.T
}

// NewMockTestingT returns a MockTestingT with common default values
func NewMockTestingT(t *testing.T) MockTestingT {
return MockTestingT{
MockHelper: func() {},
MockCleanup: func(f func()) {
f()
},
MockName: func() string {
return "mock-name"
},
t: t,
}
}

func (m MockTestingT) Error(args ...any) {
m.t.Helper()

if m.MockError == nil {
m.t.Errorf("t.Error was not expected to be called")
return
}
m.MockError(args...)
}

func (m MockTestingT) Helper() {
m.t.Helper()

m.MockHelper()
}

func (m MockTestingT) Skip(args ...any) {
m.t.Helper()

if m.MockSkip == nil {
m.t.Errorf("t.Skip was not expected to be called")
return
}
m.MockSkip(args...)
}

func (m MockTestingT) Skipf(format string, args ...any) {
m.t.Helper()

if m.MockSkipf == nil {
m.t.Errorf("t.Skipf was not expected to be called")
return
}
m.MockSkipf(format, args...)
}

func (m MockTestingT) SkipNow() {
m.t.Helper()

if m.MockSkipNow == nil {
m.t.Errorf("t.SkipNow was not expected to be called")
return
}
m.MockSkipNow()
}

func (m MockTestingT) Name() string {
m.t.Helper()

if m.MockName == nil {
m.t.Errorf("t.Name was not expected to be called")
return ""
}
return m.MockName()
}

func (m MockTestingT) Log(args ...any) {
m.t.Helper()

if m.MockLog == nil {
m.t.Errorf("t.Log was not expected to be called")
return
}
m.MockLog(args...)
}

func (m MockTestingT) Cleanup(f func()) {
m.t.Helper()

if m.MockCleanup == nil {
m.t.Errorf("t.Cleanup was not expected to be called")
return
}
m.MockCleanup(f)
}

Expand All @@ -67,12 +126,6 @@ func Contains(t *testing.T, s, substr string) {
}
}

// NotCalled is going to mark a test as failed if called
func NotCalled(t *testing.T) {
t.Helper()
t.Errorf("function was not expected to be called")
}

func CreateTempFile(t *testing.T, data string) string {
dir := t.TempDir()
path := filepath.Join(dir, "mock.file")
Expand Down
2 changes: 1 addition & 1 deletion snaps/matchJSON.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func matchJSON(c *config, t testingT, input any, matchers ...match.JSONMatcher)
t.Helper()

snapPath, snapPathRel := snapshotPath(c)
testID := testsRegistry.getTestID(t.Name(), snapPath)
testID := testsRegistry.getTestID(snapPath, t.Name())

j, err := validateJSON(input)
if err != nil {
Expand Down
93 changes: 23 additions & 70 deletions snaps/matchJSON_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,8 @@ func TestMatchJSON(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
snapPath := setupSnapshot(t, jsonFilename, false)

mockT := test.MockTestingT{
MockHelper: func() {},
MockName: func() string {
return "mock-name"
},
MockError: func(args ...any) {
test.NotCalled(t)
},
MockLog: func(args ...any) { test.Equal(t, addedMsg, args[0].(string)) },
}
mockT := test.NewMockTestingT(t)
mockT.MockLog = func(args ...any) { test.Equal(t, addedMsg, args[0].(string)) }

MatchJSON(mockT, tc.input)

Expand Down Expand Up @@ -96,18 +88,9 @@ func TestMatchJSON(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
setupSnapshot(t, jsonFilename, false)

mockT := test.MockTestingT{
MockHelper: func() {},
MockName: func() string {
return "mock-name"
},
MockError: func(args ...any) {
test.Equal(t, tc.err, (args[0].(error)).Error())
},
MockLog: func(args ...any) {
// this is called when snapshot is written successfully
test.NotCalled(t)
},
mockT := test.NewMockTestingT(t)
mockT.MockError = func(args ...any) {
test.Equal(t, tc.err, (args[0].(error)).Error())
}

MatchJSON(mockT, tc.input)
Expand All @@ -119,16 +102,8 @@ func TestMatchJSON(t *testing.T) {
t.Run("should apply matchers in order", func(t *testing.T) {
setupSnapshot(t, jsonFilename, false)

mockT := test.MockTestingT{
MockHelper: func() {},
MockName: func() string {
return "mock-name"
},
MockError: func(args ...any) {
test.NotCalled(t)
},
MockLog: func(args ...any) { test.Equal(t, addedMsg, args[0].(string)) },
}
mockT := test.NewMockTestingT(t)
mockT.MockLog = func(args ...any) { test.Equal(t, addedMsg, args[0].(string)) }

c1 := func(val any) (any, error) {
return map[string]any{"key2": nil}, nil
Expand All @@ -152,20 +127,14 @@ func TestMatchJSON(t *testing.T) {
t.Run("should aggregate errors from matchers", func(t *testing.T) {
setupSnapshot(t, jsonFilename, false)

mockT := test.MockTestingT{
MockHelper: func() {},
MockName: func() string {
return "mock-name"
},
MockError: func(args ...any) {
test.Equal(t,
"\x1b[31;1m\n✕ match.Custom(\"age\") - mock error"+
"\x1b[0m\x1b[31;1m\n✕ match.Any(\"missing.key.1\") - path does not exist"+
"\x1b[0m\x1b[31;1m\n✕ match.Any(\"missing.key.2\") - path does not exist\x1b[0m",
args[0],
)
},
MockLog: func(args ...any) { test.NotCalled(t) },
mockT := test.NewMockTestingT(t)
mockT.MockError = func(args ...any) {
test.Equal(t,
"\x1b[31;1m\n✕ match.Custom(\"age\") - mock error"+
"\x1b[0m\x1b[31;1m\n✕ match.Any(\"missing.key.1\") - path does not exist"+
"\x1b[0m\x1b[31;1m\n✕ match.Any(\"missing.key.2\") - path does not exist\x1b[0m",
args[0],
)
}

c := func(val any) (any, error) {
Expand All @@ -183,17 +152,9 @@ func TestMatchJSON(t *testing.T) {
t.Run("if it's running on ci should skip creating snapshot", func(t *testing.T) {
setupSnapshot(t, jsonFilename, true)

mockT := test.MockTestingT{
MockHelper: func() {},
MockName: func() string {
return "mock-name"
},
MockError: func(args ...any) {
test.Equal(t, errSnapNotFound, args[0].(error))
},
MockLog: func(args ...any) {
test.NotCalled(t)
},
mockT := test.NewMockTestingT(t)
mockT.MockError = func(args ...any) {
test.Equal(t, errSnapNotFound, args[0].(error))
}

MatchJSON(mockT, "{}")
Expand All @@ -208,20 +169,12 @@ func TestMatchJSON(t *testing.T) {
func(received any) { test.Equal(t, addedMsg, received.(string)) },
func(received any) { test.Equal(t, updatedMsg, received.(string)) },
}
mockT := test.MockTestingT{
MockHelper: func() {},
MockName: func() string {
return "mock-name"
},
MockError: func(args ...any) {
test.NotCalled(t)
},
MockLog: func(args ...any) {
printerExpectedCalls[0](args[0])
mockT := test.NewMockTestingT(t)
mockT.MockLog = func(args ...any) {
printerExpectedCalls[0](args[0])

// shift
printerExpectedCalls = printerExpectedCalls[1:]
},
// shift
printerExpectedCalls = printerExpectedCalls[1:]
}

// First call for creating the snapshot
Expand Down
2 changes: 1 addition & 1 deletion snaps/matchSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func matchSnapshot(c *config, t testingT, values ...any) {
}

snapPath, snapPathRel := snapshotPath(c)
testID := testsRegistry.getTestID(t.Name(), snapPath)
testID := testsRegistry.getTestID(snapPath, t.Name())
snapshot := takeSnapshot(values)
prevSnapshot, line, err := getPrevSnapshot(testID, snapPath)
if errors.Is(err, errSnapNotFound) {
Expand Down
Loading

0 comments on commit c904be0

Please sign in to comment.