Skip to content

Commit

Permalink
fix: snaps.Update should apply and on snapshot create (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis authored Jan 28, 2025
1 parent 560fde0 commit 00f3055
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ t.Run("snapshot tests", func(t *testing.T) {
s := snaps.WithConfig(
snaps.Dir("my_dir"),
snaps.Filename("json_file"),
snaps.Ext(".json")
snaps.Ext(".json"),
snaps.Update(false)
)

Expand Down
2 changes: 1 addition & 1 deletion snaps/matchJSON.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func matchJSON(c *Config, t testingT, input any, matchers ...match.JSONMatcher)
snapshot := takeJSONSnapshot(j)
prevSnapshot, line, err := getPrevSnapshot(testID, snapPath)
if errors.Is(err, errSnapNotFound) {
if isCI {
if !shouldCreate(c.update) {
handleError(t, err)
return
}
Expand Down
13 changes: 13 additions & 0 deletions snaps/matchJSON_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ func TestMatchJSON(t *testing.T) {
test.Equal(t, 1, testEvents.items[erred])
})

t.Run("if snaps.Update(false) should skip creating snapshot", func(t *testing.T) {
setupSnapshot(t, fileName, false)

mockT := test.NewMockTestingT(t)
mockT.MockError = func(args ...any) {
test.Equal(t, errSnapNotFound, args[0].(error))
}

WithConfig(Update(false)).MatchJSON(mockT, "{}")

test.Equal(t, 1, testEvents.items[erred])
})

t.Run("should update snapshot when 'shouldUpdate'", func(t *testing.T) {
snapPath := setupSnapshot(t, jsonFilename, false, true)

Expand Down
2 changes: 1 addition & 1 deletion snaps/matchSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func matchSnapshot(c *Config, t testingT, values ...any) {
snapshot := takeSnapshot(values)
prevSnapshot, line, err := getPrevSnapshot(testID, snapPath)
if errors.Is(err, errSnapNotFound) {
if isCI {
if !shouldCreate(c.update) {
handleError(t, err)
return
}
Expand Down
13 changes: 13 additions & 0 deletions snaps/matchSnapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ func TestMatchSnapshot(t *testing.T) {
test.Equal(t, 1, testEvents.items[erred])
})

t.Run("if snaps.Update(false) should skip creating snapshot", func(t *testing.T) {
setupSnapshot(t, fileName, false)

mockT := test.NewMockTestingT(t)
mockT.MockError = func(args ...any) {
test.Equal(t, errSnapNotFound, args[0].(error))
}

WithConfig(Update(false)).MatchSnapshot(mockT, 10, "hello world")

test.Equal(t, 1, testEvents.items[erred])
})

t.Run("should return error when diff is found", func(t *testing.T) {
setupSnapshot(t, fileName, false)

Expand Down
2 changes: 1 addition & 1 deletion snaps/matchStandaloneJSON.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func matchStandaloneJSON(c *Config, t testingT, input any, matchers ...match.JSO
snapshot := takeJSONSnapshot(j)
prevSnapshot, err := getPrevStandaloneSnapshot(snapPath)
if errors.Is(err, errSnapNotFound) {
if isCI {
if !shouldCreate(c.update) {
handleError(t, err)
return
}
Expand Down
13 changes: 13 additions & 0 deletions snaps/matchStandaloneJSON_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ func TestMatchStandaloneJSON(t *testing.T) {
test.Equal(t, 1, testEvents.items[erred])
})

t.Run("if snaps.Update(false) should skip creating snapshot", func(t *testing.T) {
setupSnapshot(t, fileName, false)

mockT := test.NewMockTestingT(t)
mockT.MockError = func(args ...any) {
test.Equal(t, errSnapNotFound, args[0].(error))
}

WithConfig(Update(false)).MatchStandaloneJSON(mockT, "{}")

test.Equal(t, 1, testEvents.items[erred])
})

t.Run("should update snapshot when 'shouldUpdate'", func(t *testing.T) {
snapPath := setupSnapshot(t, jsonStandaloneFilename, false, true)

Expand Down
2 changes: 1 addition & 1 deletion snaps/matchStandaloneSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func matchStandaloneSnapshot(c *Config, t testingT, input any) {
snapshot := pretty.Sprint(input)
prevSnapshot, err := getPrevStandaloneSnapshot(snapPath)
if errors.Is(err, errSnapNotFound) {
if isCI {
if !shouldCreate(c.update) {
handleError(t, err)
return
}
Expand Down
13 changes: 13 additions & 0 deletions snaps/matchStandaloneSnapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ func TestMatchStandaloneSnapshot(t *testing.T) {
test.Equal(t, 1, testEvents.items[erred])
})

t.Run("if snaps.Update(false) should skip creating snapshot", func(t *testing.T) {
setupSnapshot(t, fileName, false)

mockT := test.NewMockTestingT(t)
mockT.MockError = func(args ...any) {
test.Equal(t, errSnapNotFound, args[0].(error))
}

WithConfig(Update(false)).MatchStandaloneSnapshot(mockT, "hello world")

test.Equal(t, 1, testEvents.items[erred])
})

t.Run("should return error when diff is found", func(t *testing.T) {
setupSnapshot(t, standaloneFilename, false)

Expand Down
2 changes: 1 addition & 1 deletion snaps/matchYAML.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func matchYAML(c *Config, t testingT, input any, matchers ...match.YAMLMatcher)
snapshot := takeYAMLSnapshot(y)
prevSnapshot, line, err := getPrevSnapshot(testID, snapPath)
if errors.Is(err, errSnapNotFound) {
if isCI {
if !shouldCreate(c.update) {
handleError(t, err)
return
}
Expand Down
13 changes: 13 additions & 0 deletions snaps/matchYAML_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ items:
test.Equal(t, 1, testEvents.items[erred])
})

t.Run("if snaps.Update(false) should skip creating snapshot", func(t *testing.T) {
setupSnapshot(t, fileName, false)

mockT := test.NewMockTestingT(t)
mockT.MockError = func(args ...any) {
test.Equal(t, errSnapNotFound, args[0].(error))
}

WithConfig(Update(false)).MatchYAML(mockT, "")

test.Equal(t, 1, testEvents.items[erred])
})

t.Run("should update snapshot when 'shouldUpdate'", func(t *testing.T) {
snapPath := setupSnapshot(t, yamlFilename, false, true)
printerExpectedCalls := []func(received any){
Expand Down
13 changes: 13 additions & 0 deletions snaps/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,16 @@ func shouldUpdate(u *bool) bool {

return updateVAR == "true"
}

// shouldCreate determines whether snapshots should be created
func shouldCreate(u *bool) bool {
if isCI {
return false
}

if u != nil {
return *u
}

return true
}

0 comments on commit 00f3055

Please sign in to comment.