Unit testing with Bookshelf.js and knex.js

后端 未结 2 791
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 08:18

I\'m relatively new to Node and am working on a project using knex and bookshelf. I\'m having a little bit of trouble unit testing my code and I\'m not sure what I\'m doing

2条回答
  •  心在旅途
    2020-12-14 08:54

    I have been using in-memory Sqlite3 databases for automated testing with great success. My tests take 10 to 15 minutes to run against MySQL, but only 30 seconds or so with an in-memory sqlite3 database. Use :memory: for your connection string to utilize this technique.

    A note about unit tesing - This is not true unit testing, since we're still running a query against a database. This is technically integration testing, however it runs within a reasonable time period and if you have a query-heavy application (like mine) then this technique is going to prove more effective at catching bugs than unit testing anyway.

    Gotchas - Knex/Bookshelf initializes the connection at the start of the application, which means that you keep the context between tests. I would recommend writing a schema create/destroy script so that you and build and destroy the tables for each test. Also, Sqlite3 is less sensitive about foreign key constraints than MySQL or PostgreSQL, so make sure you run your app against one of those every now and then to ensure that your constraints will work properly.

提交回复
热议问题