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

The "Testing with SuperTest" 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 sets up testing with SuperTest, which allows Express requests and retrieves results without having to start an Express server.

Preview
Close

Transcript from the "Testing with SuperTest" Lesson

[00:00:00]
>> Scott Moss: Okay, so what I have is basically this. So I'm using supertest which given an express application will setup a request function or object for you that allows you to actually, run the express requests and get back the result. So it's basically allowing you to execute the controllers on your Express app without actually starting an Express server, right?

[00:00:28]
Because Express, like I said, it's two parts. There's a routing middleware and controller side, and then there's the server side. You don't need to start a server to tie into the other side, and that's what supertest is doing. So we're actually starting a server here. We're just tieing into the event like nature of express.

[00:00:45]
Which basically says if you hit this route, fire this event. Which runs this middle and this controller, that's what we're doing. So if I go to one of my APIs which is slash todos' I know I'm getting back two things on the data property. So if I go to the spec, you could see the that's exactly what I'm doing.

[00:01:04]
I expect to get 200, I expect that data property have two, so if I run this.
>> Scott Moss: We should get back,
>> Scott Moss: okay, and we do. Did anybody write their test differently? Not so much as the assertion for just like, how they set the test up that they use something else other than supertest.

[00:01:28]
I'm just interested on how you've done that and what you did.
>> Scott Moss: Everybody used supertest? Do you able to use it? Yeah, sometimes it's [COUGH] pretty easy. It's created by the same person that made Express, so I would imagine it would work very well with it, cool. And I mean, that's basically for testing.

[00:01:48]
There's really not much more to it. But actually, it all depends on how you write your lambda. In this case, we wrote a lambda using Express, so supertest was A-okay. But if we decided to write, you know, just Vanilla JavaScript, then it would be up to you to extract out your code.

[00:02:03]
Kind of like how I was showing you earlier. So you can easily test it in here. But when you run your test, the only thing you would have to be aware of is, if there's specific AWS things that you would need in your test. But that's true with any server, so you would just mark that up.

[00:02:22]
So there shouldn't be any difference here. At the end of the day you're just testing your function that has three inputs. And then as far as the event goes, you can go to AWS and grab the event that you need to test. And you can pass that into your test as well.

[00:02:37]
Or if you're calling lambda from another lambda and have a custom event, you can test that as well because you're the one calling it. So there's really is no reason why you can't test your lambdas'. It's no different than testing anything else. Any questions on testing with lambdas' and how that works?

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