1+ import { it , describe , expect , vi , beforeEach } from "vitest" ;
2+
3+ vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } ) ;
4+ vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
5+
6+ let errors , report , ERROR_BUFFER_LIMIT , showLast ;
7+
8+ beforeEach ( async ( ) => {
9+ vi . resetModules ( ) ;
10+ const module = await import ( "../src/modules/ErrorModule" ) ;
11+ errors = module . errors ;
12+ report = module . report ;
13+ ERROR_BUFFER_LIMIT = module . ERROR_BUFFER_LIMIT ;
14+ showLast = module . showLast ;
15+ } ) ;
16+
17+ describe ( "report" , ( ) => {
18+ it ( "should be able to report error" , ( ) => {
19+ report ( "hehe" )
20+ expect ( errors ) . toHaveLength ( 1 )
21+ } )
22+
23+ it ( "should not exceed error buffer limit" , ( ) => {
24+ for ( let i = 0 ; i < 100 ; i ++ ) {
25+ report ( `error ${ i } ` )
26+ }
27+ expect ( errors . length ) . lessThanOrEqual ( ERROR_BUFFER_LIMIT ) ;
28+ } )
29+ } )
30+
31+ describe ( "showLast" , ( ) => {
32+ it ( "should not log error if no error exists" , ( ) => {
33+ showLast ( ) ;
34+ expect ( console . log ) . toHaveBeenCalledWith ( expect . stringContaining ( "No errors to report" ) ) ;
35+ } )
36+
37+ it ( "should log error if error exists" , ( ) => {
38+ report ( "hehe" )
39+ showLast ( ) ;
40+ expect ( console . error ) . toHaveBeenCalledWith ( expect . stringContaining ( "hehe" ) ) ;
41+ } )
42+ } )
0 commit comments