Skip to content

Commit

Permalink
fix(ngMocks): respects custom errors on lookups #7041
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Nov 15, 2023
1 parent ffde5dd commit 6c78f54
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ export default <T>(...args: any[]): T => {
} else {
try {
result.push(getInjection(declaration));
} catch {
// nothing to do
} catch (error) {
// forwarding unexpected errors: https://github.com/help-me-mom/ng-mocks/issues/7041
if (!error || typeof error !== 'object' || (error as any).ngTempTokenPath === undefined) {
throw error;
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion libs/ng-mocks/src/lib/mock-helper/mock-helper.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export default <T>(...args: any[]) => {
if (args.length === 1) {
try {
return TestBed.inject ? TestBed.inject(args[0]) : /* istanbul ignore next */ TestBed.get(args[0]);
} catch {
} catch (error) {
// forwarding unexpected errors: https://github.com/help-me-mom/ng-mocks/issues/7041
if (!error || typeof error !== 'object' || (error as any).ngTempTokenPath === undefined) {
throw error;
}
throw new Error(`Cannot find an instance via ngMocks.get(${funcParseFindArgsName(args[0])})`);
}
}
Expand Down
133 changes: 133 additions & 0 deletions tests/issue-7041/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import {
Inject,
Injectable,
InjectionToken,
NgModule,
} from '@angular/core';
import { TestBed } from '@angular/core/testing';

import { ngMocks } from 'ng-mocks';

const ENVIRONMENT_NAME = new InjectionToken('ENVIRONMENT_NAME');

@Injectable()
class ServiceA {
constructor(
@Inject(ENVIRONMENT_NAME) private readonly envName: string,
) {}

public getCode(): string {
return this.envName;
}
}

@Injectable()
class ServiceB {
constructor(private readonly serviceA: ServiceA) {}

public getCode(): string {
return this.serviceA.getCode();
}
}

@Injectable()
class ServiceC {
constructor(private readonly serviceA: ServiceA) {}

public getCode(): string {
return this.serviceA.getCode();
}
}

@NgModule({
providers: [
{
provide: ENVIRONMENT_NAME,
useFactory: () => {
throw new Error('SHOW ME');
},
},
ServiceA,
ServiceB,
],
})
class TargetModule {}

// @see https://github.com/help-me-mom/ng-mocks/issues/7041
describe('issue-7041', () => {
beforeEach(() =>
TestBed.configureTestingModule({
imports: [TargetModule],
}).compileComponents(),
);

describe('TestBed', () => {
it('throws the original error in ENVIRONMENT_NAME', () => {
expect(() => TestBed.get(ENVIRONMENT_NAME)).toThrowError(
'SHOW ME',
);
});

it('throws the original error in ServiceA', () => {
expect(() => TestBed.get(ServiceA)).toThrowError('SHOW ME');
});

it('throws the original error in ServiceB', () => {
expect(() => TestBed.get(ServiceB)).toThrowError('SHOW ME');
});

it('throws the original error in ServiceC', () => {
expect(() => TestBed.get(ServiceC)).toThrowError(
/NullInjectorError: No provider for ServiceC/,
);
});
});

describe('ngMocks.findInstance', () => {
it('throws the original error in ENVIRONMENT_NAME', () => {
expect(() =>
ngMocks.findInstance(ENVIRONMENT_NAME),
).toThrowError('SHOW ME');
});

it('throws the original error in ServiceA', () => {
expect(() => ngMocks.findInstance(ServiceA)).toThrowError(
'SHOW ME',
);
});

it('throws the original error in ServiceB', () => {
expect(() => ngMocks.findInstance(ServiceB)).toThrowError(
'SHOW ME',
);
});

it('throws the original error in ServiceC', () => {
expect(() => ngMocks.findInstance(ServiceC)).toThrowError(
/Cannot find an instance/,
);
});
});

describe('ngMocks.get', () => {
it('throws the original error in ENVIRONMENT_NAME', () => {
expect(() => ngMocks.get(ENVIRONMENT_NAME)).toThrowError(
'SHOW ME',
);
});

it('throws the original error in ServiceA', () => {
expect(() => ngMocks.get(ServiceA)).toThrowError('SHOW ME');
});

it('throws the original error in ServiceB', () => {
expect(() => ngMocks.get(ServiceB)).toThrowError('SHOW ME');
});

it('throws the original error in ServiceC', () => {
expect(() => ngMocks.get(ServiceC)).toThrowError(
/Cannot find an instance/,
);
});
});
});

0 comments on commit 6c78f54

Please sign in to comment.