mercredi 22 janvier 2020

Cant clear database after tests are run on Mocha and Chai using mongodb with mongoose in my node.js app

I will include both the files that are needed for the same the main issue is that after every test I run I want no more entries to be added in my database and it should be cleaned after the test are done so I used aftereach() but I suppose it is not working because of something wrong I did, can you help me with that.

here is my test.js

process.env.NODE_ENV = 'test';
// const mongoose = require('mongoose');
const chai = require('chai');
const chaiHttp = require('chai-http');
const Task = require('../config/model');
const server = require('../index');

const { expect } = chai;

chai.use(chaiHttp);

describe('Task', (done) => {
  afterEach(() => {
    Task.crud.drop();
    done();
  });
});

// Test Get Tasks

describe('/GET tasks', () => {
  it('it should GET all the tasks', async () => {
    const res = await chai.request(server)
      .get('/task');
      // .end((err, res) => {
    expect(res).to.have.status(200);
    // expect(res.body).to.be.a('array');
    // done(err);
  });
});

describe('/Post tasks', () => {
  it('should Post the task', async () => {
    const taskPost = {
      task: 'run as fast as possible you idiot',
    };
    const res = await chai.request(server)
      .post('/task')
      .send(taskPost);
      // .end((err, res) => {
    expect(res).to.have.status(200);
    //  done();
  });
});

describe('/GET/:ID', () => {
  it('should Get the task by ID', async () => {
    const tasks = new Task({ task: 'The Lord of the Rings' });
    const task = await tasks.save();
    const res = await chai.request(server)
      .get(`/task/${task.id}`)
      .send(task);
      // .end((error, res) => {
    expect(res).to.have.status(200);
    //  done();
    // });
  });
});

describe('/PUT/:ID task', () => {
  it('it should UPDATE a task given the id', async () => {
    const tasks = new Task({ task: 'The Chronicles of Narnia' });
    const task = await tasks.save();
    const res = await chai.request(server)
      .put(`/task/${task.id}`)
      .send({ task: 'The Chronicles of Sarvesh' });
    // .end((error, res) => {
    expect(res).to.have.status(200);
    // });
  });
});

and the files that is in /config/model

const mongoose = require('mongoose');

const Schema = new mongoose.Schema({
  task: {
    type: String,
    required: true,
  },

});

module.exports = mongoose.model('crud', Schema, 'crud');

Aucun commentaire:

Enregistrer un commentaire