未加星标

Testing Promises with Mocha

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

If you test javascript promises with Mocha, there are several styles you can use to write your tests. If you follow the Mocha docs on testing asynchronous code you risk writing ‘evergreen’ tests. Evergreen tests never fail, even if your code is broken. That is really bad. What is the safest way to write async tests with Mocha?

Testing promises

When you assert on the result of a promise, there are several failure modes you want to test for. This is what can go wrong:

Your assertion on the result of the promise can fail. The promise you test can reject unexpectedly. Using the ‘done’callback

The first strategy suggested in the mocha documentation is using the ‘done’ callback. This is an extra argument to the callback in the ‘it’ . You call it after the last assertion on the result of the promise:

it('resolves', (done) => {
const resolvingPromise = new Promise( (resolve) => {
resolve('promise resolved');
});
resolvingPromise.then( (result) => {
expect(result).to.equal('promise resolved');
done();
});
}

There are two problems with this style: you won’t see the cause of your test failures and nothing is holding you back from writing an evergreen test.

Timeout instead of failure

When your assertion fails, or when the promise rejects, you will get this failure:

Error: timeout of 2000ms exceeded. Ensure done() is being called.

Normally, if code throws an error, Mocha catches the error and displays it as the reason for a failing test. If you throw an Error inside the ‘then’, the error will be swallowed by the promise, which will then reject. Mocha never hears about the error, stops waiting after 2 seconds and throws a timeout.

Forget ‘done’ and you have an evergreen test

If you forget to add ‘done’ as an argument to ‘it’, you will not get a “done is not defined” . Remember, an error inside a then will just cause the promise to reject, which has no effect at all. Mocha never knows it has to wait for ‘done()’. Your test will now be evergreen. It will never fail, even if your code is broken.

Use.then(done,done)

You can fix the timeout problem and prevent unintentional evergreen tests if you use this style:

it('resolves', (done) => {
resolvingPromise.then( (result) => {
expect(result).to.equal('promise resolved');
}).then(done, done);
});

Now you are not calling ‘done’ inside the then, but you chain a .then(done,done) after the promise. The first ‘done’ will signal the completion of a successful test to mocha. The second done is called in case of a promise rejection. This is the safest style to use with the ‘done’ callback:

Your test failures are caught and displayed. When you forget to supply ‘done’ as argument, you will get done is not defined. If you forget to end your test with.then(done,done), mocha warns you about a missing ‘done’.

Note: It would be nicer to put the ‘done’ in the promise’s finally but at the moment that is not yet included in babel-polyfill .

Returning apromise

The.then(done,done) style is still verbose. Mocha gives us another option to test asynchronous code. Just return a promise from your ‘it’ callback:

it('resolves', () => {
return resolvingPromise.then( (result) => {
expect(result).to.equal('promise resolved');
});
});

Although this style is the most common, certainly combined with chai-as-promised , I would recommend against it. When you forget to type ‘return’, your test is evergreen and there is no safeguard to warn you.

The real winner is async-await

Next to the.then(done,done) style, the best way to test promises is with async await . This is an ES7 feature you can use today with babel. Declare a function ‘async’ and ‘await’ your promise. This style is just as safe as the.then(done,done) style, but it’s much easier to read since there is no more chaining and nesting of then.

it('assertion success', async () => {
const result = await resolvingPromise;
expect(result).to.equal('promise resolved');
});

You can find a working examples of the code in this blogpost in this github repo .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: Java
分页:12
转载请注明
本文标题:Testing Promises with Mocha
本站链接:http://www.codesec.net/view/524224.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(43)