
Learn how to initialise our NodeJs Express test-driven development project with Jest
Learn how to create a simple Nodejs Express REST API in just 10 minutes!
A brieft introduction to the various REST HTTP Methods we can use inside the Nodejs Express REST API.
How to install Jest to use with Nodejs Express for test-driven development
Making our first test with Jest for Nodejs Express REST API with test-driven development
Making our first test with Jest for Nodejs Express REST API with test-driven development PASS
How to setup Jest to automatically run on file changes in our Nodejs Express app
Defining our Mongoose/MongoDB model to use with our Express REST API.
How to use jest.fn() to mock functions in our Mongoose model to verify that our Express controller has called the model correctly
Making our jest.fn() to mock functions in our Mongoose model to verify that our Express controller has called the model correctly
How to setup our Jest test environment correctly when using Mongoose inside Nodejs Express.
How to use node-mocks-http package to verify that our Nodejs Express Request and Response objects have been used correctly
How to write a Jest test with node-mocks-http Request object with the Express controller and making the test pass
How can we test if the response status code is set correctly and that the response is actually being sent back in Nodejs Express with Jest?
How to test if JSON body is being sent back correctly in our Nodejs Express REST API with Jest
Create an integration test for the to-do app using super test and jest, testing a post against the express app and validating the response title and status across multiple layers.
Export the express app for supertest, then implement the todos post route to return 201 for resource creation, bridging unit tests to true integration testing.
Add express.json middleware in App.js to parse JSON payloads on post endpoints, ensuring request.body is populated for tests.
Fix the address already in use error by preventing double app.listen during tests, so the test app runs only once and avoids unhandled promise rejection.
Set up a cloud-based MongoDB database using Mlab, sign up for a free 500 MB plan, choose cloud provider and region, name the database, and deploy.
Connect a node express API to a cloud MongoDB using mongoose by creating a connect module, exporting a connect function, and wiring it into app.js for integration tests.
Modify unit tests to use async/await to handle promises from createTodo. Fix mongoose deprecation warning by enabling useNewUrlParser and ensure both unit and integration tests pass before manual server testing.
Create a server.js to run the Express REST API by importing app.js and starting server. Run node server.js, test with Postman, and clear collections to verify new document is created.
Explore error handling in unit and integration tests using Jest, by validating required fields like title and done in a to-do model and preventing unhandled promise rejections.
Restart the Node.js server after code changes and run manual error-handling tests in Postman, identify 500 internal server errors, and transform html error responses into proper rest error responses.
Implement middleware to create a rest error response and adjust tests to expect a json body with a validation error message, instead of an html response.
Align the unit and integration tests with Jest by matching expected and received errors, then run a final manual Postman test to verify proper error handling.
In Node.js with Express, write the first test for getTodos using Jest, fix the failing test by adding a getTodos function, and confirm all tests pass against the Todo model.
Create a jest test for the get todos controller to return status 200 with a json body of all todos, using a mocked Todo.find.
Experiment with error handling in a Node.js Express app by writing async tests with Jest that verify next is called with an error and implement try-catch in the todo controller.
Learn to write an integration test for GET /todos in a Node.js Express app with Jest, validating a 200 response and an array of todos with title and done.
Use postman to test localhost todos endpoint with a http get after starting the server via node server dot js. Integration tests create todos, followed by update and delete operations.
Learn to implement and test the get todo by id endpoint, create a focused unit test to ensure the method exists, and validate fetching a single todo by id.
Create a Jest mock for TodoModel.findById and verify it is called with the route todoId when invoking the Todo controller's get by id.
Write a unit test that mocks Todo.findById to return a todo item, asserts a 200 response and a json body matching the Todo in an express route, using Jest.
Explore nodejs express unit testing and integration tests with jest by solving response testing for getById, mocking the todo model, and verifying a 200 status and the json body returned.
Implement error handling tests in a Node.js Express app using Jest, asserting rejected promises propagate via next and handling not-found items in the get by id controller.
Handle the not-found TodoModel case by returning a 404 when an item is missing, and update tests to mock find by ID returning null.
Develop a 404 integration test for fetching a non-existent todo by id, validate id handling between invalid and missing IDs, and confirm unit, integration, and manual tests pass.
Test that the put request to the todo controller returns a 200 status with the updated todo as json, using the request body and id.
Test http put error handling in node express with jest by mocking a todo update that rejects, asserting next receives the error, and implementing try-catch for 404 on missing items.
Explore updating a non-existent todo by id, which returns a 404 with a null update result, validated by unit tests, and proceed to the integration test in the next lecture.
HTTP put integration test for updating a todo in a rest api, validating status 200 and updated title and done fields, and add a 404 test case.
Learn to implement the delete endpoint in nodejs express, test the http delete by finding by ID and deleting the Todo, and use jest.mock to replace the whole Todo model.
Learn to test the http delete method from unit to integration tests in node express, covering find by id and delete, 200 responses, and 404 not found handling.
Learn how to write a full CRUD REST API with Nodejs, Express and Jest using the test-driven development method!
TDD is becoming more and more of standard, and for good reasons!
With TDD, you can feel more safe and secure that any changes you make are not breaking existing functionality in your applications. Manual tests cannot be sustainable in large applications, or even medium applications. Plus, they're incredibly tedious.
Without TDD... well, you know the story. You try to add functionality while trying to touch the existing code as little as possible, because you don't want to break things unintentionally..
With TDD you won't be scared to make big refactors in your code to clean up messy code, you will know quickly if you've broken any existing functionality!
I'll show you step-by-step how to create a REST API with Express with unit tests and integration tests using the Jest test framework. There's going to be lots of opportunities for you to work on your own and do some exercises as well for optimal learning!
If you have any questions or suggestions to the course, feel free to message me anytime, I love interacting with my students!