import { expect } from 'chai' ; import { BrowserWindow } from 'electron' ; import { emittedOnce } from './events-helpers' ; async function ensureWindowIsClosed ( window ) { if ( window && ! window . isDestroyed ( ) ) { if ( window . webContents && ! window . webContents . isDestroyed ( ) ) { const isClosed = emittedOnce ( window , 'closed' ) ; window . close ( ) ; await isClosed ; } else { window . close ( ) ; } } } export const closeWindow = async ( window = null , { assertNotWindows } = { assertNotWindows : true } ) => { await ensureWindowIsClosed ( window ) ; if ( assertNotWindows ) { const windows = BrowserWindow . getAllWindows ( ) ; try { expect ( windows ) . to . have . lengthOf ( 0 ) ; } finally { for ( const win of windows ) { await ensureWindowIsClosed ( win ) ; } } } } ; export async function closeAllWindows ( ) { for ( const w of BrowserWindow . getAllWindows ( ) ) { await closeWindow ( w , { assertNotWindows : false } ) ; } }