摩卡:异步与同步 [英] Mocha: async vs sync

查看:116
本文介绍了摩卡:异步与同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Mocha文档,"Mocha测试是按顺序运行的",这意味着按定义的顺序进行.

According to Mocha documentation, "Mocha tests run serially" which means in the order they are defined.

我的问题是:是什么使异步(带有完成的回调)测试与 sync 不同?

My question is: what makes async (with done callback) tests different than sync?

推荐答案

您通过传递给it调用带有参数(通常名为done)的函数来告诉Mocha测试是异步的.然后,Mocha将使用第一个参数调用此函数,该参数是一个回调,您必须调用该回调来告诉Mocha测试已结束.

You tell Mocha that a test is asynchronous by passing to the it call a function that takes an argument (traditionally named done). Mocha will then call this function with a first argument which is a callback that you must call to tell Mocha the test is over.

异步测试和同步测试之间的唯一区别是,对于异步测试,Mocha将在继续进行下一个测试之前等待done回调被调用.如果测试被认为是同步的,那么当您传递给it的函数返回时,Mocha将继续进行下一个测试.如果Mocha也是通过异步测试 来做到这一点,那么它将无法将未处理的异常与适当的测试相关联.

The only difference between an asynchronous test and a synchronous one is that for an asynchronous test Mocha will wait for the done callback to be called before moving on to the next test. If the test is deemed to be synchronous, then Mocha will move on to the next test as soon as the function you passed to it returns. If Mocha were to do this with asynchronous tests too then it would not be able to associate unhandled exceptions with the appropriate test.

这篇关于摩卡:异步与同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆