Skip to main content

Command Palette

Search for a command to run...

[TIL] test code using jest & supertest modules

04/28/23

Published
[TIL] test code using jest & supertest modules

Reasons why testing is important

  1. It increases product stability.

  2. It helps reduce side effects caused by adding or modifying features.

  3. It enables coding without uncertainty.

  4. It ultimately boosts productivity.

  5. It makes debugging easier.

  6. It helps avoid repetitive tasks during development.

  7. It enables writing cleaner and more reusable code.

Types of Testing

  1. Unit Testing - testing small-scale functionality, such as individual classes or methods.

  2. Integration Testing - testing potential issues that could arise when combining multiple functionalities (including external libraries, database access, and the entire codebase).

  3. Acceptance Testing - testing scenarios that align with user stories (from a business perspective).

  4. E2E (End-to-end) Test: A test that verifies whether all the components necessary for the desired outcome in a given feature are working correctly together (from a technical perspective).

Testing in Layered Architecture:

  • Mock Functions: Used to mock specific methods in order to verify information such as whether a method has been called or if input values match without actually executing code that is time-consuming, expensive, or has dependencies.

  • DI (Dependency Injection): Refers to passing dependencies from one object to another that wants to use them. It allows for mocking using mock functions.

  • Constructor Injection: One way to implement DI. When calling an object's constructor, dependencies are passed in and the object uses those dependencies to execute code.

  • Unit Test

    • jest: Testing in each layer

      • Repository layer test

      • Service layer test

      • Controller layer test

  • Integration Test

    • Supertest + jest: All API Request & Response test