1- import chalk from 'chalk' ;
2- import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
1+ import { describe , expect , it , vi } from 'vitest' ;
32import { FileResult } from './file-system' ;
4- import { formatBytes } from './formatting' ;
53import { logMultipleResults , logPromiseResults } from './log-results' ;
64
7- const succeededCallback = ( result : PromiseFulfilledResult < FileResult > ) => {
8- const [ fileName , size ] = result . value ;
9- console . info (
10- `- ${ chalk . bold ( fileName ) } ` +
11- ( size ? ` (${ chalk . gray ( formatBytes ( size ) ) } )` : '' ) ,
12- ) ;
13- } ;
14-
15- const failedCallback = ( result : PromiseRejectedResult ) => {
16- console . warn ( `- ${ chalk . bold ( result . reason ) } ` ) ;
17- } ;
18-
195describe ( 'logMultipleResults' , ( ) => {
20- const succeededCallbackMock = vi . fn ( ) . mockImplementation ( succeededCallback ) ;
21- const failedCallbackMock = vi . fn ( ) . mockImplementation ( failedCallback ) ;
22-
23- beforeEach ( ( ) => {
24- succeededCallbackMock . mockClear ( ) ;
25- failedCallbackMock . mockClear ( ) ;
26- } ) ;
27-
28- afterEach ( ( ) => {
29- vi . restoreAllMocks ( ) ;
30- } ) ;
6+ const succeededCallbackMock = vi . fn ( ) ;
7+ const failedCallbackMock = vi . fn ( ) ;
318
329 it ( 'should call logPromiseResults with successfull plugin result' , async ( ) => {
3310 logMultipleResults (
@@ -72,8 +49,8 @@ describe('logMultipleResults', () => {
7249 failedCallbackMock ,
7350 ) ;
7451
75- expect ( succeededCallbackMock ) . toHaveBeenCalled ( ) ;
76- expect ( failedCallbackMock ) . toHaveBeenCalled ( ) ;
52+ expect ( succeededCallbackMock ) . toHaveBeenCalledOnce ( ) ;
53+ expect ( failedCallbackMock ) . toHaveBeenCalledOnce ( ) ;
7754 } ) ;
7855} ) ;
7956
@@ -86,30 +63,32 @@ describe('logPromiseResults', () => {
8663 value : [ 'out.json' ] ,
8764 } as PromiseFulfilledResult < FileResult > ,
8865 ] ,
89- 'Uploaded reports successfully: ' ,
90- succeededCallback ,
66+ 'Uploaded reports successfully:' ,
67+ result => {
68+ console . info ( result . value ) ;
69+ } ,
9170 ) ;
9271
93- expect ( console . info ) . toHaveBeenCalledWith (
94- expect . stringContaining ( 'Uploaded reports successfully: ' ) ,
95- ) ;
96- expect ( console . info ) . toHaveBeenCalledWith (
97- expect . stringContaining ( '- [1mout.json[22m' ) ,
72+ expect ( console . info ) . toHaveBeenNthCalledWith (
73+ 1 ,
74+ 'Uploaded reports successfully:' ,
9875 ) ;
76+ expect ( console . info ) . toHaveBeenNthCalledWith ( 2 , [ 'out.json' ] ) ;
9977 } ) ;
10078
10179 it ( 'should log on fail' , async ( ) => {
10280 logPromiseResults (
10381 [ { status : 'rejected' , reason : 'fail' } as PromiseRejectedResult ] ,
104- 'Generated reports failed: ' ,
105- failedCallback ,
82+ 'Generated reports failed:' ,
83+ result => {
84+ console . warn ( result . reason ) ;
85+ } ,
10686 ) ;
10787
108- expect ( console . warn ) . toHaveBeenCalledWith (
109- expect . stringContaining ( 'Generated reports failed: ' ) ,
110- ) ;
111- expect ( console . warn ) . toHaveBeenCalledWith (
112- expect . stringContaining ( '- [1mfail[22m' ) ,
88+ expect ( console . warn ) . toHaveBeenNthCalledWith (
89+ 1 ,
90+ 'Generated reports failed:' ,
11391 ) ;
92+ expect ( console . warn ) . toHaveBeenNthCalledWith ( 2 , 'fail' ) ;
11493 } ) ;
11594} ) ;
0 commit comments