Olivvv
Olivvv

Reputation: 1200

how to abstract and re-use a describe() block in jasmine?

I work on an app that handles CRUD on a certain type of documents. Therefore we want often to test the conformity of that document against a set of values. I have a describe() block with it() inside that are testing each field of that document.

I would like to make that re-usable, but simply wrapping a function around that describe block leads to timing issues, and the following test are running too early, before the tests in that wrapping function.

Is there a pattern to do this ? Should it be a promise ? Should I make of it a matcher ? docToBeEqualTo() ? I haven't seen it() used within matchers, it does not seems to be the right place for this

I guess I am missing a common pattern here. can you help ?

thanks a lot

edit: requested code exemple :

function compareDocs(targetData, timeout){
        describe('the doc should contain : ', function(){
            it('The amount field should be : "' + targetData.amount + '". ', function(){
                expect(element(by.css(selector)).getAttribute('value')).toEqual(targetData.amount);
            }, timeout);


            it('The foo bar number  should be : "' + targetData.fooBar + '". ', function(){
                expect(element(by.css(selector)).getAttribute('value')).toEqual(targetData.fooBarNumber);

            }, timeout);

            return browser.waitForAngular();
        });
});

used like this:

describe('first describe', function(){
            it('compare docs', function(){
               compareDocs(currentData,targetData);
            }, timeout);
});
describe('second describe where the doc is deleted', function(){
        it('should remove payment from list', function(){
              deleteDoc();//this runs too early, and delete the doc before compareDocs() has finished
              expect(element(by.css(selector)).isPresent).toBe(false);              
            }, timeout);
        });

TL;DR : how to prevent deleteDoc() to run before compareDoc() has finished ?

Upvotes: 1

Views: 793

Answers (1)

giri-sh
giri-sh

Reputation: 6962

Jasmine always checks for blocks of describe and nested describe functions. However, when you define a suite(describe) or spec(it) inside a function that executes only on being called, protractor doesn't recognise the suite or spec in the function specified. You can either use loops or self executing functions to execute suites and specs (This wouldn't solve your problem i suppose). Only possible solution is to call a function that has block of code without any suites or specs in it. Here's an example -

function compareDocs(targetData, timeout){
    console.log('The amount field should be : "' + targetData.amount + '". ');
    expect(element(by.css(selector)).getAttribute('value')).toEqual(targetData.amount);
    console.log('The foo bar number  should be : "' + targetData.fooBar + '". ');       
    expect(element(by.css(selector)).getAttribute('value')).toEqual(targetData.fooBarNumber);
    return browser.waitForAngular();
};

NOTE: A describe suite nested inside an it spec won't execute.

Hope it helps.

Upvotes: 2

Related Questions