@@ -4,14 +4,15 @@ import { join } from 'path';
4
4
import process from 'process' ;
5
5
import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
6
6
import { MEMFS_VOLUME } from '@code-pushup/models/testing' ;
7
- import { mockConsole , unmockConsole } from '../../test/console.mock' ;
8
7
import {
8
+ FileResult ,
9
9
NoExportError ,
10
10
ensureDirectoryExists ,
11
11
importEsmModule ,
12
12
logMultipleFileResults ,
13
13
toUnixPath ,
14
14
} from './file-system' ;
15
+ import * as logResults from './log-results' ;
15
16
16
17
// Mock file system API's
17
18
vi . mock ( 'fs' , async ( ) => {
@@ -63,70 +64,32 @@ describe('ensureDirectoryExists', () => {
63
64
} ) ;
64
65
65
66
describe ( 'logMultipleFileResults' , ( ) => {
66
- let logs : string [ ] ;
67
- const setupConsole = async ( ) => {
68
- logs = [ ] ;
69
- mockConsole ( msg => logs . push ( msg ) ) ;
70
- } ;
71
- const teardownConsole = async ( ) => {
72
- logs = [ ] ;
73
- unmockConsole ( ) ;
74
- } ;
75
-
76
- beforeEach ( async ( ) => {
77
- logs = [ ] ;
78
- setupConsole ( ) ;
79
- } ) ;
80
-
81
67
afterEach ( ( ) => {
82
- teardownConsole ( ) ;
68
+ vi . restoreAllMocks ( ) ;
83
69
} ) ;
84
70
85
- it ( 'should log reports correctly`' , async ( ) => {
86
- logMultipleFileResults (
87
- [ { status : 'fulfilled' , value : [ 'out.json' ] } ] ,
88
- 'Uploaded reports' ,
71
+ it ( 'should call logMultipleResults with the correct arguments' , ( ) => {
72
+ const logMultipleResultsSpy = vi . spyOn (
73
+ logResults ,
74
+ 'logMultipleResults' as never ,
89
75
) ;
90
- expect ( logs ) . toHaveLength ( 2 ) ;
91
- expect ( logs [ 0 ] ) . toContain ( 'Uploaded reports successfully: ' ) ;
92
- expect ( logs [ 1 ] ) . toContain ( '- [1mout.json[22m' ) ;
93
- } ) ;
94
-
95
- it ( 'should log report sizes correctly`' , async ( ) => {
96
- logMultipleFileResults (
97
- [ { status : 'fulfilled' , value : [ 'out.json' , 10000 ] } ] ,
98
- 'Generated reports' ,
76
+ const persistResult = [
77
+ {
78
+ status : 'fulfilled' ,
79
+ value : [ 'out.json' , 10000 ] ,
80
+ } as PromiseFulfilledResult < FileResult > ,
81
+ ] ;
82
+ const messagePrefix = 'Generated reports' ;
83
+
84
+ logMultipleFileResults ( persistResult , messagePrefix ) ;
85
+
86
+ expect ( logMultipleResultsSpy ) . toHaveBeenCalled ( ) ;
87
+ expect ( logMultipleResultsSpy ) . toHaveBeenCalledWith (
88
+ persistResult ,
89
+ messagePrefix ,
90
+ expect . any ( Function ) ,
91
+ expect . any ( Function ) ,
99
92
) ;
100
- expect ( logs ) . toHaveLength ( 2 ) ;
101
- expect ( logs [ 0 ] ) . toContain ( 'Generated reports successfully: ' ) ;
102
- expect ( logs [ 1 ] ) . toContain ( '- [1mout.json[22m ([90m9.77 kB[39m)' ) ;
103
- } ) ;
104
-
105
- it ( 'should log fails correctly`' , async ( ) => {
106
- logMultipleFileResults (
107
- [ { status : 'rejected' , reason : 'fail' } ] ,
108
- 'Generated reports' ,
109
- ) ;
110
- expect ( logs ) . toHaveLength ( 2 ) ;
111
-
112
- expect ( logs ) . toContain ( 'Generated reports failed: ' ) ;
113
- expect ( logs ) . toContain ( '- [1mfail[22m' ) ;
114
- } ) ;
115
-
116
- it ( 'should log report sizes and fails correctly`' , async ( ) => {
117
- logMultipleFileResults (
118
- [
119
- { status : 'fulfilled' , value : [ 'out.json' , 10000 ] } ,
120
- { status : 'rejected' , reason : 'fail' } ,
121
- ] ,
122
- 'Generated reports' ,
123
- ) ;
124
- expect ( logs ) . toHaveLength ( 4 ) ;
125
- expect ( logs ) . toContain ( 'Generated reports successfully: ' ) ;
126
- expect ( logs ) . toContain ( '- [1mout.json[22m ([90m9.77 kB[39m)' ) ;
127
-
128
- expect ( logs ) . toContain ( 'Generated reports failed: ' ) ;
129
- expect ( logs ) . toContain ( '- [1mfail[22m' ) ;
130
93
} ) ;
131
94
} ) ;
132
95
0 commit comments