This course has been updated! We now recommend you take the API Design in Node.js, v3 course.

Check out a free preview of the full REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course:
The "Exercise: 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:

In this challenge you are to write out tests for the GraphQL model mutations and resolvers.

Get Unlimited Access Now

Transcript from the "Exercise: Testing" Lesson

>> Scott Moss: So, lets get into testing. So we all did testing yesterday, right? Did anyone actually go home and do testing last night? No, I didn't think so. Okay, testing, okay so I'm like a test crazy person. I actually enjoy writing tests, I don't know why. I just do, it just feels good.

[00:00:24] Writing tests [LAUGH] I don't know why I just like seeing that green, it's my favorite color. So testing with graphql is way easier than testing what we did yesterday and you're gonna see why. Like it's so easy to write tasks in graphql it's ridiculous. And the reason it is, one, we don't have to run a server.

[00:00:43] We just don't have to run a server at all. You're probably thinking like, well, it's hooked up to the server, how do we do this? We don't, we don't have to run a server. We don't even have to go through express at all. We don't even have to touch Express to test our GraphQL.

[00:00:54] Even though GraphQL's hooked up to Express right now, we don't even have to look at Express, so have nothing to do with Express. In fact it has nothing to do with the node. It has nothing to do with anything. It's literally, you're going to see it has nothing to do with a server.

[00:01:06] It has nothing to do with express. We're just going to be testing pure graph QO and you can pretty much do that in any environment that can execute our code. We can literally execute graph QO anywhere. In fact there's a package called graph QO anywhere and allows you to use graph QO anywhere.

[00:01:22] Like you can use GraphQL to make CSS or React components, or whatever you want. They just take that query language and spit out whatever you want, right, depending on what template you give it. So we're gonna use GraphQL to execute inside of our tests. We can execute query and inspect the response and the DB activity so what we'll do it run a query against the schemes on some variables.

[00:01:45] We'll look at the response that we get back and then we'll also look in the database to try to see what happened. If we expect it to happen. So, that would be our strategy with testing. So let's look at some of the tests here and you can check out lesson 14.

[00:02:00] [NOISE] And if you go down to the test folder in the helper's file, you should see a nice little function here. And this is literally the only function we need to write all the test for our graph cue out and basically what it is using is the Graph QL JS implementation.

[00:02:27] This is the implementation straight from Facebook. When Facebook created Graph QL, they, Graph QL was just a speck. It wasn't a code, it wasn't a language, it was just a speck. And then they made the JavaScript version of it, the implementation of it. Facebook made it, this is that version.

[00:02:42] This is pretty much what everything in JavaScript that use GraphQL is based off on. It's basically the compiler for GraphQL, if you will. It has the AST, the rules, everything's pretty much based off this. We're gonna use it raw, so Apollo uses this internally to run our queries.

[00:02:58] We're just gonna use this to run our tests for not have the power left flexibility. So what we can do, using that graph to our query, so this one function right here, we can use to actually go ahead and execute all of our text. It's gonna be using the graph QO library, that Facebook implemented.

[00:03:14] It's just a raw library that every implementation of graph QO uses underneath it. So we're gonna use it for the flexibility. And basically all we have to do is we pass in the schema which is a combination of resolvers and the type definitions, right? Those two combined as a schema.

[00:03:30] The query is the thing that we write in graphical that we're asking for, so that's either query or mutation, it's just a query. This object right here is empty because what it's gonna be is gonna be the root value, which is nothing. We're not passing a root value for test.

[00:03:46] It's just like in every single resolver we have, the root value I'm place holding with an underscore, it's nothing. This next object is gonna be the context. I'm passing the user down as the context. And in the last argument is gonna be the variables for mutations. So with this one function that returned our promise we can execute our entire graph kill schema without a server or anything we just run it like coat.

[00:04:07] It returns our promise and it resolves the values. Pretty simple, right? So I just imported the schema I have GraphQL here and for your test, all you gotta do is patch in a query, some variables associated with it, and what user you're signed in as. And it'll execute your query, and you can check it out.

[00:04:23] So I wrote some tests, we can go check them out on user spec. And you can see right here, if you haven't written tests on the back end, it's a good habit to have stateless test, as in they don't rely on previous states of previous tests. By saying that, I'm dropping the data base every time I write a test.

[00:04:43] Every time I'm making a search I drop the data base. I don't want any data left over from the old test to be there because it might affect this next test. So that's why it's a good way to just drop the data base every single time you do, One of these it's.

[00:04:57] And mocha, whenever you do before each of after each it's basically saying before each it after each it do this. So that's what I'm doing here. So before each I'm dropping a database and I'm creating a user, I'm assigning it to this closure so i can have access to it down here.

[00:05:12] And then just to be safe after each I'm also just gonna drop it. So always have a clean database when I come inside of this. So our first one is it should get me. We have a query called get me. And you can see I'm just using that function run query that I just showed you.

[00:05:28] And I'm just going to run the query, I'm going to pass the query that I want, it's just a string. It's literally just a string. The same thing you would pass in graphical. It's just a string. The second argument is variables. I don't have any variables, this doesn't take any variables so I don't need any.

[00:05:41] And the last one is the user. And you can see this one is the user. And as you can see I get back the same thing I would get in ground and I just make some assertions on it. That's our test. That's how simple it is. We didn't talk to express, we didn't talk to a server, we didn't fire up a port.

[00:05:56] Don't even need the Internet we just run this test. Pretty clean. And it's just more of the same heres an example of a mutation. It's just a string you mutate I'm testing to see if I can update me. Here's an example of variables. There's a variable called input who's type is this.

[00:06:13] I'm updating it You can see I pass in this input variable as well or the user. And then I just run some tests.
>> Scott Moss: So if you go ahead and run this right now, I'm just gonna put a dot only here. And run the rn test.
>> Scott Moss: They both pass so, what I want you all to do is we're gonna work on this for 30 minutes and I wanna see how many test you can write for the crazy mutations that we have, for the song and playlist model.

[00:06:54] And you can write all test for all crazy mutations that's pretty dope but just do as much as you can using this exact formula. Everything that you need is right here in this user spec file. Everything you need. You expect from chai so do your assertions. You have run Query to actually execute it.

[00:07:11] Then you have dropDb to drop the database. You could even just literally just copy this and change all the things that you need. But I want you to write as many tests as you can inside the song spec JS and the playlist spec JS. Test the mutations, test the queries, making sure you're getting back what you need.

[00:07:29] Making sure you know you can resolve all those, properties and stuff like that.