API with NestJS and TDD: Part 3

TDD guide with NestJS to delete a property: service and controller tests, updated mocks, and DELETE endpoint; check the repos on GitHub.

domingo, 17 de agosto de 2025 • 3 min read • Q2BSTUDIO Team

Artificial-Intelligence-

Hello Coders, welcome to the third part of the Create API with NestJS using TDD approach project. We continue with the implementation using the TDD methodology and translating everything into Spanish to facilitate learning.

Reminder of the key concepts of the TDD approach

1. Write a failing test. The initial step of the TDD cycle is to create a test for the small functionality you want to implement.

2. Write the minimum code to pass the test. Implement the necessary logic just so the test stops failing.

3. Refactor. Improve the structure and readability of the code while keeping all tests green.

This cycle, known as Red-Green-Refactor, guarantees that a functional version of the code always exists and that the critical parts of the project are covered by tests.

In this installment, we add the functionality to delete a property and write the corresponding tests in the file src/properties/test/properties.service.spec.ts. For the service test, cases are created that validate that a property is deleted and that an exception is thrown if the property does not exist.

Following TDD, we run the tests with npm run test and observe the failures because the remove implementation is missing in the service. The solution is to add the logic in src/properties/properties.service.ts to find the property by id, validate its existence, and then delete it, returning the deleted object.

After implementing remove, we run npm run test again and detect that in the unit tests, the remove mock is missing in the mockRepository. Therefore, we update beforeEach so that the repository mock includes find, findOne, create, save, and remove with implementations that return appropriate resolved values.

With that complete mock, we run npm run test and the service tests pass. Now it's time to add the controller tests in src/properties/test/properties.controller.spec.ts, where the call to the remove service is simulated, and it is validated that the controller delegates correctly and returns the deleted property.

Just like in the service, we add a remove function to the controller mock that returns a promise resolved with an object containing the id so the controller test can run.

Before running npm run test, we verify that the route and handler are missing in the controller, so we add a DELETE endpoint in src/properties/properties.controller.ts that receives the id as a parameter and calls propertiesService.remove with that id.

When running npm run test again, all tests pass, and the complete flow of deleting a property is covered in both the service and the controller.

As a pending next step, you can integrate Swagger to document the API and visualize endpoints such as listing, getting, creating, updating, and deleting properties. This task will allow you to expose the documentation and test the API from a user-friendly interface.

Final resources and project repositories. You can review the final version of the final-part-removeRest branch at https://github.com/joeaspiazudeveloper/nestjs-houselocation-tdd/tree/final-part-removeRest and the complete project with CRUD and Swagger at https://github.com/joeaspiazudeveloper/properties-api-nestjs

About Q2BSTUDIO. We are Q2BSTUDIO, a software development company specialized in custom applications and custom software. We offer personalized solutions for companies, including artificial intelligence (AI) for businesses, AI agents, cybersecurity services, and cloud services (AWS and Azure). Additionally, we provide business intelligence services and implementations with Power BI to transform data into strategic decisions. Our experience in custom applications and custom software combines good development practices and a TDD approach to obtain products always ready for production.

Keywords to improve positioning: custom applications, custom software, artificial intelligence, cybersecurity, cloud services (AWS and Azure), business intelligence services, AI for businesses, AI agents, Power BI.

I hope this tutorial was useful to you. If you liked it, share and like it to support the content. Soon I will share the third part on GitHub with each detailed step and the final version of the project. It was a pleasure to make this tutorial. The TDD approach brings many benefits to maintain the quality and stability of the software. See you later, Coders.

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.