javascript - How to mock sequelize model methods in Node.js - Stack Overflow

I have been struggling a lot to mock database in one of my side projects while writing junits. Can some

I have been struggling a lot to mock database in one of my side projects while writing junits. Can somebody please help me out here. Below is what the scenario looks like:

Before that, the source code is here -

  1. I have created a model using sequelize module in Nodejs. And I access my db through this model.

  2. I want to mock the db calls when running junits. For example findOne method here which returns a promise (.js#L4). Basically when running this particular endpoint I want to skip accessing the db.

Any help is appreciated!

Regards, Sunil

I have been struggling a lot to mock database in one of my side projects while writing junits. Can somebody please help me out here. Below is what the scenario looks like:

Before that, the source code is here - https://github./sunilkumarc/track-courier

  1. I have created a model using sequelize module in Nodejs. And I access my db through this model.

  2. I want to mock the db calls when running junits. For example findOne method here which returns a promise (https://github./sunilkumarc/track-courier/blob/master/models/parcels.js#L4). Basically when running this particular endpoint I want to skip accessing the db.

Any help is appreciated!

Regards, Sunil

Share Improve this question edited Feb 23, 2017 at 17:22 Sunil Kumar asked Feb 22, 2017 at 17:46 Sunil KumarSunil Kumar 5456 silver badges13 bronze badges 5
  • 3 Possible duplicate of Mocking database in node.js? – Hodrobond Commented Feb 22, 2017 at 17:51
  • 1 Take a look on proxyquire – Jaime Commented Feb 22, 2017 at 21:09
  • 1 Also, your current tests looks to me more like integration/e2e tests than unit test. Check this Writing great unit test – Jaime Commented Feb 22, 2017 at 21:15
  • @Hodrobond Yes. Looks like it is duplicate. I don't know how I missed it. Thanks for pointing out. – Sunil Kumar Commented Feb 23, 2017 at 5:56
  • @Jaime Thanks for the link. I will go through it. – Sunil Kumar Commented Feb 23, 2017 at 5:56
Add a ment  | 

2 Answers 2

Reset to default 1

While people have pointed out in the ments to a similar question that it's probably wise to include a test Database, I'm going to answer the question directly.

Utilizing Jest, you can do the following to mock individual calls on specific models

const myUser = User.build({
  ...attributes,
});

jest.spyOn(User, 'findOne').mockImplementation((options) => Promise.resolve(myUser));

const mockedUser = await User.findOne({});

In my experience, I do agree with the menters. Mocking specific functions like these are not always the most useful as they may not accurately depict the return values of a specific Sequelize function. For example, User#findOne may return null. If you provide rejectOnEmpty, you will have to build your own logic for handling this, which may differ from the exact logic used in the Sequelize library.

Ultimately your code is likely responsible for handling whatever Sequelize returns correctly, and with the level of integration to your data layer, this is going to be something very difficult to mock correctly - not to mention extremely tedious.

Documentation to Model#findAll, where you can see rejectOnEmtpy: https://sequelize/api/v6/class/src/model.js~model#static-method-findAll

I used to use Sinon.JS for mocking anything, in this case I did it as:

// assume "Users" is our table

sinon.replace(Users, "create", () => console.log(`mocked "Users" table's "create" method`));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742352944a4427877.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信