1
+ import { afterEach , beforeEach , describe , expect , it } from 'vitest' ;
2
+ import { logPersistedResults , persistReport } from './persist' ;
3
+ import { readFileSync , unlinkSync } from 'fs' ;
1
4
import { Report } from '@quality-metrics/models' ;
2
5
import {
3
- MEMFS_VOLUME ,
4
6
dummyConfig ,
5
7
dummyReport ,
8
+ MEMFS_VOLUME ,
6
9
mockPersistConfig ,
7
10
} from '@quality-metrics/models/testing' ;
8
- import { readFileSync , unlinkSync } from 'fs' ;
9
11
import { vol } from 'memfs' ;
10
12
import { join } from 'path' ;
11
- import { afterEach , beforeEach , describe , expect , it } from 'vitest' ;
12
13
import { mockConsole , unmockConsole } from './mock/helper.mock' ;
13
- import { persistReport } from './persist' ;
14
14
15
15
vi . mock ( 'fs' , async ( ) => {
16
16
const memfs : typeof import ( 'memfs' ) = await vi . importActual ( 'memfs' ) ;
@@ -35,10 +35,9 @@ const readReport = (format: 'json' | 'md') => {
35
35
} ;
36
36
37
37
const config = dummyConfig ( MEMFS_VOLUME ) ;
38
+ let logs : string [ ] = [ ] ;
38
39
39
40
describe ( 'persistReport' , ( ) => {
40
- let logs : string [ ] = [ ] ;
41
-
42
41
beforeEach ( async ( ) => {
43
42
vol . reset ( ) ;
44
43
vol . fromJSON (
@@ -143,3 +142,54 @@ describe('persistReport', () => {
143
142
// TODO: should throw PersistDirError
144
143
// TODO: should throw PersistError
145
144
} ) ;
145
+
146
+ describe ( 'logPersistedResults' , ( ) => {
147
+ beforeEach ( async ( ) => {
148
+ vol . reset ( ) ;
149
+ vol . fromJSON (
150
+ {
151
+ [ reportPath ( 'json' ) ] : '' ,
152
+ [ reportPath ( 'md' ) ] : '' ,
153
+ } ,
154
+ MEMFS_VOLUME ,
155
+ ) ;
156
+ unlinkSync ( reportPath ( 'json' ) ) ;
157
+ unlinkSync ( reportPath ( 'md' ) ) ;
158
+
159
+ logs = [ ] ;
160
+ mockConsole ( msg => logs . push ( msg ) ) ;
161
+ } ) ;
162
+
163
+ afterEach ( ( ) => {
164
+ logs = [ ] ;
165
+ unmockConsole ( ) ;
166
+ } ) ;
167
+
168
+ it ( 'should log report sizes correctly`' , async ( ) => {
169
+ logPersistedResults ( [ { status : 'fulfilled' , value : [ 'out.json' , 10000 ] } ] ) ;
170
+ expect ( logs . length ) . toBe ( 2 ) ;
171
+ expect ( logs ) . toContain ( 'Generated reports successfully: ' ) ;
172
+ expect ( logs ) . toContain ( '- [1mout.json[22m ([90m9.77 kB[39m)' ) ;
173
+ } ) ;
174
+
175
+ it ( 'should log fails correctly`' , async ( ) => {
176
+ logPersistedResults ( [ { status : 'rejected' , reason : 'fail' } ] ) ;
177
+ expect ( logs . length ) . toBe ( 2 ) ;
178
+
179
+ expect ( logs ) . toContain ( 'Generated reports failed: ' ) ;
180
+ expect ( logs ) . toContain ( '- [1mfail[22m' ) ;
181
+ } ) ;
182
+
183
+ it ( 'should log report sizes and fails correctly`' , async ( ) => {
184
+ logPersistedResults ( [
185
+ { status : 'fulfilled' , value : [ 'out.json' , 10000 ] } ,
186
+ { status : 'rejected' , reason : 'fail' } ,
187
+ ] ) ;
188
+ expect ( logs . length ) . toBe ( 4 ) ;
189
+ expect ( logs ) . toContain ( 'Generated reports successfully: ' ) ;
190
+ expect ( logs ) . toContain ( '- [1mout.json[22m ([90m9.77 kB[39m)' ) ;
191
+
192
+ expect ( logs ) . toContain ( 'Generated reports failed: ' ) ;
193
+ expect ( logs ) . toContain ( '- [1mfail[22m' ) ;
194
+ } ) ;
195
+ } ) ;
0 commit comments