Check out a free preview of the full Serverless with AWS Lambda course

The "Testing Lambdas" Lesson is part of the full, Serverless with AWS Lambda course featured in this preview video. Here's what you'd learn in this lesson:

Scott discusses testing lambda functions using Jest and SuperTest.

Preview
Close

Transcript from the "Testing Lambdas" Lesson

[00:00:03]
>> Scott Moss: The next thing we're gonna do is we're gonna write some tests. So because what good is all this if we're not writing tests?
>> Scott Moss: I'm gonna checkout to lesson 4. And basically, lesson 4 is like the same thing as lesson 3 solution. And leading up to what I was saying about me abstracting this app out is so that I can write tests for it.

[00:00:24]
This is pretty much what you're gonna be doing. So there's a couple ways you can test this. If you think about it, you can just forget about lambda, right? Just forget about it, you're just testing an express out. And at that point, you could test this however you want, this is an express out.

[00:00:38]
But then if you're like, well actually, yeah, I wanna test what's happening in lambda and see what's going on here. Let me get this to go away. I wanna test this whole thing. You could do that too, but what good is that gonna do? Really, it's just testing some library that you downloaded.

[00:00:55]
What's the point of doing that, at least in this example? So I don't really recommend testing that. And also It's a good practice, and I talked about this yesterday in the other course, that when you create these functions like that, you just don't put stuff in this function that you can't test, right?

[00:01:13]
So for instance, let's get rid of this. Let's just get rid of this for now. And I just write my own function here.
>> Scott Moss: And I got the event, the context. And then I decide to come in here and I'm gonna make another function. How are you gonna test this function?

[00:01:36]
Can you tell me how you're gonna test this function? You can't test this function. You can only test that it ran like testing this function. This function was declared inside of handler so there's no way, you don't have access to it. You can't just say, let me grab function two and test it.

[00:01:53]
You can't, it's stuck in this function so the only way that you could ever test it is if you were to call it in here at one point and then you test handler. And then find some way to, I mean, what you would have to do is you would expect this function to do some type of mutation and then hopefully that mutation is returned by this handler.

[00:02:13]
And then you'd check the mutation for a value, that is the worst way to test something, that's really bad. Just don't do it. Or unless you're like, I'm gonna put some state up here again and I will mutate that state down here. You're like man and then you're gonna test this state to see if it changed.

[00:02:29]
That's really bad, so don't do that. You won't be able to test anything and you're gonna drive yourself crazy. So what you should do instead is one, don't declare things like that in your functions and instead declare other functions and use them inside of this function. And you can just test them individually, and everything's good to go.

[00:02:50]
So that's what I recommend doing which is why I have this set out here. Everybody follow me on that? It's just good testing, in general. And I don't know how that applies to other languages, but in JavaScript that's definitely the truth. So as far as writing tests, what you can do is we'll just make a new file in here and we'll call this api.spec.js.

[00:03:12]
We're gonna be using jests for testing, which should already be set up in the package.json. And it's just a simple command, jest. There's some other options we can pass here, but we're not using databases and we're not doing anything like that. So we can just use the default jest command.

[00:03:28]
Default, it'll look for a spec file and run it for us. We don't have to do anything. And luckily for us, jest is basically Mocha, but better. So if you know Mocha, I'm sorry, it's basically Jasmine, but better. But then Jasmine is also just Mocha cuz Mocha came from Jasmine.

[00:03:42]
So they're all the same. So you can just come in here and you just write some text. So I'm gonna say describe API. And just to see if this is working, I'm gonna say test. Instead of saying it, I'm gonna say test which is what jest likes to use, but I believe you can still say it.

[00:04:02]
>> Scott Moss: And then expect is built into jest, so I can just say expect and I'm just gonna say expect (1) toBe(1). And we'll see if that runs, just to make sure our test isn't broken.
>> Scott Moss: Yeah, looks like it should be okay. So what we're going to do is go into this API.

[00:04:26]
I want you to think of a strategy of how you want to test this. My recommendation is test this the express way. Use an express type thing to test this and I can give you a hint of what you could use. If you look at the package at JSON, there's this, right here, supertest if you use that, you could just look it up.

[00:04:49]
I would use that to test my ExpressUp, it's pretty simple. So I always have some tests and do that and see if you can just get some tests running for this which will be really cool. And it shouldn't take too long, there's no right or wrong here. I just want you to get in the habit of realizing that writing tests for a lambda function is pretty much the same thing as writing a test for anything, if you write it in such a way.

[00:05:13]
You should never be at the point where you're testing the event object and the context object. If you're testing that, it's like, what are you doing? Why are you doing that? You shouldn't even be testing that. Make another function that takes the values from those objects and test those things.

[00:05:27]
You shouldn't be testing the handlers themselves. That's not your job to ensure that the handler is receiving the right arguments. That's AWS's job, so you shouldn't be testing those. If your handler's not receiving the right argument, there's nothing you can do about it and passing tests isn't gonna help you.

[00:05:42]
Your function is gonna fail because AWS messed up, so there's no point in testing that.

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