Skip to content

Commit 00f3055

Browse files
authored
fix: snaps.Update should apply and on snapshot create (#119)
1 parent 560fde0 commit 00f3055

12 files changed

+84
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ t.Run("snapshot tests", func(t *testing.T) {
282282
s := snaps.WithConfig(
283283
snaps.Dir("my_dir"),
284284
snaps.Filename("json_file"),
285-
snaps.Ext(".json")
285+
snaps.Ext(".json"),
286286
snaps.Update(false)
287287
)
288288

snaps/matchJSON.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func matchJSON(c *Config, t testingT, input any, matchers ...match.JSONMatcher)
100100
snapshot := takeJSONSnapshot(j)
101101
prevSnapshot, line, err := getPrevSnapshot(testID, snapPath)
102102
if errors.Is(err, errSnapNotFound) {
103-
if isCI {
103+
if !shouldCreate(c.update) {
104104
handleError(t, err)
105105
return
106106
}

snaps/matchJSON_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ func TestMatchJSON(t *testing.T) {
171171
test.Equal(t, 1, testEvents.items[erred])
172172
})
173173

174+
t.Run("if snaps.Update(false) should skip creating snapshot", func(t *testing.T) {
175+
setupSnapshot(t, fileName, false)
176+
177+
mockT := test.NewMockTestingT(t)
178+
mockT.MockError = func(args ...any) {
179+
test.Equal(t, errSnapNotFound, args[0].(error))
180+
}
181+
182+
WithConfig(Update(false)).MatchJSON(mockT, "{}")
183+
184+
test.Equal(t, 1, testEvents.items[erred])
185+
})
186+
174187
t.Run("should update snapshot when 'shouldUpdate'", func(t *testing.T) {
175188
snapPath := setupSnapshot(t, jsonFilename, false, true)
176189

snaps/matchSnapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func matchSnapshot(c *Config, t testingT, values ...any) {
6363
snapshot := takeSnapshot(values)
6464
prevSnapshot, line, err := getPrevSnapshot(testID, snapPath)
6565
if errors.Is(err, errSnapNotFound) {
66-
if isCI {
66+
if !shouldCreate(c.update) {
6767
handleError(t, err)
6868
return
6969
}

snaps/matchSnapshot_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ func TestMatchSnapshot(t *testing.T) {
9292
test.Equal(t, 1, testEvents.items[erred])
9393
})
9494

95+
t.Run("if snaps.Update(false) should skip creating snapshot", func(t *testing.T) {
96+
setupSnapshot(t, fileName, false)
97+
98+
mockT := test.NewMockTestingT(t)
99+
mockT.MockError = func(args ...any) {
100+
test.Equal(t, errSnapNotFound, args[0].(error))
101+
}
102+
103+
WithConfig(Update(false)).MatchSnapshot(mockT, 10, "hello world")
104+
105+
test.Equal(t, 1, testEvents.items[erred])
106+
})
107+
95108
t.Run("should return error when diff is found", func(t *testing.T) {
96109
setupSnapshot(t, fileName, false)
97110

snaps/matchStandaloneJSON.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func matchStandaloneJSON(c *Config, t testingT, input any, matchers ...match.JSO
7070
snapshot := takeJSONSnapshot(j)
7171
prevSnapshot, err := getPrevStandaloneSnapshot(snapPath)
7272
if errors.Is(err, errSnapNotFound) {
73-
if isCI {
73+
if !shouldCreate(c.update) {
7474
handleError(t, err)
7575
return
7676
}

snaps/matchStandaloneJSON_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ func TestMatchStandaloneJSON(t *testing.T) {
173173
test.Equal(t, 1, testEvents.items[erred])
174174
})
175175

176+
t.Run("if snaps.Update(false) should skip creating snapshot", func(t *testing.T) {
177+
setupSnapshot(t, fileName, false)
178+
179+
mockT := test.NewMockTestingT(t)
180+
mockT.MockError = func(args ...any) {
181+
test.Equal(t, errSnapNotFound, args[0].(error))
182+
}
183+
184+
WithConfig(Update(false)).MatchStandaloneJSON(mockT, "{}")
185+
186+
test.Equal(t, 1, testEvents.items[erred])
187+
})
188+
176189
t.Run("should update snapshot when 'shouldUpdate'", func(t *testing.T) {
177190
snapPath := setupSnapshot(t, jsonStandaloneFilename, false, true)
178191

snaps/matchStandaloneSnapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func matchStandaloneSnapshot(c *Config, t testingT, input any) {
5050
snapshot := pretty.Sprint(input)
5151
prevSnapshot, err := getPrevStandaloneSnapshot(snapPath)
5252
if errors.Is(err, errSnapNotFound) {
53-
if isCI {
53+
if !shouldCreate(c.update) {
5454
handleError(t, err)
5555
return
5656
}

snaps/matchStandaloneSnapshot_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ func TestMatchStandaloneSnapshot(t *testing.T) {
7272
test.Equal(t, 1, testEvents.items[erred])
7373
})
7474

75+
t.Run("if snaps.Update(false) should skip creating snapshot", func(t *testing.T) {
76+
setupSnapshot(t, fileName, false)
77+
78+
mockT := test.NewMockTestingT(t)
79+
mockT.MockError = func(args ...any) {
80+
test.Equal(t, errSnapNotFound, args[0].(error))
81+
}
82+
83+
WithConfig(Update(false)).MatchStandaloneSnapshot(mockT, "hello world")
84+
85+
test.Equal(t, 1, testEvents.items[erred])
86+
})
87+
7588
t.Run("should return error when diff is found", func(t *testing.T) {
7689
setupSnapshot(t, standaloneFilename, false)
7790

snaps/matchYAML.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func matchYAML(c *Config, t testingT, input any, matchers ...match.YAMLMatcher)
9595
snapshot := takeYAMLSnapshot(y)
9696
prevSnapshot, line, err := getPrevSnapshot(testID, snapPath)
9797
if errors.Is(err, errSnapNotFound) {
98-
if isCI {
98+
if !shouldCreate(c.update) {
9999
handleError(t, err)
100100
return
101101
}

snaps/matchYAML_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ items:
172172
test.Equal(t, 1, testEvents.items[erred])
173173
})
174174

175+
t.Run("if snaps.Update(false) should skip creating snapshot", func(t *testing.T) {
176+
setupSnapshot(t, fileName, false)
177+
178+
mockT := test.NewMockTestingT(t)
179+
mockT.MockError = func(args ...any) {
180+
test.Equal(t, errSnapNotFound, args[0].(error))
181+
}
182+
183+
WithConfig(Update(false)).MatchYAML(mockT, "")
184+
185+
test.Equal(t, 1, testEvents.items[erred])
186+
})
187+
175188
t.Run("should update snapshot when 'shouldUpdate'", func(t *testing.T) {
176189
snapPath := setupSnapshot(t, yamlFilename, false, true)
177190
printerExpectedCalls := []func(received any){

snaps/utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,16 @@ func shouldUpdate(u *bool) bool {
125125

126126
return updateVAR == "true"
127127
}
128+
129+
// shouldCreate determines whether snapshots should be created
130+
func shouldCreate(u *bool) bool {
131+
if isCI {
132+
return false
133+
}
134+
135+
if u != nil {
136+
return *u
137+
}
138+
139+
return true
140+
}

0 commit comments

Comments
 (0)