Cleaning the database in-between Mocha tests with Pg-promise
describe('db truncation in before hook', () => {
beforeEach('truncating db', () => {
return db.none('TRUNCATE courses RESTART IDENTITY')
});
// in one test we insert data into our database
it('inserting data into db', () => {
return db.none('insert into courses (title) values (${title})', { title: 'hello' })
});
// in another test we check db is indeed been cleaned by the before hook
it('not seeing data in the db', () => {
return db.any('select * from courses').then((data) => {
expect(data).to.deep.equal([]);
})
});
});
Why are we returning
db.any/none
promises from our tests?
Because if Mocha sees a promise returned from theit
block, it will wait till this promise gets resolved. Otherwise our tests would always be passing by not hittingexpect
at the right time.
https://mochajs.org/#asynchronous-codeWhat does the before hook do?
It truncates thecourses
table, and restarts its sequence columns (egid
) count.
https://www.postgresql.org/docs/9.1/static/sql-truncate.html
Written by lakesare
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Pg-promise
Authors
Evgenia Karunus
7.591K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#