question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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):

https://github.com/angular/angular/blob/609024f93dc2df038ca5c3d8bfc2406ca5a2a6ac/packages/core/testing/src/test_bed.ts#L246-L256

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:closed
  • Created 4 years ago
  • Reactions:13
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

8reactions
cexbrayatcommented, Apr 16, 2019

@Goodwine Thank you for your answer. Yeah I knew this was the goal and I very much look forward to it 😉

My point is:

  • can we have a better signature to not have to explicitely cast to Type<MyAbstract>? (looks like no?)
  • if no, maybe the deprecation message and the changelog message should be more explicit about this use-case. Injector.get is not used that often in an application, whereas TestBed.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:

import { Type } from '@angular/core';

const http = TestBed.get(HttpTestingController as Type<HttpTestingController>);
4reactions
jkyoutseycommented, Jun 13, 2019

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>?

Read more comments on GitHub >

github_iconTop Results From Across the Web

strict mode typescript checks fails from tsconfig.json [duplicate]
I have a simple typing error detected with cli flag --strict $ ./node_modules/.bin/tsc --strict main.ts main.ts:6:5 - error TS2322: Type ...
Read more >
Upgrading from AngularJS to Angular
Applications built with component directives are much easier to migrate to Angular than applications built with lower-level features like ng-controller , ng- ...
Read more >
@angular/router-deprecated | Yarn - Package Manager
CanLoad guards in the Router are deprecated. Use CanMatch instead. compiler-cli. Commit, Type, Description. 7d88700933, fix, accept inheriting the constructor ...
Read more >
Angular 9's Best Hidden Feature: Strict Template Checking
The new feature is known as strict template checking. Angular has been checking expressions and bindings within the templates of our ...
Read more >
号外:Angular 4 正式版发布!! - 掘金
This will improve the speed of ngc and you will get better type checking ... Angular is now compliant with TypeScript's StrictNullChecks.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found