简体   繁体   中英

jasmine spy not finding property

I have a file that basically looks like this(shortened)

const octokit = new (require("@octokit/rest"))();
function buildRepo(name) {
  fs.promises
    .readFile("data/settings.json")
    .then(data => JSON.parse(data))
    .then(settings => settings.repositories.find(repo => repo.name === name))
    .then(repo => {
      let repoName = repo.url
        .substring(repo.url.lastIndexOf("/") + 1)
        .slice(0, -4);
      let jobName = repo.name;
      return octokit.repos
        .get({
          owner: "munhunger",
          repo: repoName
        })
        .then(({ data }) => {
          ...
        });
    });
}

module.exports = { buildRepo };

And so I want to write a test for what it does with the data that it gets from the octokit.repos.get function. But since that function will go out to the internet and look at GitHub repositories, I want to mock it.

I have a few tests running with jasmine, and I read up slightly on it and it seems as if jasmine should be able to mock this for me.

However, the test that I have written seems to fail.

const builder = require("./index");

describe("spyOn", () => {
  it("spies", () => {
    spyOnProperty(builder, "octokit");
    builder.buildRepo("blitzbauen");
  });
});

With the error octokit property does not exist . What am I doing wrong here? would I need to add octokit to module.exports ?(which seems rather insane)

Yes, you'd need to add Octokit to module.exports since you now only export buildRepo . Anything from a module that is not exported can't be accessed directly by other modules, so if it should be accessible it should be exported.

Alternatively, you may be able to mock the entire Octokit module with Jasmine so any calls by any script are made to the mocked version, but I'm not sure how you'd go about doing that since my experience with Jasmine is limited

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM