Skip to content

[Bug]: jest v30 branch reporting incorrect branch coverage for controllers and return types with nestjs #16209

@joaosarmentoptc

Description

@joaosarmentoptc

Version

30.0.0

Steps to reproduce

I need to reopen this. #15760 as I am experiencing this issue and apparently others in the community.
I also raised a Stack Overflow question: https://stackoverflow.com/questions/79945599/jest-is-marking-uncovered-branch-on-commas-and

It has been a year since @ciaranduncan14596 reported this and I am surprised this is not fixed yet.

We use nestjs as our node.js framework of choice and have implementation of controllers that look something as follows

@Controller('things')
@ApiTags('things')
@UseFilters(HttpExceptionFilter)
@ApiProduces('application/json')
@ApiDefaultResponse(ERROR_RESPONSE_SCHEMAS.DEFAULT_ERROR_RESPONSE)
@ApiInternalServerErrorResponse(
  ERROR_RESPONSE_SCHEMAS.INTERNAL_SERVER_ERROR_RESPONSE
)
export class ThingsController {
  /* istanbul ignore next */
  constructor(private readonly thingsService: ThingsService) {}
 
  /* istanbul ignore next */
  @Get('/:id/user/:userId')
  @ApiNotFoundResponse(ERROR_RESPONSE_SCHEMAS.NOT_FOUND_RESPONSE)
  @ApiOkResponse({ type: dto.Thing})
  async getThingForUser(
    @Param('id', ParseIntPipe) id: number,
    @Param('userId', ParseIntPipe) userId: number
  /* istanbul ignore next */
  ): Promise<dto.Thing> {
    return this.thingsService.getThingForUser(id, userId);
  }

We've recently attempted to upgrade from jest version 29 to jest version 30 and received some unexpected behaviour in terms of our branch coverage. It's inexplicitly reporting a drop in branch coverage despite no changes other than the jest upgrade. The lcov-report indicates that the issue is with our {} within our constructor and <dto.Thing> but we've found no clean way to mitigate this. All possible code paths are covered. Dropping the coverage for branches is our only option thus far

Expected behavior

Branch coverage should have matched between versions 29 and 30

Jest Version 29

  ------------------------------|---------|----------|---------|---------|-------------------
  File                          | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
  ------------------------------|---------|----------|---------|---------|-------------------
  All files                     |   98.72 |    95.23 |   96.61 |    98.6 |                                 
   thing                      |   98.28 |    93.02 |   95.91 |   98.12 |                   
    thing.consumer.ts   |     100 |      100 |     100 |     100 |                   
    thing.controller.ts |     100 |      100 |     100 |     100 |                                
    thing-admin.service.ts    |     100 |    90.62 |     100 |     100 | 202,318           
    thing.controller.ts       |     100 |      100 |     100 |     100 |                           
  ------------------------------|---------|----------|---------|---------|-------------------

Jest Version 30

------------------------------|---------|----------|---------|---------|-------------------
File                          | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------------------------|---------|----------|---------|---------|-------------------
All files                     |   98.56 |    83.33 |   96.07 |   98.42 |                           
 thing                      |   98.24 |    81.33 |   95.65 |   98.07 |                   
  thing.consumer.ts   |     100 |       75 |     100 |     100 | 11-30                          
  thing-admin.service.ts    |     100 |     87.5 |     100 |     100 | 17,202-263,318    
  thing.controller.ts       |     100 |       75 |     100 |     100 | 34-59             
  thing.service.ts          |    90.9 |    92.85 |      80 |      90 | 127-138           
------------------------------|---------|----------|---------|---------|-------------------

Actual behavior

Branch coverage drops to 75% across multiple controllers and consumers with no changes in code functionality

Additional context

No response

Environment

System:
    OS: macOS 14.7.6
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 18.19.0 
    npm: 8.19.4
  npmPackages:
    jest: ^30.0.0 => 30.0.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions