Skip to content

Auto mocking is unsafe according to the linter. #3288

@codiophile

Description

@codiophile

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

The linter errors if you use auto mocking: https://docs.nestjs.com/fundamentals/testing#auto-mocking

src/app.controller.spec.ts
  22:17  error  Unsafe assignment of an `any` value            @typescript-eslint/no-unsafe-assignment
  23:11  error  Unsafe return of a value of type `any`         @typescript-eslint/no-unsafe-return
  23:18  error  Unsafe construction of a(n) `any` typed value  @typescript-eslint/no-unsafe-call

Minimum reproduction code

https://docs.nestjs.com/fundamentals/testing#auto-mocking

Steps to reproduce

  1. Create new NestJS project
  2. Add auto mocking to a test (https://docs.nestjs.com/fundamentals/testing#auto-mocking)
  3. Run npm run lint

Expected behavior

Using code from the official documentation shouldn't cause the linter to fail. Either offer code that the linter is happy with or include instructions on which rules to disable and where, to keep the linter from complaining.

I don't know enough about how auto mocking works, to know if there is a type safe way of doing it, but that would be the ideal solution. If it is necessary to disable some rules, I would prefer if it is done in a way that doesn't affect the rest of the code.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

11.0.7

Packages versions

[System Information]
OS Version     : macOS24.5.0
NodeJS Version : v22.9.0
NPM Version    : 10.8.3 

[Nest CLI]
Nest CLI Version : 11.0.7 

[Nest Platform Information
platform-express version : 11.1.3
schematics version       : 11.0.5
testing version          : 11.1.3
common version           : 11.1.3
core version             : 11.1.3
cli version              : 11.0.7

Node.js version

No response

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions