postman-bdd icon indicating copy to clipboard operation
postman-bdd copied to clipboard

before hook inside describe is not load

Open otoniel-isidoro opened this issue 8 years ago • 1 comments

When I use the before block inside the describe block the code inside the before block isn't called but if I put the before block outside of the describe block it is called normally. Is this the normal behaviour?

My script:

eval(globals.postmanBDD);

var schema = {"type":"array","minItems":3,"items":{"type":"object","properties":{"id":{"type":"integer"},"action_code":{"type":["string","null"]},"source":{"type":"string"},"event_extra_info":{"type":"object","properties":{"tag":{"type":"string"}}},"created_at":{"type":"string"}},"required":["id","action_code","source","event_extra_info","created_at"],"additionalProperties":false}};
    

describe('Approved Calculators API', function(){
    before('load schema', function(){ 
        // For now the schema will be maintened on the script too
        console.log("schema:", globals.schema);
        if(globals.schema !== null && globals.schema !== undefined) {
            schema = JSON.parse(globals.schema);
        }
    });
   
    
   it('should return a valid JSON response', function(){
     expect(response).to.be.json;
    //  expect(response.body).to.not.be.empty;
     expect(response.body).to.have.schema(schema);  
   }); 
   
   it('should return 200 as status code', function(){
     expect(response).to.have.status(200);
   });
   
   it('should not take more than 200 miliseconds to load', function(){
     expect(response.time).to.be.at.most(200);
   });
   
   after('clean', function(){
     postman.clearGlobalVariable("schema"); 
   });
});

otoniel-isidoro avatar Feb 06 '17 13:02 otoniel-isidoro

Good catch. The code is currently written with the expectation that any hooks (e.g. before, after, etc.) will be written before anything else, but there's no reason I can't make it work for this scenario too.

The before hook runs one time, before the very first describe block. So, currently, if you define a before hook after the first describe block, then the hook will never trigger. But I'll change it so that if a describe block has already occurred, then the before hook will just run immediately.

JamesMessinger avatar Feb 06 '17 14:02 JamesMessinger