-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Track 5/9] The tests 🤓 #14
Comments
Finish |
Step 2/6 - Libraries everywhere: axiosEstimated time: 2 hoursAnother of those cool libraries is axios. This library makes our work of performing requests to a server easier. Take a look at their docs. Now, let's increment our step-by-step for the test execution:
In order to acomplish this new first step, your task is to:
NOTE: you can open a pull request after this step, so we can check if your setup is going ok for now. |
Finish |
Step 3/6 - The last library (for now): chaiEstimated time: 30 minutesThe last library for tests is chai. Chai is a very complete library that handles assertion, i.e., checking if the result has the values that were expected to have. As usual, take a look at theis docs, install it on your project and try to use the Try to use these functions as if you were writing a sentence, to improve readability. For example: expect(queryResponseField).to.be.eq('Hello, Taqtiler!'); |
Finish |
Step 4/6 - Database and environmentsEstimated time: 1 hourNow we're going to include the database on our tests. After all, we should test if our queries will be returning what is on the database correctly, and if our mutations are changing the database accordingly. Do you remember we talked about setting up 2 containers because we were going to use 2 databases? Yeah, the north remembers 🐺! As a preparation for this step, you're going to setup 2 environments: the one you're already using to develop with One way of handling separate environments is to use environment variables. They are variables that can be set for each environment, so when we start our server on that environment, we use the specific values for it. We could have as environment variables, for example, our database host, user, url, etc... Your task now is to set some environment variables to connect with the right database for each environment: Now, our step-by-step for the tests is incremented again:
NOTE: remember that previous note about async tasks and Promises? Here's another example that we should use it properly, because before start running the tests (not that joke again), you should verify:
After it all, you can run the tests. |
Finish |
Step 5/6 - The "createUser" Mutation testEstimated time: 3 hoursNow it's time to (finally) write the tests for your recently implemented
NOTE: don't forget to open a PR with your test. |
Finish |
Step 6/6 - Error handlingEstimated time: 2 hoursNow it's time to make our
As already said, there are more possibilities, but these 2 are ok for the onboard. What do you think we should do on these scenarios besides throwing a generic error? Well, there are multiple solutions to that, but we have a suggestion. Let's return the following structure on the {
code: number; // Conveniently equal to the HTTP protocol status code
message: string; // Message to describe the error
additionalInfo?: string; // Additional info, generally good for the client developer to know more of what happened
// ... GraphQL default fields
} You should be able to research and find how you can treat errors when you find one on your code, but it's basically throwing an error and treating it on a proper function that the Your task now is:
|
Finish |
Click here for your next track |
Step 1/6 - First test setup
Estimated time: 1 hour
Now you're going to work on another super-important concept on the process of building your own server. Have you ever stopped to think about how you would test if your code works? 🤔
If you thought about performing the query/mutation on one of those applications (browser, graphiql, graphql playground) and checking if it works as expected, that's one way of doing it. But we have a more solid proposal: writing more code for tests! The principle is simple: you give an input and check if the response is the expected. Why is it better? Well, some advantages:
If you search on the Internet, you'll find out there are many ways to test a code. Depending on the project, people decide to use one, or several of them. Here, we're going to write
E2E tests
(end-to-end). For each query/mutation of your server, you're going to setup the database for them, provide some input scenarios, and then check if the response is given as expected. Take some minutes to read about them on the Internet.Setting up tests on your server is not an easy task. There are a couple of things to learn and prepare first. We're going to create a step-by-step to help.
As you can imagine, giving the previous steps, there are more cool libraries to help us on this process. We're going to present them one at a time.
The first one is the main library to write the tests codes: mocha. Take a look at their docs. It's important to know that mocha provides some methods that can be executed like a timeline. This is very useful to handle tasks in specific orders.
Your task is very simple in this first step:
devDependencies
to install it. Would you be able to say why? 🤔index.ts
inside it.package.json
to run your tests withnpm run test
The text was updated successfully, but these errors were encountered: