Testing database related code in NodeJS

后端 未结 1 1124
轮回少年
轮回少年 2021-02-20 00:06

Using PostgreSQL.

I\'m trying to set up a proper test suite for an API. So far it works but the tests are done on the main database directly. I then have to remove every

相关标签:
1条回答
  • 2021-02-20 00:39

    I would suggest a separate test database. It can be light, and you will want to know the data that is in there (so you can test it!). A base dataset that can handle all your business rules can be exported as a SQL file (or some export method).

    Typically your application will have a connection to the database, and your test framework will have some approach to run a method prior to tests starting. It is here that you specify the test DB. Your database access objects (DAOs), or scripts, methods, will utilize the main connection in some way, either as a method parameter, or require statement, etc.

    As an example, I'm using the knex module to connect to the DB and build queries. I initialize and reference my single DB connection as specified in their docs.

    var Knex = require( 'knex' );
    Knex.knex = Knex.initialize( {
      client : 'mysql',
      connection : {
      host : 'my.domain.com',
      user : 'dbrole',
      password : 'password',
      database : 'productiondb',
      charset : 'utf8'
      }
    } );
    

    My DAOs get the connection like this:

    var knex = require('knex').knex;
    

    Now in my unit tests, before a test suite is run, I can set my connection to be the test DB

    var Knex = require( 'knex' );
    Knex.knex = Knex.initialize( {
      client : 'mysql',
      connection : {
      host : '127.0.0.1',
      user : 'root',
      password : 'root',
      database : 'testdb',
      charset : 'utf8'
      }
    } );
    

    And there you have it! Exact same code is used in test and production, and your production DB is de-coupled from your tests. This pattern can work with a lot of frameworks, so you'll have to adapt (and clean up your tests if they are junking up the test DB, maybe a restore to default when all tests are complete).

    Edit: By the way, knex works with postgre and is a fun way to build queries in pure node JS. It can also execute raw SQL.

    0 讨论(0)
提交回复
热议问题