Skip to content

Commit 69553aa

Browse files
authored
fix(store): be more explicit when checking if Angular is in test mode (#1831)
1 parent cdd7ae3 commit 69553aa

3 files changed

Lines changed: 53 additions & 95 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ $ npm install @ngxs/store@dev
1414
- Fix: Do not run `Promise.then` within synchronous tests when decorating factory [#1753](https://github.com/ngxs/store/pull/1753)
1515
- Fix: Provide `NoopNgxsExecutionStrategy` explicitly when the zone is nooped [#1819](https://github.com/ngxs/store/pull/1819)
1616
- Fix: Complete the state stream once the root view is removed [#1830](https://github.com/ngxs/store/pull/1830)
17+
- Fix: Be more explicit when checking if Angular is in test mode [#1831](https://github.com/ngxs/store/pull/1831)
1718
- Fix: Devtools Plugin - Do not connect to devtools when the plugin is disabled [#1761](https://github.com/ngxs/store/pull/1761)
1819
- Fix: Router Plugin - Cleanup subscriptions when the root view is destroyed [#1754](https://github.com/ngxs/store/pull/1754)
1920
- Fix: WebSocket Plugin - Cleanup subscriptions and close the connection when the root view is destroyed [#1755](https://github.com/ngxs/store/pull/1755)
Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
import { getPlatform, COMPILER_OPTIONS, CompilerOptions, PlatformRef } from '@angular/core';
2-
import { memoize } from './memoize';
1+
import { ɵglobal } from '@angular/core';
32

4-
/**
5-
* @description Will be provided through Terser global definitions by Angular CLI
6-
* during the production build. This is how Angular does tree-shaking internally.
7-
*/
8-
declare const ngDevMode: boolean;
9-
10-
function _isAngularInTestMode(): boolean {
11-
const platformRef: PlatformRef | null = getPlatform();
12-
if (!platformRef) return false;
13-
const compilerOptions = platformRef.injector.get(COMPILER_OPTIONS, null);
14-
if (!compilerOptions) return false;
15-
const isInTestMode = compilerOptions.some((item: CompilerOptions) => {
16-
const providers = (item && item.providers) || [];
17-
return providers.some((provider: any) => {
18-
return (
19-
(provider && provider.provide && provider.provide.name === 'MockNgModuleResolver') ||
20-
false
21-
);
22-
});
23-
});
24-
return isInTestMode;
3+
export function isAngularInTestMode(): boolean {
4+
return (
5+
typeof ɵglobal.__karma__ !== 'undefined' ||
6+
typeof ɵglobal.jasmine !== 'undefined' ||
7+
typeof ɵglobal.jest !== 'undefined' ||
8+
typeof ɵglobal.Mocha !== 'undefined'
9+
);
2510
}
26-
27-
export const isAngularInTestMode =
28-
// Caretaker note: we have still left the `typeof` condition in order to avoid
29-
// creating a breaking change for projects that still use the View Engine.
30-
typeof ngDevMode === 'undefined' || ngDevMode ? memoize(_isAngularInTestMode) : () => false;
Lines changed: 44 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,59 @@
1-
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
2-
import { getPlatform, platformCore } from '@angular/core';
3-
import { platformBrowser } from '@angular/platform-browser';
4-
import {
5-
platformBrowserDynamicTesting,
6-
BrowserDynamicTestingModule
7-
} from '@angular/platform-browser-dynamic/testing';
8-
import { getTestBed } from '@angular/core/testing';
1+
import { ɵglobal } from '@angular/core';
2+
93
import { isAngularInTestMode } from '@ngxs/store/internals';
104

115
describe('[utils/angular]', () => {
126
describe('isAngularInTestMode', () => {
13-
function resetEnv() {
14-
const fn = <any>isAngularInTestMode;
15-
// Reset the memoization
16-
fn && fn.reset && fn.reset();
17-
18-
// Reset the angular platform
19-
const p = <any>getPlatform();
20-
p && p.destroy && p.destroy();
21-
}
22-
23-
beforeEach(() => {
24-
resetEnv();
7+
// Just an empty object so `typeof !== undefined` will be truthy.
8+
const nonUndefinedValue = {};
9+
10+
it('should return true if `__karma__` is available', () => {
11+
// Arrange & act & assert
12+
const __karma__ = ɵglobal.__karma__;
13+
ɵglobal.__karma__ = nonUndefinedValue;
14+
expect(isAngularInTestMode()).toEqual(true);
15+
ɵglobal.__karma__ = __karma__;
2516
});
2617

27-
afterEach(() => {
28-
resetEnv();
29-
30-
getTestBed().resetTestEnvironment();
31-
getTestBed().initTestEnvironment(
32-
BrowserDynamicTestingModule,
33-
platformBrowserDynamicTesting()
34-
);
35-
});
36-
37-
it(`should return true if the Angular Test Module has been bootstrapped`, () => {
38-
// Arrange
39-
platformBrowserDynamicTesting();
40-
// Act
41-
const result = isAngularInTestMode();
42-
// Assert
43-
expect(result).toEqual(true);
44-
});
45-
46-
it(`should return false if Angular has not been bootstrapped`, () => {
47-
// Arrange
48-
49-
// Act
50-
const result = isAngularInTestMode();
51-
// Assert
52-
expect(result).toEqual(false);
18+
it('should return true if `jasmine` is available', () => {
19+
// Arrange & act & assert
20+
const jasmine = ɵglobal.jasmine;
21+
ɵglobal.jasmine = nonUndefinedValue;
22+
expect(isAngularInTestMode()).toEqual(true);
23+
ɵglobal.jasmine = jasmine;
5324
});
5425

55-
it(`should return false if the Angular platformBrowserDynamic has been bootstrapped`, async () => {
56-
// Arrange
57-
platformBrowserDynamic();
58-
// Act
59-
const result = isAngularInTestMode();
60-
// Assert
61-
expect(result).toEqual(false);
26+
it('should return true if `jest` is available', () => {
27+
// Arrange & act & assert
28+
const jest = ɵglobal.jest;
29+
ɵglobal.jest = nonUndefinedValue;
30+
expect(isAngularInTestMode()).toEqual(true);
31+
ɵglobal.jest = jest;
6232
});
6333

64-
it(`should return false if the Angular platformBrowser has been bootstrapped`, async () => {
65-
// Arrange
66-
platformBrowser();
67-
// Act
68-
const result = isAngularInTestMode();
69-
// Assert
70-
expect(result).toEqual(false);
34+
it('should return true if `Mocha` is available', () => {
35+
// Arrange & act & assert
36+
const Mocha = ɵglobal.Mocha;
37+
ɵglobal.Mocha = nonUndefinedValue;
38+
expect(isAngularInTestMode()).toEqual(true);
39+
ɵglobal.Mocha = Mocha;
7140
});
7241

73-
it(`should return false if the Angular platformCore has been bootstrapped`, async () => {
74-
// Arrange
75-
platformCore();
76-
// Act
77-
const result = isAngularInTestMode();
78-
// Assert
79-
expect(result).toEqual(false);
42+
it('should return false if any of the values are globally available', () => {
43+
// Arrange & act & assert
44+
const __karma__ = ɵglobal.__karma__;
45+
const jasmine = ɵglobal.jasmine;
46+
const jest = ɵglobal.jest;
47+
const Mocha = ɵglobal.Mocha;
48+
delete ɵglobal.__karma__;
49+
delete ɵglobal.jasmine;
50+
delete ɵglobal.jest;
51+
delete ɵglobal.Mocha;
52+
expect(isAngularInTestMode()).toEqual(false);
53+
ɵglobal.__karma__ = __karma__;
54+
ɵglobal.jasmine = jasmine;
55+
ɵglobal.jest = jest;
56+
ɵglobal.Mocha = Mocha;
8057
});
8158
});
8259
});

0 commit comments

Comments
 (0)