简体   繁体   English

在Node.js中没有与Mocha一起运行的承诺

[英]Promises not running with Mocha, in nodejs

I have some code which makes use of promises, but it fails to run when run through Mocha. 我有一些利用Promise的代码,但是在通过Mocha运行时无法运行。 I have simplified it to the essentials: 我已将其简化为要点:

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');

when run via node test/index.js I get: 通过node test/index.js运行时,我得到:

zzzz IN
zzzz OUT
[normal] xxx

but via mocha: 但是通过摩卡:

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

zzzz IN
zzzz OUT


0 passing (0ms)

Is this an issue in Mocha or in the way I have configured things? 这是Mocha的问题还是我配置事物的方式?

Package.json: 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"
    }
}

Running with node 6.7.0 on MacOS X 10.12.2. 在MacOS X 10.12.2上以节点6.7.0运行。 Have also tried with ' bluebird ', ' promise ' and native Promise, but same behaviour with each. 还尝试过使用“ bluebird ”,“ promise ”和原生Promise,但两者的行为相同。

BTW this code is meant to be part of the application I am integration testing, but because no promises are completing I can't launch the server from within Mocha. 顺便说一句,该代码旨在成为我正在集成测试中的应用程序的一部分,但是由于无法兑现承诺,因此无法从Mocha内部启动服务器。

Turns out it works, but you need to put the code with an 'it' function call, such that: 事实证明它是可行的,但是您需要使用“ it”函数调用来放置代码,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM