Advanced GraphQL Advanced GraphQL

Unit Test Resolvers with Jest

Check out a free preview of the full Advanced GraphQL course:
The "Unit Test Resolvers with Jest" Lesson is part of the full, Advanced GraphQL course featured in this preview video. Here's what you'd learn in this lesson:

Scott illustrates that GraphQL can run anywhere because a server does not need to test GraphQL server.

Get Unlimited Access Now

Transcript from the "Unit Test Resolvers with Jest" Lesson

[00:00:00]
>> Scott Moss: All right, so the first test I'm gonna do is. Actually, let me just copy this boilerplate. There we go, okay. So I need to grab the task resolvers.
>> Scott Moss: There we go. Change this to tasks.
>> Scott Moss: Boom. And now let's just test one of these resolvers. So if we go to.

[00:00:32]
>> Scott Moss: Where we at? Test our resolver, pick up easy one. Okay, this one right here. This one takes, tasks, it literally takes nothing. I just returns an array of tasks. So, that one's super easy to test. Like one line of code is used in the models, super simple.

[00:00:49] So, let's do that. So what we'll do is we'll copy the task. We have the task resolvers here. So, in order to test it, the first thing we gotta do is, we need to actually add some task in the database. So we could use, let's just add some in here, we'll just say wait.

[00:01:07]
>> Scott Moss: db.models.test.create which takes an array of things I can create, I can create some task here, let me get a Mongo Id here, id = mongoose.
>> Scott Moss: There we go, mongoose.schemea.object
>> Scott Moss: I'm pretty sure that generates an object ID, maybe. Okay. So what we'll do is, we'll just make some task here, which, if you go look at the task model, it needs a name, a project, and a type.

[00:02:01] Everything else it's pretty much default, so let's do that. So we'll just say name: 'test', project: id, what was the other? The type: 'dev', the other one is default, and we'll make another one.
>> Scott Moss: test2, design, cool. So now we got some test here, I'm gonna put a .only on this, so it only runs this spec.

[00:02:33] And then I'm gonna execute it with yarn test.
>> Scott Moss: And it still ran project aspect for some reason.
>> Scott Moss: So this one is, what did it say,
>> Scott Moss: Await is a reserved word. This must not be the latest node in this terminal, it's gotta go in here. Let's go here.

[00:03:02]
>> Scott Moss: Yeah.
>> Scott Moss: Cool. All right, still getting it. I did not put an A sync in front of get project. Okay, project resolver, getProject. Forgot to put an async here. Nice.
>> Scott Moss: All right, now we got some real errors, okay. Use this. I love it when errors are helpful.

[00:03:37] You could just copy and past this, and put this in. That's beautiful. There we go.
>> Scott Moss: Cool. Repo euro is required for a developer test, so we need to put a repo euro here
>> Scott Moss: And we'll just make up one. Actually, I know a good repo on GitHub, it's called tipe.

[00:04:09]
>> Speaker 2: [LAUGH]
>> Scott Moss: [LAUGH] github.com/tipeio/tipe. You should go look at it if you haven't.
>> Speaker 2: [LAUGH]
>> Scott Moss: There we go, assetURL's required for that one, man.
>> Scott Moss: There we go, and we'll just put. There we go.
>> Scott Moss: Boom, okay, now our test is actually not dying. So we created those tests, and now we wanna actually test them.

[00:04:52] So we'll get some results here, and we'll run our Resolver. Which is basically taskResolvers.task. No, I'm sorry. taskResolvers.query.task. There we go, and it doesn't really take any arguments, and I don't think it's even using any, so let's check it out.
>> Scott Moss: Args.input, so it takes an input here, but I'm just gonna keep it blank cuz we're really not using it.

[00:05:35] So knows the root argument, the second argument is the args. I'm just gonna put an input in there cuz it's using that, which it's also displaying. So we'll just get all the tasks, there we go. And then we'll get the results, and then we'll expect results to HaveLength(2).

[00:06:00] And we'll see if that works.
>> Scott Moss: This doesn't work because yeah, it's using context, I forgot that. So this is the beauty of that. So now I can come in here and context has a models property on it which has a task. Which is a db.models.task. So I'm just gonna inject that.

[00:06:32]
>> Scott Moss: And our test passed. And that's how you would unit test a resolver. You would literally go to the same process of failing over and over and over like I did, and then you probably get it to work.