Skip to content

Vitest vi.mock() auto-mocks are being detected as unused files #251

@boroth

Description

@boroth

Problem

When using the __mocks__/ directory along with vi.mock('path/to/file.ts'), the auto-loaded mock is still treated as an unused file (e.g. path/to/__mocks__/file.ts).

For this test:

import { getAppFactory } from '@/src/common/AppFactory/ts/getAppFactory';

vi.mock('@/src/common/AppFactory/ts/getAppFactory');
describe(() => {
   ...
});

I get this alert for the unused file:

    {
      "path": "src/common/AppFactory/ts/__mocks__/getAppFactory.ts",
      "actions": [
        {
          "type": "delete-file",
          "auto_fixable": false,
          "description": "Delete this file",
          "note": "File deletion may remove runtime functionality not visible to static analysis"
        },
        {
          "type": "suppress-file",
          "auto_fixable": false,
          "description": "Suppress with a file-level comment at the top of the file",
          "comment": "// fallow-ignore-file unused-file"
        }
      ]
    },

Proposed solution

I think the vitest plugin should be able to identify these vi.mock() side/auto-loaded mocks as used files.

Alternatives considered

For now, just ignoring all __mocks__/ directories.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions