Check out a free preview of the full JavaScript Testing Practices and Principles course

The "Coding an Assertion Library" Lesson is part of the full, JavaScript Testing Practices and Principles course featured in this preview video. Here's what you'd learn in this lesson:

Kent writes the solution to the assertion library exercise in pure JavaScript and goes over critical issues revealed in the exercises and instructs the audience to continue to the next exercise.

Preview
Close

Transcript from the "Coding an Assertion Library" Lesson

[00:00:00]
>> So let's work through this. So our objective here is to create a function that accepts an actual value, returns an object that has a 2b function on it that accepts the expected value and throws an error if there's something different between the two. So I'll make our function expect, and this is going to accept our actual And then this is going to return to be, and that'll be a function that is going to expect our expect- or it's going to receive our expected value.

[00:00:31]
And then if there's something wrong, we'll just move this down here. So we'll rename this result to actual And so now we can use this instead of our imperative code. We'll also want to do sorry, move down the if statement as well. And then this'll be actual. So we expect the actual to be expected, if it's not then it'll throw the error.

[00:01:00]
Okay so then we can use this with expect Results expected, or sorry, to be expected. And we'll do the same thing down here for subtract. And we run node 2.todo and we'll get that same error. So that the difference here Actually, it's kind of slight if we look at node one.

[00:01:28]
The solution for the first one. You're going to see the error message indicates that this line that this error was thrown is on line 10. Now this is on line 34. And the significance of that is the error will now always be thrown on this line 34. And there's no way for you to know was this the subtract that failed or is this the sum that failed?

[00:01:53]
We can maybe look at the stack trace a little bit. So now we see, okay, that's line 10, line 34. It's just not readily apparent to us when we look at this error message what it was that broke. And so that's what a testing framework can do for us, is it can improve our error messages so that things are a little bit more clear what exactly is failing.

[00:02:15]
The other problem is, I could go in here into our math module. Sorry, I forgot to mention that we extracted those functions, because normally you don't have your source and your test in the same file. But yeah so if broke the subtract function and I ran this again, I have indication that the subtract function is also broken.

[00:02:35]
And in fact if the sum function actually used the subtract function and the subtract function was actually the broken one, I still don't really have any indication of that because it's only running one test. And so, there are two problems here. The message isn't totally clear, and it's not running all of our tests in the file.

[00:03:01]
That's what we're going to work on next, solving that problem with a testing framework. With most testing frameworks, you'll have a describe block. You'll have IT block, whatever. I actually, we can talk about this later, but I generally try to avoid describe blocks. I try to avoid nesting my test a lot.

[00:03:24]
And so, with Jest you can use IT or test. They're aliases of one another. So we'll be writing a test function. And via actually I didn't give you an example. Here's all you just write it here. So it should the API should be sending like this. Test title and then a call back function gradual my assertions.

[00:03:45]
Well that actually the format of assertion will be something like Arrange, act, and assert. So whatever the case may be, this callback function, if there's a problem, it should throw an error, okay? So we'll pretty much just wrap this code inside of that callback function. So that's what you're gonna write in this one.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now