The code coverage report for Istanbul indicates that there is an uncovered branch within the constructor function

Currently, I am conducting a test on my node.js written TypeScript code using mocha and chai. To measure the code coverage, I have incorporated nyc.

Moreover, in this project, I am utilizing Typeorm as an ORM tool and implementing inversify as an IoC container.

During the creation of unit test cases, the code coverage indicates that there are uncovered branches wherever there is a decorator such as @Inject or @Response being passed as a parameter in either a constructor or any method.

I suspect that the following configuration could be causing this issue:

"experimentalDecorators": true,
"emitDecoratorMetadata": true,

However, disabling these configurations may lead to a failure in Typeorm functionality.

Here are some screenshots for reference:

https://i.sstatic.net/0VeHk.png

https://i.sstatic.net/LDZc3.png The content of the tsconfig.json file can be found below:

  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "declaration": true,
    "sourceMap": true,
    "outDir": "dist",
    "noImplicitAny": true,
    "baseUrl": "./",
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "resolveJsonModule": true,
    "moduleResolution": "node",
    "types": [
      "node",
      "mocha"
    ],
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "include": [
    "src/**/*.ts",
    "migrations/**/*.ts"
  ],
  "exclude": [
    "./node_modules",
    "./dist"
  ]
}

For running tests and generating test coverage reports, use the following npm commands:

"test": "mocha -r ts-node/register ./tests/unit/**/*.ts --exit",
"test:cov": "rimraf reports && rimraf .nyc_output && nyc mocha

Answer №1

After including "@istanbuljs/nyc-config-typescript" in the .nycrc.json file to extend an array and adding "source-map-support/register" in the .mocharc.json file to require a resolved array, the issue was successfully resolved

.nycrs.json


{
    "extends" : [
        "@istanbuljs/nyc-config-typescript"
    ],
    "include": [
        "src"
    ],
    "extension": [
        ".ts"
    ],
    "check-coverage": true,
    "lines": 0,
    "reporter": [
        "lcov",
        "text-summary"
    ],
    "report-dir": "reports/coverage",
    "all": true,
    "exclude": [
        "./node_modules",
        "src/*.d.ts",
        "./dist",
        "./migrations",
        "./commitlint.config.js",
        "**/*.entity.ts",
        "**/I[A-Z]*.{ts}",
        "**/*.config.{ts}",
        "**/config.ts"

    ],
    "timeout": 4000
}

.mocharc.json

{
    "extension": [
      ".ts"
    ],
    "recursive": true,
    "require": [
      "ts-node/register",
      "source-map-support/register"
    ],
    "exit": true,
    "spec": "./tests/unit/**/*.ts"
  }

Then execute the test coverage command:

nyc mocha

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to defining a typescript class property using an index signature

type TField = { field1: string; field2: boolean; field3: TMyCustom; } class Test1 { // I opt for using TypeScript's index signature to declare class fields [key: keyof TField]: TField[typeof key] // Instead of individually declaring each ...

Incorporating observables into an existing axios post request

Currently, I have an API using axios to send HTTP POST requests. These requests contain logs that are stored in a file. The entire log is sent at once when a user signs into the system. Now, I've been instructed to modify the code by incorporating Rx ...

What is the best way to convert Angular libraries to ES2016 format?

I have made the decision to completely stop supporting outdated browsers and discontinue support for ES5 compilation/polyfills. Currently, my Angular project code is compiled with approximately 85% coverage as shown below: "compilerOptions": { ...

Converting XML file data into an array using AngularJS 4

How can I parse XML data into an array in AngularJS 4? Here is an example of the XML data: <data> <element>ESA</element> <element>XCAVS</element> <element>TQSZ</element> <element>HGDQZ</element> <e ...

The TS-Mocha and Chai duo have encountered a hitch: a peculiar error message, TS2695, informing them that the left side of the

Software Versions: "ts-mocha": "^8.0.0", "ts-node": "^10.3.0", "chai": "^4.3.4", Sample Code: expect(wrapper.find(MyListItem)).to.have.length(3); Execution Command: ts-mocha tests/**/*.tsx -r u ...

Tips on retrieving a strongly typed value from a method using Map<string, object>

Having had experience working with C# for a while, I recently ventured into a Node.js project using TypeScript V3.1.6. It was exciting to discover that TypeScript now supports generics, something I thought I would miss from my C# days. In my C# code, I ha ...

How to configure setup for running ES6 tests with Mocha 6 and Babel 7?

Looking to compile a library written in ES6/7 to ES5 and store it in a dist/ folder. Additionally, I need to run tests for this library. The development dependencies listed in my package.json file are as follows: "devDependencies": { "@babel/cli": "^7. ...

Unable to define the type for the style root in Typescript

I am encountering an error that is asking me to code the following types in the root tag: No overload matches this call. Overload 1 of 2, '(style: Styles<Theme, {}, "root">, options?: Pick<WithStylesOptions<Theme>, "fli ...

Creating a dynamic calendar application with Angular 2 using PrimeNG components

After diving into PrimeNG, I diligently followed the instructions on their documentation to get everything set up. However, my UI ended up looking quite odd, similar to the image below. Does anyone have any insight as to why this might be happening? Here ...

Error encountered with NextJS and useRouter due to TypeScript - The type 'undefined' is invalid for use as an index

I'm facing a TypeScript error that I can't seem to resolve, despite trying solutions from other questions. It's giving me the error message 'Type 'undefined' cannot be used as an index type.' Type 'undefined' ...

The data type returned by a method is determined by the optional parameter specified in the

I have a situation where I need to create a class with a constructor: class Sample<T> { constructor(private item: T, private list?: T[]) {} } and now I want to add a method called some that should return: Promise<T> if the parameter list ...

A Typescript type that verifies whether a provided key, when used in an object, resolves to an array

I have a theoretical question regarding creating an input type that checks if a specific enum key, when passed as a key to an object, resolves to an array. Allow me to illustrate this with an example: enum FormKeys { x = "x", y = "y&q ...

What happens when Angular elements don't have an injector?

Exploring Angular's custom elements while steering clear of dependency injection is my current experiment. const MyElementElement = createCustomElement(MyElementComponent, { injector }); customElements.define('my-element', MyElementElement) ...

JS : Removing duplicate elements from an array and replacing them with updated values

I have an array that looks like this: let arr = ['11','44','66','88','77','00','66','11','66'] Within this array, there are duplicate elements: '11' at po ...

What are the best practices for integrating Qt with React in TSX?

While I've figured out how to communicate qt with JS successfully, the challenge arises when trying to use React in TSX for frontend development. The previous solution failed on this front. Backend code: #./main.py import os from PySide6.QtWidgets ...

Having trouble showing the fa-folders icon in Vuetify?

Utilizing both Vuetify and font-awesome icons has been a successful combination for my project. However, I am facing an issue where the 'fa-folders' icon is not displaying as expected: In the .ts file: import { library } from '@fortawesome/ ...

The object[] | object[] type does not have a call signature for the methods 'find()' and 'foreach()'

Here are two array variables with the following structure: export interface IShop { name: string, id: number, type: string, } export interface IHotel { name: string, id: number, rooms: number, } The TypeScript code is as shown below ...

Dealing with Nested Body Payload in PATCH Requests with Constructor in DTOs in NestJS

Within my NestJS environment, I have constructed a DTO object as follows: export class Protocol { public enabled?: boolean; public allowed?: boolean; constructor(enabled: boolean, allowed: boolean) { // With a necessary constructor this.enabled = ...

Preventing access to the login portal

Currently, I am exploring the most effective method to prevent or redirect users from accessing the login page if they are already authenticated. I am currently working on a React project with TypeScript. What would be the optimal approach to achieve thi ...

Harnessing the power of external Javascript functions within an Angular 2 template

Within the component, I have a template containing 4 div tags. The goal is to use a JavaScript function named changeValue() to update the content of the first div from 1 to Yes!. Since I am new to TypeScript and Angular 2, I am unsure how to establish comm ...