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

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.

主站蜘蛛池模板: 海伦市| 兴业县| 梁山县| 桦南县| 天津市| 林口县| 历史| 红河县| 郁南县| 平湖市| 康保县| 民乐县| 邻水| 和龙市| 威信县| 青阳县| 海门市| 台中县| 钦州市| 瑞丽市| 桐乡市| 乌兰浩特市| 阿克苏市| 博乐市| 资阳市| 抚州市| 商洛市| 吕梁市| 兴国县| 遂溪县| 兴义市| 娱乐| 南城县| 邹平县| 榆树市| 三明市| 肥西县| 灌南县| 东阳市| 资溪县| 临朐县|