jasmine add 'it' block as a function in before each

378 views Asked by At

I am using Frisby.js for automating my api's. There are two api addUser and delete user, which I need to use before multiple other api's and then delete it. I am using the following block format in cases where I need to use the two api's.

 describe('Successfully change password', function () {
    //addUser 
    beforeEach(function(done) {
      frisby
        .post(adduser_endpoint, newuser_payload)
        .expect('status',200)
        .done(done)
    })
     it('Enter valid Password to set password', function (done) {
     frisby
        .post(reset_password_endpoint, change_password_payload)
        .expect('status', 200)
        .done(done)
   })
  //delete user
  afterEach(function(done) {
     frisby
        .del(del_user_endpoint)
        .expect('status',200)
        .done(done)
    })


})

This is the redundant code being called multiple times. I was trying to extract the two function so that it can be reused, and then rewrite above test as following. However, this is not working.

const addUser = function(done) {
  frisby
    .post(adduser_endpoint, newuser_payload)
    .expect('status',200)
    .done(done)
})
const delete =function(done) {
 frisby
    .del(del_user_endpoint)
    .expect('status',200)
    .done(done)
}

describe('Successfully change password', function () {
//addUser 
beforeEach(addUser())
 it('Enter valid Password to set password', function (done) {
 frisby
    .post(reset_password_endpoint, change_password_payload)
    .expect('status', 200)
    .done(done)
 })
//delete user
 afterEach(delete())
})
1

There are 1 answers

0
Thomas Ingalls On

Try this:

beforeEach(addUser()) --> beforeEach(addUser)

afterEach(delete()) --> afterEach(delete)

What you had was an invocation of the functions that you defined. What you were really passing was an unresolved promise to beforeEach and afterEach, where you wanted to pass a function that takes a single parameter, done.

This change passes your predefined functions, addUser and delete -- which have the correct signature -- to the setup and teardown functions.