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.

Use typed get method in TestBed class

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

Current behavior

The get static method of TestBed class does not return a typed object.

const service = TestBed.get(MyService); // service is typed as any

Expected behavior

The get static method should return a typed object.

const service = TestBed.get(MyService); // service should be typed as MyService

Minimal reproduction of the problem with instructions

Look examples.

What is the motivation / use case for changing the behavior?

This would improve type safety.

Environment


Angular version: 6.1.9

Others:

A possible implementation using new helper types from TypeScript would be:

    static get<T extends new (...args: any[]) => any>(token: T, notFoundValue?: T | InstanceType<T>): InstanceType<T>;

I’m not sure if the notFoundValue should be a constructor or an instance, so I typed it as both, but fell free to update it as is actually needed. I tested that signature and it actually returns a typed object as wanted.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:24 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Goodwinecommented, Aug 19, 2019

Per discussion with @vikerman, the Angular team can’t accept such a breaking change at this point, so the approach we’ll now take is to create a new method TestBed.inject which will be type-safe. And deprecate TestBed.get.

1reaction
Goodwinecommented, Mar 13, 2019

Talking with the angular team (I’m not part of the angular team), I’ll actually send PRs in the following order:

  1. Add @deprecate to current function signature and add same signature as Injector.get, however it would still return any.
    • This already happened on #13785 but now breaking changes aren’t as easy to make 😃
  2. Add set of types that will eventually be used, even if they aren’t used yet.
  3. Insert breaking changes that could land in a future major version, but definitely not Angular 8
Read more comments on GitHub >

github_iconTop Results From Across the Web

TestBed
Configures and initializes environment for unit testing and provides methods for creating components and services in unit tests. See more... class TestBed { ......
Read more >
What's the difference between TestBed.get and new ...
The angular guide demonstrates two different ways of testing, one by calling new Service() and providing the dependencies to the constructor ...
Read more >
Testing Dependency Injection • Angular - codecraft.tv
The inject function wraps the test spec function but lets us also inject dependencies using the parent injector in the TestBed . We...
Read more >
Angular unit testing tutorial with examples
Unit testing is the process of testing small, isolated pieces of code. Also known as isolated testing, unit tests do not use external ......
Read more >
Testing Components in Angular 2 with Jasmine
We then set up our testing module using TestBed.configureTestingModule . Our testing module needs access to the form classes, methods, ...
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