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

Scott gives an overview on how the tests were written for the course.

Get Unlimited Access Now

Transcript from the "Testing with Jest" Lesson

[00:00:00]
>> Scott Moss: Okay, the only other thing I wanted to talk about before I get to some really cool stuff is how I set up the test in here. So all the tests that you've been running and stuff, it's only for the product type, the coupon type, and the user type.

[00:00:16] Although they've been created for you, they don't have any tests. I left them empty, so if you wanted to get practice writing tests for GraphQL, you can write them in there. So just to walk through how I set up tests and stuff, basically, let's see here, the product.

[00:00:29] If we go look at the resolvers, I'm just testing the resolvers as just regular functions, completely outside of GraphQL. The resolvers have nothing to do with GraphQL at this point. I'm literally just testing them as functions. I mock out all the arguments, and then I expect some result, just pure functions.

[00:00:47] And that's the beauty of GraphQL, the resolvers are just pure functions, they don't need to know about GraphQL. I'm just testing with pure functions, I'm not using GraphQL at all, so that's pretty simple. And then for the schema, the way I'm testing it out is two ways. One, to test the schema types, I'm converting the schema, the STL, so the string, over to an object representation, which is gonna be here.

[00:01:11] This is basically gonna give me an object representation of every single type. It'll give me the fields, the names, the validations, the values, every single thing about a type. And I can use that to just literally run tests against it to make sure it has the right names and return types and things like that, so that's one way I'm testing it.

[00:01:27] As far as the queries, the way that I test that is I'm creating a mockServer. So the sweet thing about GraphQL is because everything is strictly type, you could mock out an entire server. Because if I know a name is a string, I could just send back a fake string.

[00:01:41] If I know a age is an integer, I can send back a fake integer, so I can mock out an entire server. So you can send me a query that's infinitely long, and I can mock it out on the fly because I know exactly what the types are.

[00:01:51] I'll look at the inflow object, I compare that to the schema, I see what the types are. I generate those fake types for you, and I send you back a fake response that satisfies GraphQL's query schema. So pretty simple, a lot of people take that to another level and add things like fake or js to it and stuff like.

[00:02:10] So I'm creating a mockServer, which is given to us for free from this package called graphql-tools. And basically what that does is you give it a schema, and it literally just creates a mockServer for you. And it acts just like a server, I can run queries against it.

[00:02:24] The only difference is I don't have to create resolvers. The resolvers are gonna be mocked out for me. The resolvers know that a name is a string, so it must return a string. But it will error out if the query doesn't match the schema that I gave it, so that's how I'm testing the queries.

[00:02:37] This will break if this thing asks for something that this schema didn't have. So that's how I'm testing the queries and the mutations without actually starting the server. Other than that, there's only some slight MongoDB setup stuff for the test. Which basically creates a MongoDB for every single describe block on the fly, shuts it down, and then deletes it.

[00:02:59] And that's how we can run the test in parallel and keep them fast. And that's just here in the test-db-setup file. And that's it for testing, so yeah, feel free if you want to continue. If you wanna start testing, which you should, go into the coupon and user and write some tests if you want to.

[00:03:20] But we're not gonna do that here, that's just for you to do. Look at the test that I wrote in the product and continue with that. Challenge yourself, see if you can get to 100% test coverage. That'll be amazing if you could, that's ridiculous, but yeah, that's there for you.

[00:03:33] Other than that, we don't have anything other left for this course. The last thing I wanted to do is answer some questions. And then I have a list of things I wanna show you, but I wanna open up for questions first.
>> Speaker 2: Just a quick one along the lines of testing, do you have a way of finding code coverage with-

[00:03:52]
>> Scott Moss: Yes, that's the sweet thing about Jest is that code coverage is free. You just pass the coverage flag, so if you run your test, let me check out the master and run this very quick. Actually, I'll run it like this, so maybe it'll work here on this one.

[00:04:08] So if I say --coverage,
>> Scott Moss: So it'll spit out coverage automatically, it's free, that's why I like Jest. All the other free ones, you gotta sell all this stuff up, you gotta use NYC and Istanbul, all this crazy stuff, Jest is like, here you go. I mean, underneath the hood, it probably uses a lot of those things, but still, it does it for you for free.

[00:04:34] I'm sure there's some setup here, it should be looking at some files and stuff like that, like why is it looking at the test-db-setup file? I don't want it to look at that, but yeah, you can set up the coverage configuration in the jest config in your package, that JSON.

[00:04:49] Or if you have a jest config.json in your root, you can set it up there. But by default, yeah, that's what it does, it does that.