samedi 28 septembre 2019

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 get 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)

Aucun commentaire:

Enregistrer un commentaire