Skip to content

Commit 1758d34

Browse files
evgenyfedorenkobrandonroberts
authored andcommitted
feat(store): enable immutability checks by default (#2266)
Closes #2217 BREAKING CHANGE: Immutability checks are enabled by default. BEFORE: Immutability checks are opt-in. AFTER: If state or action is mutated then there will be a run time exception thrown.
1 parent 7ecaa22 commit 1758d34

File tree

4 files changed

+30
-65
lines changed

4 files changed

+30
-65
lines changed

modules/store/schematics/ng-add/index.spec.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,4 @@ describe('Store ng-add Schematic', () => {
130130

131131
expect(content).toMatch(/export interface AppState {/);
132132
});
133-
134-
it('should add runtime checks by default', () => {
135-
const options = { ...defaultOptions, module: 'app.module.ts' };
136-
137-
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
138-
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
139-
140-
expect(content).toMatch(/runtimeChecks: {/);
141-
expect(content).toMatch(/strictStateImmutability: true,/);
142-
expect(content).toMatch(/strictActionImmutability: true/);
143-
});
144133
});

modules/store/schematics/ng-add/index.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
branchAndMerge,
99
chain,
1010
mergeWith,
11-
template,
1211
url,
1312
noop,
1413
move,
@@ -59,18 +58,9 @@ function addImportToNgModule(options: RootStoreOptions): Rule {
5958
const storeModuleReducers = options.minimal ? `{}` : `reducers`;
6059

6160
const storeModuleConfig = options.minimal
62-
? `{
63-
runtimeChecks: {
64-
strictStateImmutability: true,
65-
strictActionImmutability: true
66-
}
67-
}`
61+
? `{}`
6862
: `{
69-
metaReducers,
70-
runtimeChecks: {
71-
strictStateImmutability: true,
72-
strictActionImmutability: true
73-
}
63+
metaReducers
7464
}`;
7565
const storeModuleSetup = `StoreModule.forRoot(${storeModuleReducers}, ${storeModuleConfig})`;
7666

modules/store/spec/runtime_checks.spec.ts

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,32 @@ import * as metaReducers from '../src/meta-reducers';
77

88
describe('Runtime checks:', () => {
99
describe('createActiveRuntimeChecks:', () => {
10-
it('should disable all checks by default', () => {
10+
it('should enable immutability checks by default', () => {
1111
expect(createActiveRuntimeChecks()).toEqual({
1212
strictStateSerializability: false,
1313
strictActionSerializability: false,
14-
strictActionImmutability: false,
15-
strictStateImmutability: false,
14+
strictActionImmutability: true,
15+
strictStateImmutability: true,
1616
});
1717
});
1818

19-
it('should log a warning in dev mode when no configuration is provided', () => {
20-
const spy = spyOn(console, 'warn');
21-
22-
createActiveRuntimeChecks();
23-
24-
expect(spy).toHaveBeenCalled();
25-
});
26-
27-
it('should not log a warning in dev mode when configuration is provided', () => {
28-
const spy = spyOn(console, 'warn');
29-
30-
createActiveRuntimeChecks({});
31-
32-
expect(spy).not.toHaveBeenCalled();
33-
});
34-
35-
it('should not log a warning when not dev mode when no configuration is provided', () => {
36-
spyOn(ngCore, 'isDevMode').and.returnValue(false);
37-
const spy = spyOn(console, 'warn');
38-
39-
createActiveRuntimeChecks();
40-
41-
expect(spy).not.toHaveBeenCalled();
42-
});
43-
4419
it('should allow the user to override the config', () => {
4520
expect(
4621
createActiveRuntimeChecks({
4722
strictStateSerializability: true,
4823
strictActionSerializability: true,
49-
strictActionImmutability: true,
50-
strictStateImmutability: true,
24+
strictActionImmutability: false,
25+
strictStateImmutability: false,
5126
})
5227
).toEqual({
5328
strictStateSerializability: true,
5429
strictActionSerializability: true,
55-
strictActionImmutability: true,
56-
strictStateImmutability: true,
30+
strictActionImmutability: false,
31+
strictStateImmutability: false,
5732
});
5833
});
5934

60-
it('should disable runtime checks in production', () => {
35+
it('should disable runtime checks in production by default', () => {
6136
spyOn(ngCore, 'isDevMode').and.returnValue(false);
6237

6338
expect(createActiveRuntimeChecks()).toEqual({
@@ -67,6 +42,22 @@ describe('Runtime checks:', () => {
6742
strictStateImmutability: false,
6843
});
6944
});
45+
46+
it('should disable runtime checks in production even if opted in to enable', () => {
47+
spyOn(ngCore, 'isDevMode').and.returnValue(false);
48+
49+
expect(
50+
createActiveRuntimeChecks({
51+
strictStateSerializability: true,
52+
strictActionSerializability: true,
53+
})
54+
).toEqual({
55+
strictStateSerializability: false,
56+
strictActionSerializability: false,
57+
strictActionImmutability: false,
58+
strictStateImmutability: false,
59+
});
60+
});
7061
});
7162

7263
describe('USER_RUNTIME_CHECKS Token', () => {
@@ -114,7 +105,7 @@ describe('Runtime checks:', () => {
114105
expect(serializationCheckMetaReducerSpy).not.toHaveBeenCalled();
115106
});
116107

117-
it('should not create a meta reducer without config', () => {
108+
it('should create immutability meta reducer without config', () => {
118109
const serializationCheckMetaReducerSpy = spyOn(
119110
metaReducers,
120111
'serializationCheckMetaReducer'
@@ -136,7 +127,7 @@ describe('Runtime checks:', () => {
136127

137128
const _store = TestBed.get<Store<any>>(Store);
138129
expect(serializationCheckMetaReducerSpy).not.toHaveBeenCalled();
139-
expect(immutabilityCheckMetaReducerSpy).not.toHaveBeenCalled();
130+
expect(immutabilityCheckMetaReducerSpy).toHaveBeenCalled();
140131
});
141132
});
142133

modules/store/src/runtime_checks.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,11 @@ export function createActiveRuntimeChecks(
1515
runtimeChecks?: Partial<RuntimeChecks>
1616
): RuntimeChecks {
1717
if (isDevMode()) {
18-
if (runtimeChecks === undefined) {
19-
console.warn(
20-
'@ngrx/store: runtime checks are currently opt-in but will be the default in the next major version with the possibility to opt-out, see https://ngrx.io/guide/migration/v8 for more information.'
21-
);
22-
}
2318
return {
2419
strictStateSerializability: false,
2520
strictActionSerializability: false,
26-
strictStateImmutability: false,
27-
strictActionImmutability: false,
21+
strictStateImmutability: true,
22+
strictActionImmutability: true,
2823
...runtimeChecks,
2924
};
3025
}

0 commit comments

Comments
 (0)