@ bigtest/mocha (英语) 
用于同步状态测试的同步 Mocha 函数
概要
import { describe, beforeEach, it } from '@bigtest/mocha';
import { expect } from 'chai';
describe('clicking my button', () => {
beforeEach(() => $button.click());
// repeatedly asserts until passing
it('shows a loading indicator', () => {
expect($button.className).to.include('is-loading');
});
// repeatedly asserts it is passing until the timeout
it.always('does not navigate away', () => {
expect(app.location).to.equal('/')
}).timeout(200);
})
同源请求
通常,当测试同步状态时(例如制取的内容) 您的测试需要在正确的时间进行 在正确的上下文中执行。 如果你的测试也成功 很快,他们就会失败,如果他们跑得太慢, 那么,你只是有 慢测试.
汇合在一个国家中,意味着反对一个国家,直到 ,在这种情况下,你已成功聚合 那个州!
此软件包使用
@bigtest/convergence
重复陈述,直到他们通过或暂停
过期。 以这种方式进行测试 让他们能通过瞬间
达到理想状态。 这导致了非常快的测试 当
测试一个同步的东西.
读取 @bigtest/convergence 文档在
趋同
更多的信息,为什么在理想状态上聚合比
试着好好把握时间.
怎么会这样?
大部分情况下,你写测试的方式 与你一样
常与摩诃迦叶作作是书. 唯一的区别是这个
包包 Mocha 软件包 it 在交汇器中
您使用 it 成为一致的说法
允许您轻松测试同步状态 .
此套件还包了 Mocha 钩子 before, (中文(简体) ), after, (中文(简体) ),
beforeEach,以及 afterEach 自动支持时间和
运行返回 Convergence 来自 @bigtest/convergence。 。 。 .
写入测试
因为一致的断言是反复运行 直到他们通过,它 实值 高度建议 那个你 别 执行任何副作用 在你的断言。 这将导致你的副作用运行 多次,也许甚至数百次.
为此,你应该把副作用生成代码保存在 钩子和 从你的说法. 这些"纯粹的断言"也 帮助您的测试更加清晰易读.
: no entry sign:不这样做:
describe('my button', () => {
it('shows a loading indicator after clicking', () => {
// this will be called every time the assertion runs
$button.click();
// it might take a few milliseconds for any side-effects to
// happen, so this might fail the first time and cause this entire
// assertion to run again, thus clicking the button again
expect($button.className).to.include('is-loading');
});
});
: white check mark:做到这一点:
describe('clicking my button', () => {
// keep side-effects inside hooks
beforeEach(() => $button.click());
// a pure assertion has no side-effects; even if it fails, it can
// be run again and again without consequence
it('shows a loading indicator', () => {
expect($button.className).to.include('is-loading');
});
});
讨人喜欢 未发生
另一种常见的情况是断言 没有 发生了。 如果你通常要测试这个(或者甚至用 以上趋同的断言) 测试可能通过 在副作用出现之前就成功.
在这些情景中,你想在状态遇到一个 期望 特定期间。换句话说,"如果 在X 时间长度上,此测试依然正确 "即将经过"
@bigtest/mocha 提供 it.always 就是这个方法 这个
方法将贯穿整个期间
确保它永远不会失败。 当断言失败时,测试
失败。 如果这个断言从未失败过,它就会在紧接着
暂停期.
describe('clicking my button', () => {
beforeEach(() => $button.click());
// the default timeout for it.always is 100ms
it.always('does not navigate away for at least 1 second', () => {
expect(app.location).to.equal('/');
}).timeout(1000);
});
牵引钩
有时你可能试图执行同步任务以发现它失败
由于未达到预想状态。 举个例子,你不能
如果DOM中不存在,请单击按钮。 你可以用这个
@bigtest/convergence 与这些州汇合并返回
在你的钩子内会合 钩子由
@bigtest/mocha 将自动设置超时并返回运行
Convergence 实例.
describe('clicking my button', () => {
// @bigtest/mocha will wait for a returned Convergence to converge
// before continuing with the assertions
beforeEach(() => new Convergence()
.once(() => expect($button).to.exist)
.do(() => $button.click()));
it('shows a loading indicator', () => {
expect($button.className).to.include('is-loading');
});
});
看看这个 @bigtest/convergence API 密码
文档
与《公约》 Convergence 课.
暂停测试
暂停测试在调试时非常有用. 它允许你
在测试的关键时刻调查您的申请
套间。 莫查没有停止测试的说服力,但
@bigtest/mocha 通过提供 it.pause
方法。 它通过设置当前超时到 0 并赐予莫查
永远无法解脱的诺言 这有效地暂停了整个
套房直到您删除 it.pause 并重新开始测试.
describe('clicking my button', () => {
beforeEach(() => $button.click());
// if the class is never set, this test will fail; it.pause allows
// us to investigate the app at this point in the test suite
it.pause('shows a loading indicator', () => {
expect($button.className).to.include('is-loading');
});
});