Search code examples
node.jsunit-testingexpressmocha.jssupertest

Unit test with rollback on express


Hi I´m trying to make a unit test creating some fake data and executing a request (using supertest) to check if the controller works well. The problem rise up when the controller tries to get the fake data, because it exists only inside the transaction.

So my questions are:

  • Did someone make a junit like test using node.js and express?
  • How do you manage the database data? How do you do rollback on it?

Thanks in advance.


Solution

  • If I'm understanding you correctly, you are trying to run a bunch of api calls with some fake data, but you are unable to actually get the data from database?

    If that's the case, one of two things I would suggest:

    1. Allow your api to update the db, then retrieve from db. When your tests are done, you can do the full clean up at that point (that can be achieve via the after() in mocha)

    2. Use sinon.js, and stub all db calls to return some fake data that you know.