import VM from 'vm' ; import * as JestUtil from 'jest-util' ; import { ModuleMocker } from 'jest-mock' ; import { LegacyFakeTimers , ModernFakeTimers } from '@jest/fake-timers' ; import { JestEnvironment , EnvironmentContext } from '@jest/environment' ; import { Window } from 'happy-dom' ; import { Script } from 'vm' ; import { Global , Config } from '@jest/types' ; /**
 * Happy DOM Jest Environment.
 */ export default class HappyDOMEnvironment implements JestEnvironment { public fakeTimers : LegacyFakeTimers < number > = null ; public fakeTimersModern : ModernFakeTimers = null ; public window = new Window ( ) ; public global : Global . Global = < Global . Global > ( < unknown > this . window ) ; public moduleMocker : ModuleMocker = new ModuleMocker ( < typeof globalThis > ( < unknown > this . window ) ) ; param config param options constructor ( config , options ? ) { VM . createContext ( this . global ) ; VM . runInContext ( 'window.Function = (() => {}).constructor;' , this . global ) ; this . global . Error . stackTraceLimit = 100 ; this . global . Buffer = Buffer ; this . window . global = this . global ; JestUtil . installCommonGlobals ( < typeof globalThis > ( < unknown > this . window ) , config . globals ) ; if ( options && options . console ) { this . global . console = options . console ; this . global . window [ 'console' ] = options . console ; } this . fakeTimers = new LegacyFakeTimers ( { config , global : < typeof globalThis > ( < unknown > this . window ) , moduleMocker : this . moduleMocker , timerConfig : { idToRef : ( id ) => id , refToId : ( ref ) => ref } } ) ; this . fakeTimersModern = new ModernFakeTimers ( { config , global : < typeof globalThis > ( < unknown > this . window ) } ) ; } returns public async setup ( ) { } returns public async teardown ( ) { this . fakeTimers . dispose ( ) ; this . fakeTimersModern . dispose ( ) ; this . global . happyDOM [ 'cancelAsync' ] ( ) ; this . global = null ; this . moduleMocker = null ; this . fakeTimers = null ; this . fakeTimersModern = null ; } param script returns public runScript ( script ) { return script . runInContext ( this . global ) ; } returns public getVmContext ( ) { return this . global ; } }