Search code examples
javascriptunit-testingjestjsnestjstypeorm

code coverage issue in entity class in nestjs typeorm


I have written many module for Nest.js/TypeORM but I never got code coverage issue for entity class. In my one such class we have mapping of ManyToOne. Now code coverage is showing that this line is not covered.

Do I need to write separate test file for entity class like we write for controller and service class? Also is there any way I can accomodate testing of entity class in my controller or service test files? Is there any way I can handle this issue?


Solution

  • Personally, I would ignore code coverage for entity and DTO files. They're really just interfaces that have metadata, and usually don't contain any logic other than possibly lazy loading a class instance so that it can be read properly at runtime.

    If you are going to have code coverage for these files, you can do one of two things

    1. move the lazy loading function to its own file so you can call it directly and "trigger" the function, thus providing coverage

    2. make an integration test that actually talks to the database so TypeORM reads the metadata and covers the decorator.

    You can see an example of the first option in this answer