vlucas / frisby

Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.
http://frisbyjs.com
1.53k stars 201 forks source link

Function .use using outside value #558

Closed daspedras1 closed 4 years ago

daspedras1 commented 4 years ago

Hey @vlucas, how you doing?

I saw this issue (https://github.com/vlucas/frisby/issues/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!!!

daspedras1 commented 4 years ago

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)
};
H1Gdev commented 4 years ago

@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);
daspedras1 commented 4 years ago

Hey @H1Gdev , it worked for me.

Thanks a lot! You really helped me.