When we use Angular's schematics to generate components, unnecessary asynchronous code is added to the test setup hook for its test suite.
The following test setup is generated by Angular's component
generation schematic:
Alternatively, we might be using Angular's waitForAsync
test function wrapper (formerly named async
) as seen in the following code snippet:
Here's the deal: It's only necessary to call the static TestBed.compileComponents
method if we're not using the Angular CLI to run our tests (who would do such a thing, Google? ππ).
The Angular CLI compiles our application and tests before the tests are run so no asynchronous action is needed for setting up the declarables.
Let's simplify the common test setup by leaving out async-await, waitForAsync
, and even the TestBed.compileComponents
invocation as seen in this code snippet:
The following points are true for common Angular testbed setup for tests covering all types of Angular declarables:
- No need to use async-await
- No need to use
waitForAsync
(formerly namedasync
) - No need to call
TestBed.compileComponents
Of course, there might be other reasons for introducing asynchronicity to our test setup but compiling and linking declarables is not one of them.
Enjoy a little less boilerplate in your Angular tests π
Top comments (3)
I haven't given it a go yet.. but if that's true that'll be a big win! Nice!
Just a warning for someone, you may need compileComponents if you are not executing your tests with ng test.
What's your toolchain, @kellyprankin?