beforeEach不允许其他测试条件运行

时间:2019-03-28 04:50:25

标签: node.js mongoose mocha supertest

当我添加beforeEach时,

测试条件不执行。如果我删除了beforeEach块,则执行测试条件;如果我删除了测试条件,则beforeEach成功运行。但是当我运行它们时出现错误

server-test.js

const request = require("supertest");
const expect = require("expect");
const { ObjectID } = require("mongodb");

var { Todo } = require('./../models/todo');
var { app } = require('./server');

const todos = [{
    _id: new ObjectID(),
    task: 'first task'
}, {
    _id: new ObjectID(),
    task: 'second task',
    completed: true
}]


beforeEach((done) => {
    Todo.deleteMany({})
        .then(() => {
            console.log("deleted existing todo");
            return Todo.insertMany(todos)
        })
        .then(() => {
            console.log("inserted todo");
            return done();
        })
        .catch((err) => {
            console.log("error in inserting todo");
            return done();
        })
})


describe('create new Todo', () => {
    it('todo created successfully', (done) => {
        var task = 'new task';

        request(app)
            .post('/todo')
            .send({ task })
            .expect(200)
            .expect((res) => {
                expect(res.body.task).toBe(task)
            })
            .end((err, res) => {
                if (err) {
                    return done(err);
                }
                Todo.find({ task: task })
                    .then((doc) => {
                        // if(!err){
                        expect(doc.length).toBe(1);
                        expect(doc[0].task).toBe(task);
                        done();
                        // }
                    })
                    .catch((err) => {
                        done(err);
                    })
            })
    })

})

server.js

require('./../config/config')

const express = require('express');
const bodyParser = require('body-parser');
const {ObjectID} = require('mongodb')
const _ = require('lodash');

var mongoose = require("../db/setup");
var {Todo} = require('../models/todo');
var {User} = require('../models/user');
var {authenticate} = require('./../middlewares/authenticate');

var app = express();
var port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(express.json());


app.post('/todo', (req, res) => {
    var newtodo = new Todo({
        completed: req.body.completed,
        completedAt: req.body.completedAt,
        task: req.body.task
    })

    newtodo.save()
        .then((doc) => {
            res.send(doc);
        }, (err) => {
            res.status(400).send(err);
        })
})

package.json

"dependencies": {
    "bcrypt": "^3.0.5",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.3",
    "crypto-js": "^3.1.9-1",
    "express": "^4.16.4",
    "jsonwebtoken": "^8.5.1",
    "lodash": "^4.17.11",
    "mongoose": "^5.4.19",
    "validator": "^10.11.0"
  },
  "devDependencies": {
    "expect": "^24.5.0",
    "mocha": "^6.0.2",
    "nodemon": "^1.18.10",
    "supertest": "^4.0.0"
  }

创建新的待办事项

删除现有待办事项

1) "before each" hook for "todo created successfully"

0传球(2s)

1个失败

1)“成功创建待办事项”的“之前”钩子:

 Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

插入待办事项

0 个答案:

没有答案