【问题标题】:Promises not running with Mocha, in nodejs在 nodejs 中,Promise 不能与 Mocha 一起运行
【发布时间】:2017-06-16 04:21:42
【问题描述】:

我有一些使用 Promise 的代码,但是通过 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 中的问题还是我的配置方式?

包.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 中启动服务器。

【问题讨论】:

    标签: node.js promise mocha.js bluebird


    【解决方案1】:

    事实证明它有效,但您需要将代码与“it”函数调用一起放置,这样:

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

    【讨论】:

    猜你喜欢
    • 2015-11-29
    • 2016-03-15
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多