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

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.

主站蜘蛛池模板: 将乐县| 唐海县| 宁河县| 九江县| 泸水县| 花莲县| 北流市| 安乡县| 新蔡县| 黄大仙区| 新营市| 乌海市| 宝丰县| 昌宁县| 禹州市| 榆林市| 闽侯县| 阿拉尔市| 聂拉木县| 新平| 辽中县| 利辛县| 桑植县| 阳西县| 广河县| 沽源县| 神池县| 唐山市| 班玛县| 曲周县| 镇江市| 宜阳县| 姜堰市| 正镶白旗| 庄浪县| 高雄市| 南岸区| 盐津县| 大悟县| 浮山县| 清原|