Use typed get method in TestBed class
See original GitHub issueI’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:
- Created 5 years ago
- Comments:24 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.Talking with the angular team (I’m not part of the angular team), I’ll actually send PRs in the following order:
@deprecate
to current function signature and add same signature asInjector.get
, however it would still returnany
.