@@ -33,6 +33,19 @@ test('snapshot files are independent of test resolution order', async t => {
33
33
t . deepEqual ( snapshot , snapshotReversed ) ;
34
34
} ) ;
35
35
36
+ function getSnapshotIds ( report ) {
37
+ function * matchAll ( string , regexp ) {
38
+ let match ;
39
+ while ( ( match = regexp . exec ( string ) ) !== null ) {
40
+ yield match ;
41
+ }
42
+ }
43
+
44
+ const ids = [ ...matchAll ( report , / ' i n d e x : ( [ - . \d ] + ) ' / g) ] . map ( match => Number ( match [ 1 ] ) ) ;
45
+
46
+ return ids ;
47
+ }
48
+
36
49
test ( 'snapshot reports are sorted in declaration order' , async t => {
37
50
const options = {
38
51
cwd : exec . cwd ( 'report-declaration-order' ) ,
@@ -44,16 +57,115 @@ test('snapshot reports are sorted in declaration order', async t => {
44
57
await exec . fixture ( [ '--update-snapshots' ] , options ) ;
45
58
46
59
const report = fs . readFileSync ( path . join ( options . cwd , 'test.js.md' ) , { encoding : 'utf8' } ) ;
60
+ const ids = getSnapshotIds ( report ) ;
47
61
48
- function * matchAll ( string , regexp ) {
49
- let match ;
50
- while ( ( match = regexp . exec ( string ) ) !== null ) {
51
- yield match ;
62
+ t . deepEqual ( ids , [ ...ids ] . sort ( ( a , b ) => a - b ) ) ;
63
+ } ) ;
64
+
65
+ const unsortedSnapshotPath = path . join ( __dirname , 'fixtures' , 'backwards-compatibility' , 'unsorted' , 'test.js.snap' ) ;
66
+ const unsortedReportPath = path . join ( __dirname , 'fixtures' , 'backwards-compatibility' , 'unsorted' , 'test.js.md' ) ;
67
+
68
+ const unsortedSnapshot = fs . readFileSync ( unsortedSnapshotPath ) ;
69
+ const unsortedReport = fs . readFileSync ( unsortedReportPath ) ;
70
+
71
+ test ( 'unsorted snapshots are unchanged when checking' , async t => {
72
+ const options = {
73
+ cwd : exec . cwd ( 'backwards-compatibility' , 'check-only' ) ,
74
+ env : {
75
+ AVA_FORCE_CI : 'not-ci'
52
76
}
53
- }
77
+ } ;
54
78
55
- const ids = [ ...matchAll ( report , / ' i n d e x : ( [ - . \d ] + ) ' / g) ] . map ( match => Number ( match [ 1 ] ) ) ;
56
- const sortedIds = [ ...ids ] . sort ( ( a , b ) => a - b ) ;
79
+ const snapshotPath = path . join ( options . cwd , 'test.js.snap' ) ;
80
+ const reportPath = path . join ( options . cwd , 'test.js.md' ) ;
81
+
82
+ // Install a known-unsorted snapshot, report
83
+ fs . copyFileSync ( unsortedSnapshotPath , snapshotPath ) ;
84
+ fs . copyFileSync ( unsortedReportPath , reportPath ) ;
85
+
86
+ // Run test
87
+ await exec . fixture ( [ ] , options ) ;
88
+
89
+ // Assert snapshot, report are unchanged
90
+ const snapshot = fs . readFileSync ( snapshotPath ) ;
91
+ const report = fs . readFileSync ( reportPath ) ;
92
+
93
+ t . deepEqual ( snapshot , unsortedSnapshot ) ;
94
+ t . deepEqual ( report , unsortedReport ) ;
95
+
96
+ // Clean up the snapshot, report
97
+ fs . unlinkSync ( snapshotPath ) ;
98
+ fs . unlinkSync ( reportPath ) ;
99
+ } ) ;
100
+
101
+ test ( 'unsorted snapshots are changed when appending' , async t => {
102
+ const options = {
103
+ cwd : exec . cwd ( 'backwards-compatibility' , 'append-only' ) ,
104
+ env : {
105
+ AVA_FORCE_CI : 'not-ci'
106
+ }
107
+ } ;
108
+
109
+ const snapshotPath = path . join ( options . cwd , 'test.js.snap' ) ;
110
+ const reportPath = path . join ( options . cwd , 'test.js.md' ) ;
111
+
112
+ // Install a known-unsorted snapshot, report
113
+ fs . copyFileSync ( unsortedSnapshotPath , snapshotPath ) ;
114
+ fs . copyFileSync ( unsortedReportPath , reportPath ) ;
115
+
116
+ // Run test
117
+ await exec . fixture ( [ ] , options ) ;
118
+
119
+ // Assert snapshot, report are changed
120
+ const snapshot = fs . readFileSync ( snapshotPath ) ;
121
+ const report = fs . readFileSync ( reportPath ) ;
122
+
123
+ t . notDeepEqual ( snapshot , unsortedSnapshot ) ;
124
+ t . notDeepEqual ( report , unsortedReport ) ;
125
+
126
+ // Assert entries appended to report are sorted
127
+ const textReport = report . toString ( ) ;
128
+ const ids = getSnapshotIds ( textReport ) ;
129
+
130
+ t . deepEqual ( ids , [ 2 , 1 , 3 , 4 ] ) ;
131
+
132
+ // Clean up the snapshot, report
133
+ fs . unlinkSync ( snapshotPath ) ;
134
+ fs . unlinkSync ( reportPath ) ;
135
+ } ) ;
136
+
137
+ test ( 'unsorted snapshots are changed when updating' , async t => {
138
+ const options = {
139
+ cwd : exec . cwd ( 'backwards-compatibility' , 'updating' ) ,
140
+ env : {
141
+ AVA_FORCE_CI : 'not-ci'
142
+ }
143
+ } ;
144
+
145
+ const snapshotPath = path . join ( options . cwd , 'test.js.snap' ) ;
146
+ const reportPath = path . join ( options . cwd , 'test.js.md' ) ;
147
+
148
+ // Install a known-unsorted snapshot, report
149
+ fs . copyFileSync ( unsortedSnapshotPath , snapshotPath ) ;
150
+ fs . copyFileSync ( unsortedReportPath , reportPath ) ;
151
+
152
+ // Run test
153
+ await exec . fixture ( [ '--update-snapshots' ] , options ) ;
154
+
155
+ // Assert snapshot, report are changed
156
+ const snapshot = fs . readFileSync ( snapshotPath ) ;
157
+ const report = fs . readFileSync ( reportPath ) ;
158
+
159
+ t . notDeepEqual ( snapshot , unsortedSnapshot ) ;
160
+ t . notDeepEqual ( report , unsortedReport ) ;
161
+
162
+ // Assert report is sorted
163
+ const textReport = report . toString ( ) ;
164
+ const ids = getSnapshotIds ( textReport ) ;
165
+
166
+ t . deepEqual ( ids , [ 1 , 2 ] ) ;
57
167
58
- t . deepEqual ( ids , sortedIds ) ;
168
+ // Clean up the snapshot, report
169
+ fs . unlinkSync ( snapshotPath ) ;
170
+ fs . unlinkSync ( reportPath ) ;
59
171
} ) ;
0 commit comments