From 3f26e99c8686417ebb29208fc8a2333593f9f07e Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Thu, 10 Jun 2021 14:46:38 +0200 Subject: [PATCH] fix(cdk/overlay): expand test environment check (#22927) We have some logic in the overlay container that tries to prevent overlays from leaking between tests. The logic is currently limited to Jasmine tests and it happened to work by accident for Jest. Jest has made some changes that will break our check so these changes rework the logic to detect Jest and Mocha correctly. Fixes #22926. --- src/cdk/overlay/overlay-container.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/cdk/overlay/overlay-container.ts b/src/cdk/overlay/overlay-container.ts index e14ea1958408..f4ff864a48a3 100644 --- a/src/cdk/overlay/overlay-container.ts +++ b/src/cdk/overlay/overlay-container.ts @@ -10,12 +10,20 @@ import {DOCUMENT} from '@angular/common'; import {Inject, Injectable, OnDestroy} from '@angular/core'; import {Platform} from '@angular/cdk/platform'; +declare const __karma__: unknown; +declare const jasmine: unknown; +declare const jest: unknown; +declare const Mocha: unknown; + /** * Whether we're in a testing environment. - * TODO(crisbeto): remove this once we have an overlay testing module. + * TODO(crisbeto): remove this once we have an overlay testing module or Angular starts tearing + * down the testing `NgModule` (see https://github.com/angular/angular/issues/18831). */ -const isTestEnvironment: boolean = typeof window !== 'undefined' && !!window && - !!((window as any).__karma__ || (window as any).jasmine); +const isTestEnvironment = (typeof __karma__ !== 'undefined' && !!__karma__) || + (typeof jasmine !== 'undefined' && !!jasmine) || + (typeof jest !== 'undefined' && !!jest) || + (typeof Mocha !== 'undefined' && !!Mocha); /** Container inside which all overlays will render. */ @Injectable({providedIn: 'root'})