How can I clear my MongoDB collection between Mocha tests? Collection.remove({}) isn't working. . . .
Does anyone see what I could be doing wrong?
I've tried User.remove({})
with this syntax, as well, to no avail.
Also, if you have a moment, does this seem like a reasonable way to write this test? I'm admittedly new to testing.
const User = require('../../../app/models/user');
const config = require('../../../config/config');
const mongoose = require('mongoose');
const chai = require('chai');
const chaiAsPromise = require('chai-as-promised');
const sinon = require('sinon');
const expect = chai.expect;
chai.use(chaiAsPromise);
mongoose.connect(config.database, { useMongoClient: true });
mongoose.Promise = global.Promise;
describe('The user model', () => {
describe('during creation', () => {
beforeEach(() => {
// Clear the User collection of all users.
User.remove({}, () => {});
});
afterEach(() => {
// Clear the User collection of all users.
User.remove({}, () => {});
});
it('should store the user in the database', () => {
let userData = {
email: 'blah@user.com',
password: '1234'
};
let user = new User(userData);
return user.save().then(
newUser => {
return User.findOne({_id: newUser._id}, (error,retrievedUser) => {
let expected = newUser._id;
let actual = retrievedUser._id;
console.log(actual,expected);
return expect(actual).to.equal(expected);
});
}
);
});