Skip to content

Commit

Permalink
fix(core): correct resets on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jan 16, 2022
1 parent 43403ca commit e3b1809
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export class MockBuilderPerformance extends MockBuilderPromise {
global.delete(global.get('builder:module'));
}

global.set('builder:config', this.cloneConfig());

const clone = this.cloneConfig();
const ngModule = super.build();
global.set('builder:config', clone);
global.set('builder:module', ngModule);

// avoiding influences on cache when users extend the testing module.
Expand Down
40 changes: 21 additions & 19 deletions libs/ng-mocks/src/lib/mock-builder/mock-builder.promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,27 @@ export class MockBuilderPromise implements IMockBuilder {
this.stash.backup();
ngMocksUniverse.config.set('mockNgDefResolver', new Map());

const params = this.combineParams();

const ngModule = initNgModules(params, initUniverse(params));
detectWrongDeclarations(params);
addMissingKeepDeclarationsAndModules(ngModule, params);
addMissingMockDeclarationsAndModules(ngModule, params);
addRequestedProviders(ngModule, params);
handleRootProviders(ngModule, params);
handleEntryComponents(ngModule);
applyPlatformModules();

ngModule.providers.push(createNgMocksToken());
ngModule.providers.push(createNgMocksTouchesToken());
ngModule.providers.push(createNgMocksOverridesToken(this.replaceDef, this.defValue));

ngMocksUniverse.config.delete('mockNgDefResolver');
this.stash.restore();

return ngModule;
try {
const params = this.combineParams();

const ngModule = initNgModules(params, initUniverse(params));
detectWrongDeclarations(params);
addMissingKeepDeclarationsAndModules(ngModule, params);
addMissingMockDeclarationsAndModules(ngModule, params);
addRequestedProviders(ngModule, params);
handleRootProviders(ngModule, params);
handleEntryComponents(ngModule);
applyPlatformModules();

ngModule.providers.push(createNgMocksToken());
ngModule.providers.push(createNgMocksTouchesToken());
ngModule.providers.push(createNgMocksOverridesToken(this.replaceDef, this.defValue));

return ngModule;
} finally {
ngMocksUniverse.config.delete('mockNgDefResolver');
this.stash.restore();
}
}

// istanbul ignore next
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const factory = (propName: string) => helperMockService.mockFunction(`console.${
export default (...methods: Array<keyof typeof console>): void => {
const backup: Array<[keyof typeof console, any]> = [];

beforeEach(() => {
beforeAll(() => {
if (methods.indexOf('log') === -1) {
methods.push('log');
}
Expand All @@ -18,9 +18,11 @@ export default (...methods: Array<keyof typeof console>): void => {
}
});

afterEach(() => {
afterAll(() => {
for (const [method, implementation] of backup) {
console[method] = implementation;
if ((console[method] as any).__ngMocks) {
console[method] = implementation;
}
}
backup.splice(0, backup.length);
});
Expand Down
19 changes: 12 additions & 7 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.throw-on-console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import coreDefineProperty from '../common/core.define-property';

const factory =
(propName: string) =>
(...args: any[]) => {
const factory = (propName: string) => {
const func = (...args: any[]) => {
const error = new Error(args.join(' '));
coreDefineProperty(error, 'ngMocksConsoleCatch', propName, false);
throw error;
};
coreDefineProperty(func, '__ngMocks', true);

// Thanks Ivy, it does not throw an error and we have to use injector.
return func;
};

// Thanks Ivy, it does not throw an error, and we have to use injector.
export default (...methods: Array<keyof typeof console>): void => {
const backup: Array<[keyof typeof console, any]> = [];

beforeEach(() => {
beforeAll(() => {
if (methods.indexOf('warn') === -1) {
methods.push('warn');
}
Expand All @@ -27,9 +30,11 @@ export default (...methods: Array<keyof typeof console>): void => {
}
});

afterEach(() => {
afterAll(() => {
for (const [method, implementation] of backup) {
console[method] = implementation;
if ((console[method] as any).__ngMocks) {
console[method] = implementation;
}
}
backup.splice(0, backup.length);
});
Expand Down
8 changes: 8 additions & 0 deletions libs/ng-mocks/src/lib/resolve/collect-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const parse = (def: any): any => {
} else if (ngMetadataName === 'ContentChild') {
if (!declarations.queries[prop]) {
declarations.queries[prop] = {
isViewQuery: false,
ngMetadataName,
selector: decorator.args[0],
...(decorator.args[1] || {}),
Expand All @@ -82,6 +83,7 @@ const parse = (def: any): any => {
} else if (ngMetadataName === 'ContentChildren') {
if (!declarations.queries[prop]) {
declarations.queries[prop] = {
isViewQuery: false,
ngMetadataName,
selector: decorator.args[0],
...(decorator.args[1] || {}),
Expand All @@ -90,6 +92,7 @@ const parse = (def: any): any => {
} else if (ngMetadataName === 'ViewChild') {
if (!declarations.queries[prop]) {
declarations.queries[prop] = {
isViewQuery: true,
ngMetadataName,
selector: decorator.args[0],
...(decorator.args[1] || {}),
Expand All @@ -98,6 +101,7 @@ const parse = (def: any): any => {
} else if (ngMetadataName === 'ViewChildren') {
if (!declarations.queries[prop]) {
declarations.queries[prop] = {
isViewQuery: true,
ngMetadataName,
selector: decorator.args[0],
...(decorator.args[1] || {}),
Expand Down Expand Up @@ -151,6 +155,7 @@ const parse = (def: any): any => {
} else if (ngMetadataName === 'ContentChild') {
if (!declarations.queries[prop]) {
declarations.queries[prop] = {
isViewQuery: false,
ngMetadataName,
selector: decorator.selector,
...(decorator.read !== undefined ? { read: decorator.read } : {}),
Expand All @@ -160,6 +165,7 @@ const parse = (def: any): any => {
} else if (ngMetadataName === 'ContentChildren') {
if (!declarations.queries[prop]) {
declarations.queries[prop] = {
isViewQuery: false,
ngMetadataName,
selector: decorator.selector,
...(decorator.descendants !== undefined ? { descendants: decorator.descendants } : {}),
Expand All @@ -172,6 +178,7 @@ const parse = (def: any): any => {
} else if (ngMetadataName === 'ViewChild') {
if (!declarations.queries[prop]) {
declarations.queries[prop] = {
isViewQuery: true,
ngMetadataName,
selector: decorator.selector,
...(decorator.read !== undefined ? { read: decorator.read } : {}),
Expand All @@ -181,6 +188,7 @@ const parse = (def: any): any => {
} else if (ngMetadataName === 'ViewChildren') {
if (!declarations.queries[prop]) {
declarations.queries[prop] = {
isViewQuery: true,
ngMetadataName,
selector: decorator.selector,
...(decorator.descendants !== undefined ? { descendants: decorator.descendants } : {}),
Expand Down

0 comments on commit e3b1809

Please sign in to comment.