官术网_书友最值得收藏!

Checking for null or not

There is a school of thought that checking for null in tests is redundant and can be removed all together. In other words, the second-to-last test we wrote would become this:

it('renders a label for the first name field', () => {
render();
expect(labelFor('firstName').textContent).toEqual('First name');
});

In the original test, the first not null expectation is a guard expectation. The test will still function even without this line, since the second expectation will throw an exception if the value of label is null.

The run-time difference between the two variants of the test is subtle. The original test will fail with a test failure, not an exception. In this second version, your test will fail with an exception, not a test failure.

In some languages and test environments, exceptions can be more painful than test failures. Ultimately, which style you choose is a matter of personal preference. In this book, I'll continue to use guard expectations for completeness, but I'd encourage you to experiment with both styles.

主站蜘蛛池模板: 虎林市| 平陆县| 句容市| 饶阳县| 安义县| 花莲县| 塘沽区| 福建省| 资兴市| 汉中市| 客服| 南汇区| 霞浦县| 介休市| 常熟市| 济南市| 古蔺县| 米泉市| 苏州市| 特克斯县| 大丰市| 吉水县| 汪清县| 横山县| 德庆县| 洮南市| 四子王旗| 斗六市| 闽侯县| 甘谷县| 晋江市| 腾冲县| 浏阳市| 榆树市| 弥渡县| 建阳市| 高阳县| 江安县| 黑龙江省| 监利县| 金秀|