承诺没有在nodejs中使用Mocha运行

时间:2017-01-30 21:19:55

标签: node.js promise mocha bluebird

我有一些使用promises的代码,但是在通过Mocha运行时无法运行。我已将其简化为基本要素:

process.env.NODE_ENV = 'test';

const Promise = require('bluebird');

console.log('zzzz IN');
Promise.resolve('xxx').then(function(val) {
    console.log('[normal]', val);
}).catch(function(error) {
    console.log('[error]', error);

})
console.log('zzzz OUT');

通过node test/index.js运行时,我得到:

zzzz IN
zzzz OUT
[normal] xxx

但是通过摩卡:

> my-server@0.0.1 test /Users/ajmas/Development/mocha-and-promise
> eslint lib && mocha --timeout 10000

zzzz IN
zzzz OUT


0 passing (0ms)

这是Mocha中的问题还是我配置的方式?

的package.json:

{
    "name": "my-server",
    "version": "0.0.1",
    "description": "Mocha and Promises test case",
    "main": "lib/main.js",
    "scripts": {
        "start-dev": "NODE_ENV=dev node test/index.js",
        "start-dev-debug": "DEBUG=express:* npm run start-dev",
        "start": "node lib/main.js",
        "test": "eslint lib && mocha --timeout 10000"
    },
    "engines": {
        "node": ">=6.7.0"
    },
    "dependencies": {
        "bluebird": "^3.4.6"
    },
    "devDependencies": {
        "chai": "^3.5.0",
        "chai-http": "^3.0.0",
        "eslint": "^3.8.1",
        "eslint-config-standard": "^6.2.1",
        "eslint-plugin-promise": "^3.3.0",
        "eslint-plugin-standard": "^2.0.1",
        "mocha": "^3.2.0"
    }
}

在MacOS X 10.12.2上使用节点6.7.0运行。还试过了' bluebird',' promise'和本机的Promise,但每个都有相同的行为。

顺便说一句,这段代码是我整合测试的应用程序的一部分,但由于没有完成承诺,我无法在Mocha中启动服务器。

1 个答案:

答案 0 :(得分:0)

原来它可行,但你需要把代码放在'它'函数调用,这样:

it('Run the promise, function(done) {
    Promise.resolve('xxx').then(function(val) {
        console.log('[normal]', val);
    }).catch(function(error) {
        console.log('[error]', error);
    })
})
相关问题