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/nonepromises from our tests?
Because if Mocha sees a promise returned from theitblock, it will wait till this promise gets resolved. Otherwise our tests would always be passing by not hittingexpectat the right time.
https://mochajs.org/#asynchronous-codeWhat does the before hook do?
It truncates thecoursestable, 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
8.539K
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#