Hey I am new to tests in angular 6(aka angular) and I have one concern regrading every test which I have seen so far.
Let's first look at a simple test of simple component(which is generated by cli)
describe('CompComponent', () => {
let component: CompComponent;
let fixture: ComponentFixture<CompComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CompComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CompComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
I have one main question:
1.How do I know for certainly that each Async beforeEach call is done before each test unit(aka it)? are there any cases where this call will happen after each it because it async call after all?
Each beforeEach will finish before any test begins. In your example, compileComponents() is asynchronous. So, we must use async() method in this case. You can set a break point at this line: console.
Usually, the most convenient way to write async tests is to use async / await . async functions implicitly return a promise. Jasmine will wait until the returned promise is either resolved or rejected before moving on to the next thing in the queue.
If all your tests only read it, then there is no need to create it over and over. If each test in your describe needs a new copy of the structure because each test is modifying the structure then you should use beforeEach to create the structure anew for each test and then afterEach if you need to tear it down cleanly.
Using the done() Method in Your Jasmine-driven Asynchronous JavaScript Tests. Jasmine. Async is an add-on library for Jasmine that provides additional functionality to do asynchronous testing. Modeled after Mocha's async test support, it brings the done() function to the Jasmine unit testing environment.
Each beforeEach
will finish before any test begins.
In your example, compileComponents()
is asynchronous. So, we must use async() method in this case.
for your reference, look to this link: https://github.com/angular/angular-cli/issues/4774
to be sure that your beforeEach
will finish before any test begins, you can try to debug your test by adding this:
compileComponents().then( result => {
console.log(result);
}
You can set a break point at this line: console.log(result);
and you will see that it will be executed all time you run your test, so if you set a breakpoint in this console.log' line and another one in your firt
ittest, you will see you will never get to the second break point before doing the
console.log` break point one, that means that we will have to wait for any async call inside the beforeEach before going to any test.
Another way to see that the beforeEach
will all time finish before any test begins is to write your test this way as well:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CompComponent ]
}).compileComponents().then( result => {
fixture = TestBed.createComponent(CompComponent);
component = fixture.componentInstance;
}
}));
You will see that in any of your test the fixture
is already available, doing that way. So you don't need to add another beforeEach to initialize your fixture
.
To understand more, you can refere to this other answer from Stack Overflow:
angular-2-testing-async-function-call-when-to-use
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With