升级后,Mocha甚至无法运行这里的简单测试代码
const assert = require('assert');
it('should complete this test', function (done) {
return new Promise(function (resolve) {
assert.ok(true);
resolve();
})
.then(done);
});
Run Code Online (Sandbox Code Playgroud)
我从这里拿了这个代码
我明白它现在抛出异常 Error: Resolution method is overspecified. Specify a callback * or * return a Promise; not both.
但是如何让它发挥作用?我不明白.我有
node -v 6.9.4
mocha -v 3.2.0
Run Code Online (Sandbox Code Playgroud)
如何运行此代码现在采用新的正确格式?
我正在尝试为数据库进行单元测试.
以下是seed.js文件:
.......
const app = require('./app')
const db = app.get('db')
const saveUsersToDB = (done) => {
db.User.bulkCreate(users)
.then(() => (done))
}
module.exports = {saveUsersToDB};
Run Code Online (Sandbox Code Playgroud)
我的app.test.js档案:
.......
const expect = require('expect')
const request = require('supertest')
const {saveUsersToDB} = require('./seed/seed');
before(saveUsersToDB)
Run Code Online (Sandbox Code Playgroud)
当我运行下面的测试是我得到的错误:
Express listening on port 3000!
1) "before all" hook: saveUsersToDB
0 passing (2s)
1 failing
1) "before all" hook: saveUsersToDB:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if …Run Code Online (Sandbox Code Playgroud) 我正在使用摩卡进行测试
it('should allow a POST to /users', async function () {
const res = await request.post('/users').send(firstUserBody);
expect(res.status).to.equal(201);
expect(res.body).not.to.be.empty;
expect(res.body).to.be.an('object');
expect(res.body.id).to.be.a('string');
firstUserIdTest = res.body.id;
});
Run Code Online (Sandbox Code Playgroud)
但我有一个错误
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Run Code Online (Sandbox Code Playgroud)
如果我使用 done() 执行此操作,该函数不是异步的,但它应该是
it('should allow a POST to /users', async function (done) {
const res = await request.post('/users').send(firstUserBody);
expect(res.status).to.equal(201);
expect(res.body).not.to.be.empty;
expect(res.body).to.be.an('object');
expect(res.body.id).to.be.a('string');
firstUserIdTest = res.body.id;
done();
});
Run Code Online (Sandbox Code Playgroud)
我应该怎么办 ?
mocha.js ×3
node.js ×3
ecmascript-6 ×1
es6-promise ×1
javascript ×1
supertest ×1
typescript ×1