vlucas / frisby

Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.

Home Page:http://frisbyjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Function .use using outside value

daspedras1 opened this issue · comments

Hey @vlucas, how you doing?

I saw this issue (#478) and I tried to do something with the use function.

I'm trying to abstract some validations, and I created a function to validate the status code. My problem is, I wanna reuse the function with multiple status, passing them in the function call.

Something like this:

function statusCodeValidation(spec, statusCode) {
    return spec.expect('status', statusCode)
}

function retrieveList(status) {
  return frisby.get(<endpoint here>)

    .use(statusCodeValidation(status))
};

retrieveList(200);

But I'm getting this:

TypeError: spec.expect is not a function

       8 | 
       9 | function statusCodeValidation(spec, statusCode) {
    > 10 |     return spec.expect('status', statusCode)
         |                 ^
      11 | }

You know how can to make this work?

Thank you so much!!!

It only works if I do this:

function statusCodeValidation(spec) {
    return spec.expect('status', 200)
}

function retrieveList(status) {
  return frisby.get(<endpoint here>)

    .use(statusCodeValidation)
};

@daspedras1

return function.

function statusCodeValidation(statusCode) {
  // return function.
  return function(spec) {
    spec.expect('status', statusCode)
  };
}

function retrieveList(status) {
  return frisby.get(<endpoint here>)
    .use(statusCodeValidation(status))
};

retrieveList(200);

Hey @H1Gdev , it worked for me.

Thanks a lot! You really helped me.