Testing
Testing your Nest server will be imperative so that once it is deployed their are no unforseen issure and it all runs smoothly. There are two different kinds of tests you will learn about in this book: Unit Tests and E2E Tests (End-to-end Tests). Unit Testing is the art of testing small snippets or blocks of code, and this could be as granular as testing individual functions or writing a test for a Controller
, Interceptor
, or any other Injectable
. There are many popular unit testing frameworks out there, and Jasmine
and Jest
are two popular ones. Nest provides special packages, @nestjs/testing
specifically, for writing unit tests in *.spec.ts
and *.test.ts
classes.
E2E Testing is the other form of testing that is commonly used and is different from unit testing only in that it tests entire functionality rather than individual functions or components, which is where the name end-to-end testing came from. Eventually applications will become so large that it is hard to test absolutely every piece of code and endpoint. In this case you can use E2E tests to test the application from beginning to the end to make sure everything works along the way. For E2E testing a Nest application can use the Jest
library again to mock up components. Along with Jest
you can use the supertest
library to simulate HTTP requests.
Testing is a very important part of writing applications and should not be ignored. This is a chapter that will be relevant no matter what language or framework you end up working with. Most large scale development companies have entire teams dedicated to writing tests for the code that is pushed to production applications, and these are called QA developers.