Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async beforeEach finished before/after each test unit(it)

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?

like image 685
Brk Avatar asked Oct 17 '18 22:10

Brk


People also ask

Is beforeEach asynchronous?

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.

How do you wait for async to finish in Jasmine?

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.

In what case would you need to use beforeEach () and afterEach () in a test suite?

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.

What is done () in Jasmine?

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.


1 Answers

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 theconsole.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

like image 83
HDJEMAI Avatar answered Oct 03 '22 00:10

HDJEMAI