Created
October 1, 2014 09:52
-
-
Save bendrucker/cf298bf515d8b6fdcd05 to your computer and use it in GitHub Desktop.
Truncate data during tests with knex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Promise = require('bluebird'); | |
var knex = require('../../src/db').knex; | |
var tables = [ | |
'organizations', | |
'campaigns', | |
'donors', | |
'pledges', | |
'payments' | |
]; | |
function truncate () { | |
return Promise.each(tables, function (table) { | |
return knex.raw('truncate table ' + table + ' cascade'); | |
}); | |
}; | |
function seed () { | |
return Promise.each(tables, function (table) { | |
return knex(table).insert(require('./seeds/' + table)); | |
}); | |
}; | |
describe('Integration Tests', function () { | |
beforeEach(function () { | |
return truncate().then(seed); | |
}); | |
afterEach(function () { | |
return truncate(); | |
}); | |
require('require-all')(__dirname + '/specs'); | |
}); |
Thanks for sharing your integration test code!
It is also recommended to update the test database to the latest schema before running any test:
const config = require(`${process.cwd()}/knexfile`)[process.env.NODE_ENV]
const knex = require('knex')(config)
const {MyPOJO} = require('../src/MyPOJO')
const {Model} = require('objection')
beforeAll(async done => {
await knex.migrate.latest()
await knex(MyPOJO.tableName).truncate()
Model.knex(knex)
done()
})
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is not restarting identity after each truncate intentional?