@@ -6,9 +6,11 @@ import { vi } from "vitest";
66import { EventManager } from "@contentstack/advanced-post-message" ;
77import { LIVE_PREVIEW_CHANNEL_ID } from "../livePreviewEventManager.constant" ;
88
9- // Mock dependencies
9+ // Mock dependencies — must be constructible (`new EventManager()`) (Vitest 4)
1010vi . mock ( "@contentstack/advanced-post-message" , ( ) => ( {
11- EventManager : vi . fn ( ) ,
11+ EventManager : vi . fn ( function ( this : unknown ) {
12+ return { on : vi . fn ( ) , send : vi . fn ( ) } ;
13+ } ) ,
1214} ) ) ;
1315
1416vi . mock ( "../../../common/inIframe" , ( ) => ( {
@@ -31,7 +33,9 @@ describe("livePreviewEventManager", () => {
3133 on : vi . fn ( ) ,
3234 send : vi . fn ( ) ,
3335 } ;
34- ( EventManager as any ) . mockImplementation ( ( ) => mockEventManager ) ;
36+ ( EventManager as any ) . mockImplementation ( function ( ) {
37+ return mockEventManager ;
38+ } ) ;
3539
3640 // Store original window
3741 originalWindow = global . window ;
@@ -209,14 +213,21 @@ describe("livePreviewEventManager", () => {
209213 } ) ;
210214
211215 it ( "should handle when EventManager constructor throws" , async ( ) => {
212- ( EventManager as any ) . mockImplementation ( ( ) => {
216+ ( EventManager as any ) . mockImplementation ( function ( ) {
213217 throw new Error ( "EventManager constructor error" ) ;
214218 } ) ;
215219
216- // Should not crash the module initialization
217- expect ( async ( ) => {
218- await import ( "../livePreviewEventManager" ) ;
219- } ) . not . toThrow ( ) ;
220+ // `new EventManager` runs while the module loads, so a throwing constructor
221+ // fails module evaluation and `import()` gets a *rejected* promise.
222+ //
223+ // We use `rejects.toThrow` (and await it), not `expect(() => import).not.toThrow()`:
224+ // the latter only catches synchronous throws; async failures become promise
225+ // rejections, which the old pattern missed and could surface as unhandled
226+ // rejections (e.g. under Vitest 4). This matches actual runtime behavior, not
227+ // a reversed expectation: the module does not catch the error internally.
228+ await expect (
229+ import ( "../livePreviewEventManager" )
230+ ) . rejects . toThrow ( "EventManager constructor error" ) ;
220231 } ) ;
221232 } ) ;
222233 } ) ;
0 commit comments