I am having real issues stubbing one particular thing using sinon. I have a simple function I am testing
const floatAPIModels = require("models/float/floatAPIModels");
const insertFloatData = async (databaseConnection, endpoint, endpointData) => {
try {
const floatModel = floatAPIModels(databaseConnection);
await databaseConnection.sync();
if (endpoint === "people") {
endpointData.forEach(async (record) => {
await floatModel.Person.upsert(record);
});
}
return true;
} catch (error) {
console.log("Unable to insert data into the database:", error);
return error;
}
};
The problem is with floatAPIModels being an Object that returns things. My implementation is this
const { DataTypes } = require("sequelize");
const floatAPIModels = (sequelize) => {
const Person = sequelize.define(
"Person",
{
people_id: { type: DataTypes.INTEGER, primaryKey: true },
job_title: { type: DataTypes.STRING(200), allowNull: true },
employee_type: { type: DataTypes.BOOLEAN, allowNull: true },
active: { type: DataTypes.BOOLEAN, allowNull: true },
start_date: { type: DataTypes.DATE, allowNull: true },
end_date: { type: DataTypes.DATE, allowNull: true },
department_name: { type: DataTypes.STRING, allowNull: true },
default_hourly_rate: { type: DataTypes.FLOAT, allowNull: true },
created: { type: DataTypes.DATE, allowNull: true },
modified: { type: DataTypes.DATE, allowNull: true },
},
{
timestamps: true,
tableName: "Person",
}
);
return {
Person,
};
};
module.exports = floatAPIModels;
I have removed some things to cut down on code. At the moment I am doing something like this
const { expect } = require("chai");
const sinon = require("sinon");
const floatAPIModels = require("src/models/float/floatAPIModels");
const floatService = require("src/services/float/floatService");
describe("insertFloatData", () => {
let databaseConnection;
let floatModelMock;
beforeEach(() => {
databaseConnection = {};
floatModelMock = {
Person: { upsert: sinon.stub().resolves() },
};
sinon.stub(floatAPIModels, "Person").returns(floatModelMock.Person);
});
afterEach(() => {
sinon.restore();
});
it("should insert endpointData into the 'people' endpoint", async () => {
const endpoint = "people";
const endpointData = [{ record: "data" }];
await floatService.insertFloatData(databaseConnection, endpoint, endpointData);
expect(floatModelMock.Person.upsert.calledOnce).to.be.true;
expect(floatModelMock.Person.upsert.firstCall.args[0]).to.deep.equal(endpointData[0]);
});
});
With the above, I get
TypeError: Cannot stub non-existent property Person
But I have tried default, and a lot of other ways, but none of them seems to work.
How can I properly stub this and get the unit test working?
Thanks
floatAPIModelsis a function that returns{ Person }object. There is noPersonproperty on this function. That's why you got the error.In order to stub the
floatAPIModelsfunction, I will use the proxyquire module to do this.E.g.
model.js:service.js:service.test.js:Test result: