Skip to content

Commit

Permalink
feat(store): enable immutability checks by default (#2266)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
evgenyfedorenko authored and brandonroberts committed Jan 7, 2020
1 parent 7ecaa22 commit 1758d34
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 65 deletions.
11 changes: 0 additions & 11 deletions modules/store/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,4 @@ describe('Store ng-add Schematic', () => {

expect(content).toMatch(/export interface AppState {/);
});

it('should add runtime checks by default', () => {
const options = { ...defaultOptions, module: 'app.module.ts' };

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);

expect(content).toMatch(/runtimeChecks: {/);
expect(content).toMatch(/strictStateImmutability: true,/);
expect(content).toMatch(/strictActionImmutability: true/);
});
});
14 changes: 2 additions & 12 deletions modules/store/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
branchAndMerge,
chain,
mergeWith,
template,
url,
noop,
move,
Expand Down Expand Up @@ -59,18 +58,9 @@ function addImportToNgModule(options: RootStoreOptions): Rule {
const storeModuleReducers = options.minimal ? `{}` : `reducers`;

const storeModuleConfig = options.minimal
? `{
runtimeChecks: {
strictStateImmutability: true,
strictActionImmutability: true
}
}`
? `{}`
: `{
metaReducers,
runtimeChecks: {
strictStateImmutability: true,
strictActionImmutability: true
}
metaReducers
}`;
const storeModuleSetup = `StoreModule.forRoot(${storeModuleReducers}, ${storeModuleConfig})`;

Expand Down
61 changes: 26 additions & 35 deletions modules/store/spec/runtime_checks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,32 @@ import * as metaReducers from '../src/meta-reducers';

describe('Runtime checks:', () => {
describe('createActiveRuntimeChecks:', () => {
it('should disable all checks by default', () => {
it('should enable immutability checks by default', () => {
expect(createActiveRuntimeChecks()).toEqual({
strictStateSerializability: false,
strictActionSerializability: false,
strictActionImmutability: false,
strictStateImmutability: false,
strictActionImmutability: true,
strictStateImmutability: true,
});
});

it('should log a warning in dev mode when no configuration is provided', () => {
const spy = spyOn(console, 'warn');

createActiveRuntimeChecks();

expect(spy).toHaveBeenCalled();
});

it('should not log a warning in dev mode when configuration is provided', () => {
const spy = spyOn(console, 'warn');

createActiveRuntimeChecks({});

expect(spy).not.toHaveBeenCalled();
});

it('should not log a warning when not dev mode when no configuration is provided', () => {
spyOn(ngCore, 'isDevMode').and.returnValue(false);
const spy = spyOn(console, 'warn');

createActiveRuntimeChecks();

expect(spy).not.toHaveBeenCalled();
});

it('should allow the user to override the config', () => {
expect(
createActiveRuntimeChecks({
strictStateSerializability: true,
strictActionSerializability: true,
strictActionImmutability: true,
strictStateImmutability: true,
strictActionImmutability: false,
strictStateImmutability: false,
})
).toEqual({
strictStateSerializability: true,
strictActionSerializability: true,
strictActionImmutability: true,
strictStateImmutability: true,
strictActionImmutability: false,
strictStateImmutability: false,
});
});

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

expect(createActiveRuntimeChecks()).toEqual({
Expand All @@ -67,6 +42,22 @@ describe('Runtime checks:', () => {
strictStateImmutability: false,
});
});

it('should disable runtime checks in production even if opted in to enable', () => {
spyOn(ngCore, 'isDevMode').and.returnValue(false);

expect(
createActiveRuntimeChecks({
strictStateSerializability: true,
strictActionSerializability: true,
})
).toEqual({
strictStateSerializability: false,
strictActionSerializability: false,
strictActionImmutability: false,
strictStateImmutability: false,
});
});
});

describe('USER_RUNTIME_CHECKS Token', () => {
Expand Down Expand Up @@ -114,7 +105,7 @@ describe('Runtime checks:', () => {
expect(serializationCheckMetaReducerSpy).not.toHaveBeenCalled();
});

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

const _store = TestBed.get<Store<any>>(Store);
expect(serializationCheckMetaReducerSpy).not.toHaveBeenCalled();
expect(immutabilityCheckMetaReducerSpy).not.toHaveBeenCalled();
expect(immutabilityCheckMetaReducerSpy).toHaveBeenCalled();
});
});

Expand Down
9 changes: 2 additions & 7 deletions modules/store/src/runtime_checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ export function createActiveRuntimeChecks(
runtimeChecks?: Partial<RuntimeChecks>
): RuntimeChecks {
if (isDevMode()) {
if (runtimeChecks === undefined) {
console.warn(
'@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.'
);
}
return {
strictStateSerializability: false,
strictActionSerializability: false,
strictStateImmutability: false,
strictActionImmutability: false,
strictStateImmutability: true,
strictActionImmutability: true,
...runtimeChecks,
};
}
Expand Down

0 comments on commit 1758d34

Please sign in to comment.