node.jspostgresqlnpmdatabase-cleanerknex.js

Cleaning database after tests in node.js


How I can clean database after each it?

In rails I use https://github.com/bmabey/database_cleaner, but I did't find something similar for node.js

node.js (v0.10.26), PostgreSQL (9.3.3), mocha, restify and knex.


Solution

  • The easy way I have found to clean the database between test is to

    DROP SCHEMA public CASCADE;
    CREATE SCHEMA public AUTHORIZATION my_test_user;
    

    Once the public schema belongs to the test user, he is able to drop and re-create the schema when needed. Be aware that drop everything that lies in your database's public schema.