Search code examples
angularjasmineprotractor

JS Jasmine's `beforeEach` not waiting before running the test


I am new to Protractor and Jasmine, i have encountered this issue while writing my first test:

    describe('Test suite for gui', () => {
    let page: GuiPage;

        beforeEach(async () => {
            page = new GuiPage();
            console.log(`GuiPage object status before each:${page}`);
            await page.login();
        });
    it('First test', async () => {
            console.log(`GuiPage object status first test: ${page}`);
            page.invokeSomeMethod();
    }
}

The output:

GuiPage object status first test: undefined
Cannot read property 'invokeSomeMethod' of undefined
....a long stacktrace....
GuiPage object status before each:[object Object]

So, clearly it seems like the test itself is invoked before the call to beforeEach is completed.
In the protractor config file i have:

exports.config = {
.....many other non relevant configs....
  onPrepare() {
    browser.ignoreSynchronization = true;
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 240000;
  }
};

Is there anything visibly wrong in my test?


Solution

  • I have faced a situation similar to this and it was frustrating. I think it might depend on the version of Jasmine you're on (older version you will experience this I think).

    Regardless, I did this to fix it:

    // add done call back
    beforeEach(async (done: DoneFn) => {
                page = new GuiPage();
                console.log(`GuiPage object status before each:${page}`);
                await page.login();
                // call done to let jasmine know we are done with this function
                done();
            });
    

    Hopefully, the above will fix it.