1- import { describe , expect , test , mock } from 'bun:test' ;
1+ import { describe , expect , test , mock , spyOn } from 'bun:test' ;
22
33// In Bun, top-level imports are cached.
44// We can use mock.module to change the implementation of a module,
@@ -11,13 +11,16 @@ const importFreshCore = (cacheKey: string) => import(`../core?${cacheKey}`);
1111
1212describe ( 'core info parsing' , ( ) => {
1313 test ( 'should call error when currentVersionInfo is invalid JSON' , async ( ) => {
14- const mockError = mock ( ( ) => { } ) ;
14+ const consoleError = spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
1515
1616 mock . module ( 'react-native' , ( ) => ( {
1717 Platform : {
1818 OS : 'ios' ,
1919 Version : 13 ,
2020 } ,
21+ DeviceEventEmitter : {
22+ addListener : mock ( ( ) => ( { remove : mock ( ( ) => { } ) } ) ) ,
23+ } ,
2124 NativeModules : {
2225 Pushy : {
2326 currentVersionInfo : '{invalid}' ,
@@ -46,30 +49,30 @@ describe('core info parsing', () => {
4649 nanoid : ( ) => 'mock-uuid' ,
4750 } ) ) ;
4851
49- mock . module ( '../utils' , ( ) => ( {
50- error : mockError ,
51- log : mock ( ( ) => { } ) ,
52- emptyModule : { } ,
53- } ) ) ;
54-
5552 // Use a unique query parameter to bypass cache if supported, or just rely on fresh environment per file.
5653 // In Bun, you can sometimes use a cache buster if it's dynamic import.
5754 await importFreshCore ( 'error' ) ;
5855
59- expect ( mockError ) . toHaveBeenCalledWith (
56+ expect ( consoleError ) . toHaveBeenCalledWith (
57+ expect . any ( String ) ,
6058 expect . stringContaining ( 'error_parse_version_info' )
6159 ) ;
60+
61+ consoleError . mockRestore ( ) ;
6262 } ) ;
6363
6464 test ( 'should not call error when currentVersionInfo is valid JSON' , async ( ) => {
65- const mockError = mock ( ( ) => { } ) ;
65+ const consoleError = spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
6666 const mockSetLocalHashInfo = mock ( ( ) => { } ) ;
6767
6868 mock . module ( 'react-native' , ( ) => ( {
6969 Platform : {
7070 OS : 'ios' ,
7171 Version : 13 ,
7272 } ,
73+ DeviceEventEmitter : {
74+ addListener : mock ( ( ) => ( { remove : mock ( ( ) => { } ) } ) ) ,
75+ } ,
7376 NativeModules : {
7477 Pushy : {
7578 currentVersionInfo : JSON . stringify ( { name : 'v1' , debugChannel : true } ) ,
@@ -90,14 +93,10 @@ describe('core info parsing', () => {
9093 } ,
9194 } ) ) ;
9295
93- mock . module ( '../utils' , ( ) => ( {
94- error : mockError ,
95- log : mock ( ( ) => { } ) ,
96- emptyModule : { } ,
97- } ) ) ;
98-
9996 await importFreshCore ( 'success' ) ;
10097
101- expect ( mockError ) . not . toHaveBeenCalled ( ) ;
98+ expect ( consoleError ) . not . toHaveBeenCalled ( ) ;
99+
100+ consoleError . mockRestore ( ) ;
102101 } ) ;
103102} ) ;
0 commit comments