Check out a free preview of the full REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course:
The "Solution: Testing" Lesson is part of the full, REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course featured in this preview video. Here's what you'd learn in this lesson:

Scott codes out tests for a GraphQL mutation and resolver. - https://github.com/FrontendMasters/api-design-node-v2/tree/lesson-14-solution

Get Unlimited Access Now

Transcript from the "Solution: Testing" Lesson

[00:00:03]
>> Scott Moss: Yeah, wrote some test maybe? People still writing tests? Yeah, okay.
>> Student: My Mongo is messed up.
>> Scott Moss: Your Mongo is messed up? What's the error you're getting?
>> Student: Connection refused.
>> Scott Moss: Conection refused? It's probably because in your config.
>> Student: It has something to do with promise.
>> Scott Moss: I think it's in your config.

[00:00:24]
>> Student: Yeah.
>> Scott Moss: Cuz you're doing testing, that you have to change the URL maybe? Is that what it is? Cuz we have the config driven approach where it switches on testing now, and you got to set a new URL for the testing database. So that might be the case.

[00:00:36] I would start there. Does anyone see me write some tests, other than the tests I already showed you, that we just wrote. Would it be beneficial? Okay, well I'll write some tests if it's beneficial. What we're going to do is write some tests on the song resolver [COUGH] so we'll go to the song spec, we'll bring this open over here, we'll also bring over the user specs, so they can see what's happening.

[00:01:05] And we'll say let's describe song. And I'll go ahead and import these two things. And then for song I'm just gonna go ahead and do the same thing with the data base stuff. We're gonna drop the DB, wait the DBA, we got a user here, probably we'll create a song as well but we'll see depending on what test we write.

[00:01:29] And then I'll write a test for, let's see. Song.graphqo. Let's write a test for creating a song, all right? Let's do that. What we'll do is we'll come over to the
>> Scott Moss: Song spread, we'll say it('should create a song', this will be async, and then what I'm gonna do is I'm just going to, very much like I did inside of the user query, we're gonna make a query here.

[00:02:09]
>> Scott Moss: And instead it's gonna be a mutation, cuz we're creating, right? So I'm gonna make a mutation, that mutation and we're gonna give it a name, we'll call it CreateNewSong. It takes in a variable of input. So I'm just going to give the same name input. Its type is going to be new song type, required.

[00:02:32] I'm going to actually use the input here. I'm sorry. I'm gonna use the mutation here called newSong. It's going to use that input variable here. And I'm just going to get back to ID and the title of the song, so there is our mutation. Now I need to supply some variables to satisfy this input, so I'll do that right here.

[00:02:54] I'll say here's an input. It is of type object, because this is a non scala type, which means it's an object. And input is newSong, newSong if you go look right here is this, right? Let's make that required, make that required. So let's just give it a title and a URL and an artist.

[00:03:16] Let's do that. So come here we'll say title is 'Drop down'. URL is, you know, some URL here.
>> Scott Moss: And then, what's the other, artist is JJ, sure. And we'll use this user. And then what we'll do is, we'll use this result. Let's just put a dot over here so we're only running this test.

[00:03:52] And what we will do, the first thing is we're gonna make sure there's no errors here. So we'll expect.errors to not.exist. So let's run that one first, make sure that doesn't exist. And we'll say test out.
>> Scott Moss: Users, forgot to import the user model for the user creations.

[00:04:26] Let's do that, guess that's important.
>> Scott Moss: There we go, user/user.model. Now let's run it.
>> Scott Moss: There we go. So that test passed. Can obviously create a song if there's no errors, but let's keep doing some more insertions there. So also expects results.new, or, I'm sorry it was other .data.newsong.

[00:04:54] It's gonna be whatever the name with the that's gonna be the property that you did right, if you don't like that, just a list it, so we're a listed a song, there we go. So that would be (result.data.song).o.exist. So we'll do that, and then we'll also say expect(result.data.song.title)to.equal.

[00:05:26] This title right here.
>> Scott Moss: There we go, so let's run that.
>> Scott Moss: Cool, that passed. So that's a test for our creating a song.
>> Scott Moss: You run the mutation. Get the result, check to see there's no errors, check to see if you got back the right shape, look at the properties.

[00:05:52] You could take it a step further and actually go look in the database, and you can be like, I would expect await Song.findOne. You could do that, you could look in the database to see if it's actually in there. But I'm pretty sure it's in there, cuz this passed.

[00:06:12]
>> Scott Moss: Everybody following me there? As you can see I really like writing tests.