Я застрял в одной проблеме, когда я тестирую модули API с mongodb. Я использую Mocha и Chai для модульного тестирования, однако я сталкиваюсь с проблемой, что когда я тестирую второй случай, мне нужно очистить коллекцию mongodb, чтобы новые данные приходили и тестировались автоматически, но я не мог найти ничего, как удалять коллекцию каждый раз. время до запуска кода модульного теста.
Пожалуйста, см. Ниже код моего юнит-теста
test. js
process.env.NODE_ENV = 'test';
let mongoose = require("mongoose");
let chai = require('chai');
let chaiHttp = require('chai-http');
const expect = require('chai').expect;
const request = require('supertest');
const app = require('../../../app.js');
const conn = require('../../../db/index.js');
chai.use(chaiHttp);
describe('notes', () => {
beforeEach((done) => { //Before each test we empty the database
Book.remove({}, (err) => {
done();
});
});
});
describe('GET /notes', () => {
before((done) => {
conn.connect()
.then(() => done())
.catch((err) => done(err));
})
after((done) => {
conn.close()
.then(() => done())
.catch((err) => done(err));
})
it('OK, getting notes has no notes', (done) => {
request(app).get('/notes')
.then((res) => {
const body = res.body;
expect(body.length).to.equal(0); // getting error here as collection have data
// res.should.have.status(200);
done();
})
.catch((err) => done(err));
});
app. js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const Note = require('./db/models/note.js').Note;
app.use(bodyParser.json());
app.get('/notes', (req, res) => {
Note.find()
.then((notes) => res.status(200).send(notes))
.catch((err) => res.status(400).send(err));
});
app.post('/notes', (req, res) => {
const body = req.body;
const note = new Note({
name: body.name,
text: body.text
});
note.save(note)
.then((note) => res.status(201).send(note))
.catch((err) => res.status(400).send(err));
});
module.exports = app;
подскажите, пожалуйста, как мне удалить коллекцию mongodb перед модульным тестом?
Заранее спасибо