1-1 Throw an Error with a Simple Test in JavaScript

Throw an Error with a Simple Test in JavaScript

In this lesson, we’ll get the most fundamental understanding of what an automated test is in JavaScript. A test is code that throws an error when the actual result of something does not match the expected output.

Tests can get more complicated when you’re dealing with code that depends on some state to be set up first (like a component needs to be rendered to the document before you can fire browser events, or there needs to be users in the database). However, it is relatively easy to test pure functions (functions which will always return the same output for a given input and not change the state of the world around them).


提取码: kewv

观看视频

Code

Transcript

We have a bug in the sum function. It's doing subtraction instead of addition. We could easily fix this, but let's go ahead and write an automated test that can make sure that this bug never surfaces again.

An automated test in JavaScript is code that throws an error when things are unexpected. Let's do that. Let's get our result from the sum of three and seven.

simple.js


const result = sum(3, 7)

We will say our expected is 10. We can say if the result is not equal to the expected value, then we can throw a new error that says, "Result is not equal to expected."


const expected = 10

if (result !== expected){

    throw new Error(`${result} is not equal to ${expected}`)

}

To run this, we can run node lessons/simple.js.

We will get our error, "-4 is not equal to 10."

image

If we replace this with addition and run that again, our script passes without throwing errors. This is the most fundamental form of a test.


const sum = (a, b) => a + b

Let's go ahead and add another test for subtract. I am going to change this from const to let.


const subtract = (a, b) => a - b

We will say, result is now equal to subtract of seven and three, and our expected is now equal to four."


result = subtract(7, 3)

expected = 4

We will just copy and paste this here.


if (result !== expected){

    throw new Error(`${result} is not equal to ${expected}`)

}

Save that and run our script again. It passes without error. In review, this is the most fundamental form of a test in JavaScript. It's simply a code that will throw an error when the result is not what we expect.

Let's go ahead and break this again. We'll run that script again. We're getting an error message. The Javascript testing framework is to make that error message as useful as possible so we can quickly identify what the problem is and fix it.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • 这次比上次要成熟多了。爱莲说作为文质兼美的文章,诗歌需要诵读,这则需要读字音,字形,同时需要将课后习题,以往资料的...
    over璞石阅读 255评论 0 0
  • 很久之前就想写一篇binder的文章, 也是总结自己的知识点把。 binder算是android自建的一个框...
    自由人是工程师阅读 392评论 0 0
  • (二) 父亲有一只三层的玻璃杯。 那是一个外来的商人送给爷爷的礼物,爷爷觉得花哨又送给了父亲,这个当时罕见的材质,...
    时无一阅读 336评论 2 1
  • 翻看了qq空间,又刷了微信圈,读着别人的文字,感受着别人的心情。 前几天看了刘震云的《一句顶一万句》...
    桃子_939e阅读 192评论 0 0