I'm trying to test out a class method, where the method ultimately calls a dependency like so:
class Repo {
api;
constructor(host) {
const api = new repoClient({
host: host
});
this.api = api;
}
async getLatestProjectPipeline(projectId) {
// sorted in descending order by default.
const projectPipelines = await this.api.Pipelines.all(projectId)
if (projectPipelines.length == 0) {
// TODO: Error out
return null;
}
return projectPipelines[0];
};
}
The caveat here is that repoClient only has properties and no immediate methods. (e.g. this.api.Pipelines is a property, then you call said property's methods.
How do I correctly stub the this.api.Pipelines.all method?
Currently, if I use sinon.createStubInstance(repoClient);, sinon complains:
Error: Found no methods on object to which we could apply mutations
However, if I don't it will actually try to call the actual constructor for repoClient, which I do not want.
You can create a stub
repoClientclass and pass it to theRepoclass's constructor.E.g.
repoClient.js:repo.js:repo.test.js: