相关疑难解决方法(0)

为什么我会收到"错误:解决方法被过度指定"?

升级后,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)

如何运行此代码现在采用新的正确格式?

mocha.js node.js

32
推荐指数
3
解决办法
8792
查看次数

摩卡:超出2000毫秒的错误超时

我正在尝试为数据库进行单元测试.

以下是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)

javascript mocha.js node.js ecmascript-6 es6-promise

11
推荐指数
3
解决办法
3万
查看次数

对于异步测试和挂钩,我应该如何处理错误,确保调用“done()”;如果返回 Promise,请确保它得到解决

我正在使用摩卡进行测试

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 node.js typescript supertest

3
推荐指数
1
解决办法
5866
查看次数