gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 785 forks source link

my ajax function somehow not covered by istanbul #892

Closed fikri2992 closed 6 years ago

fikri2992 commented 6 years ago

I have a question I have this function

    authAnonymous: function (callback) {
    rest
      .post(wso2config.token.host + "/" + wso2config.token.path, {
        username: wso2config.clientId,
        password: wso2config.clientSecret,
        data: {
          username: wso2config.anonymousUser.username,
          password: wso2config.anonymousUser.password,
          grant_type: "password",
          redirect_uri: wso2config.redirect_uri,
          scope: "somescope:thisisit"
        }
      })
      .on("complete", function (data) {
        if (data.error) callback(data, null);
        else {
          data.anonym = true;
          callback(null, {
            openid_data: data
          });
        }
      });
  },

so i want to code coverage this function and i make some unit test using Jest here's the unit test code

test("do login using authAnonymous", done => {
  openID.authAnonymous(function (error, data) {
    if (!error) {
      expect(data.anonym).toBeTruthy();
    } else {
      //expect(data.anonym).toBe(false);
    }
  });
  done();
});

somehow this unit test is working as I expected but code coverage says the statement is not covered

this says the statement is not covered screenshot_14

as i understand my code works fine and unit test is working as expected why jest code coverage says this statement not covered can anyone explain how this statement covered works ? and why mine is not covered, i believe this is my fault but i don't know what to investigate here

--- update ---

this my silly mistake, it only needs to mock the post and chaining on function like this

  post: function() {
    return {
      on: function(event, callback) {
        callback('data', 'response');
      }
    }
  }
}