New TestBed.get shows deprecation warning for abstract types and everywhere if using a strict tsconfig
See original GitHub issue🐞 bug report
Affected Package
The issue is caused by package @angular/core/testing
Is this a regression?
Yes, the previous version in which this bug was not present was: 8.0.0-beta.11
Description
8.0.0-beta.12
introduced a new TestBed.get
method and deprecated the existing one (see
https://github.com/angular/angular/commit/609024f93dc2df038ca5c3d8bfc2406ca5a2a6ac):
The new signature should be transparent for users as const service = TestBed.get(UserService);
works.
But the new signature does not work with abstract types, like for example HttpTestingController
(see https://github.com/angular/angular/blob/master/packages/common/http/testing/src/api.ts#L29) which leads to confusing deprecation warnings.
Update: this is even happening on every TestBed.get
calls if you have a strict tsconfig, see https://github.com/angular/angular/issues/29905#issuecomment-483651602
🔬 Minimal Reproduction
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
describe('UserService', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [HttpClientTestingModule]
}));
it('should be created', () => {
const http: HttpTestingController = TestBed.get(HttpTestingController);
expect(http).toBeTruthy();
});
});
🔥 Exception or Error
ng lint
shows:
/src/app/user.service.spec.ts:10:49 - get is deprecated: from v8.0.0 use Type<T> or InjectionToken<T>
🌍 Your Environment
Angular Version:
Angular CLI: 8.0.0-beta.13
Node: 11.11.0
OS: darwin x64
Angular: 8.0.0-beta.12
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.800.0-beta.13
@angular-devkit/build-angular 0.800.0-beta.13
@angular-devkit/build-optimizer 0.800.0-beta.13
@angular-devkit/build-webpack 0.800.0-beta.13
@angular-devkit/core 8.0.0-beta.13
@angular-devkit/schematics 8.0.0-beta.13
@angular/cli 8.0.0-beta.13
@ngtools/webpack 8.0.0-beta.13
@schematics/angular 8.0.0-beta.13
@schematics/update 0.800.0-beta.13
rxjs 6.4.0
typescript 3.4.3
webpack 4.29.6
cc @Goodwine who wrote it and @mhevery who reviewed: I have not followed closely but couldn’t get<T>(token: T | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
be enough?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:13
- Comments:17 (8 by maintainers)
@Goodwine Thank you for your answer. Yeah I knew this was the goal and I very much look forward to it 😉
My point is:
Type<MyAbstract>
? (looks like no?)Injector.get
is not used that often in an application, whereasTestBed.get
is used in pretty much every service unit test. So the deprecation message is showing up a lot after upgrading and other developers are going to wonder if this is normal or not.It would be awesome to not have to cast, because I think it’s not a very good developer experience to have to write:
How did this get overlooked on the upgrade guides and breaking changes? Unit tests are not second class code. I’m seeing this in the latest release version 8.0.0. Are we awaiting a quick bug-fix release that will allow this to work? Or for 8.0.0 do I just have to use the new
get<T>
?