I am trying to run my test script using mocha but there is an error of "Reference Error: beforeEach is not defined"
I am trying to run my test script using mocha but there is an error of "Reference Error: beforeEach is not defined"
Mocha 测试套件给出了一个参考错误,它说 "beforeEach is not defined"
我正在尝试 运行 我在 node.js 中使用 mocha 的待办事项应用程序的测试脚本。但是有一个参考错误,它说 "beforeEach is not defined"
const {app} = require('./../server');
const {Todo} = require('./../models/todo');
beforeEach((done) => {
Todo.remove({}).then(() => done());
});
describe('POST /todos', () => {
it('should create a new todo', (done) => {
var text = 'Test todo text';
request(app)
.post('/todos')
.send({text})
.expect(200)
.expect((res) => {
expect(res.body.text).toBe(text);
})
.end((err, res) => {
if (err) {
return done(err);
}
Todo.find().then((todos) => {
expect(todos.length).toBe(1);
expect(todos[0].text).toBe(text);
done();
}).catch((e) => done(e));
});
});
it('should not create todo with invalid body data', (done) => {
request(app)
.post('/todos')
.send({})
.expect(400)
.end((err, res) => {
if (err) {
return done(err);
}
Todo.find().then((todos) => {
expect(todos.length).toBe(0);
done();
}).catch((e) => done(e));
});
});
});
此外,我已经为我的 package.json 文件包含了所有必要的包。
我的Package.json文件如下
{
"name": "todo-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha server/**/*.test.js",
"test-watch": "nodemon --exec 'npm test' "
},
"author": "",
"license": "ISC",
"dependencies": {
"bluebird": "^3.5.3",
"body-parser": "^1.15.2",
"express": "^4.14.0",
"mongodb": "^2.2.5",
"mongoose": "^4.5.9"
},
"devDependencies": {
"expect": "^1.20.2",
"mocha": "^3.0.2",
"nodemon": "^1.10.2",
"supertest": "^2.0.0"
}
}
我刚刚尝试使用您的 package.json
文件通过简单存储库的测试来复制您的问题。我的测试文件
const expect = require('expect');
beforeEach((done) => {
done();
});
describe('just a test', function() {
it('test', function() {
expect(true).toBe(true);
})
});
然后,当运行npm t
时,测试执行成功。
可能您的项目配置有误。
在我看来,您正试图在每次对待办事项应用进行新测试之前清除 MongoDB 数据库中的数据。所以你想找到你的 todo
集合并在下一次测试之前把所有东西都放在那里,如果是
beforeEach(() => {
mongoose.connection.collections.todo
});
通过上面的代码,您直接引用位于数据库中的 todo
集合,并在 todo
.
集合上调用 drop()
函数
beforeEach(() => {
mongoose.connection.collections.todo.drop();
});
如果我满足您的要求是正确的,请告诉我。请记住这是一个异步操作,因此您需要确保在操作完成之前暂停整个测试环境,但您已经知道这一点,因为您已经尝试实施 done
回调。
此外,drop()
将接受这样的回调函数:
beforeEach((done) => {
mongoose.connection.collections.todo.drop(() => {});
});
该函数只会在您完成收集后执行。所以你还必须像这样定义后传递 done()
回调:
beforeEach((done) => {
mongoose.connection.collections.todo.drop(() => {
done();
});
});
此外,当您 运行 摩卡测试时,您会执行 npm run test
。
Mocha 测试套件给出了一个参考错误,它说 "beforeEach is not defined"
我正在尝试 运行 我在 node.js 中使用 mocha 的待办事项应用程序的测试脚本。但是有一个参考错误,它说 "beforeEach is not defined"
const {app} = require('./../server');
const {Todo} = require('./../models/todo');
beforeEach((done) => {
Todo.remove({}).then(() => done());
});
describe('POST /todos', () => {
it('should create a new todo', (done) => {
var text = 'Test todo text';
request(app)
.post('/todos')
.send({text})
.expect(200)
.expect((res) => {
expect(res.body.text).toBe(text);
})
.end((err, res) => {
if (err) {
return done(err);
}
Todo.find().then((todos) => {
expect(todos.length).toBe(1);
expect(todos[0].text).toBe(text);
done();
}).catch((e) => done(e));
});
});
it('should not create todo with invalid body data', (done) => {
request(app)
.post('/todos')
.send({})
.expect(400)
.end((err, res) => {
if (err) {
return done(err);
}
Todo.find().then((todos) => {
expect(todos.length).toBe(0);
done();
}).catch((e) => done(e));
});
});
});
此外,我已经为我的 package.json 文件包含了所有必要的包。
我的Package.json文件如下
{
"name": "todo-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha server/**/*.test.js",
"test-watch": "nodemon --exec 'npm test' "
},
"author": "",
"license": "ISC",
"dependencies": {
"bluebird": "^3.5.3",
"body-parser": "^1.15.2",
"express": "^4.14.0",
"mongodb": "^2.2.5",
"mongoose": "^4.5.9"
},
"devDependencies": {
"expect": "^1.20.2",
"mocha": "^3.0.2",
"nodemon": "^1.10.2",
"supertest": "^2.0.0"
}
}
我刚刚尝试使用您的 package.json
文件通过简单存储库的测试来复制您的问题。我的测试文件
const expect = require('expect');
beforeEach((done) => {
done();
});
describe('just a test', function() {
it('test', function() {
expect(true).toBe(true);
})
});
然后,当运行npm t
时,测试执行成功。
可能您的项目配置有误。
在我看来,您正试图在每次对待办事项应用进行新测试之前清除 MongoDB 数据库中的数据。所以你想找到你的 todo
集合并在下一次测试之前把所有东西都放在那里,如果是
beforeEach(() => {
mongoose.connection.collections.todo
});
通过上面的代码,您直接引用位于数据库中的 todo
集合,并在 todo
.
drop()
函数
beforeEach(() => {
mongoose.connection.collections.todo.drop();
});
如果我满足您的要求是正确的,请告诉我。请记住这是一个异步操作,因此您需要确保在操作完成之前暂停整个测试环境,但您已经知道这一点,因为您已经尝试实施 done
回调。
此外,drop()
将接受这样的回调函数:
beforeEach((done) => {
mongoose.connection.collections.todo.drop(() => {});
});
该函数只会在您完成收集后执行。所以你还必须像这样定义后传递 done()
回调:
beforeEach((done) => {
mongoose.connection.collections.todo.drop(() => {
done();
});
});
此外,当您 运行 摩卡测试时,您会执行 npm run test
。