vitest-dev / vitest-dev/vitest

Custom Type not recognized with `instanceof`

Open
#8,289 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 21h
Merged PRs (30d)
92

Description

Describe the bug

I'm using instanceof to check whether a variable is an instance of a custom SignatureError class (class SignatureError extends Error).

This check works correctly when running the code directly with Node at runtime, and also when using the Node test runner.

As pointed out in a previous issue on fastify/fastify-autoload (https://github.com/fastify/fastify-autoload/issues/472#issuecomment-3057127685),
the issue might come from module cache inconsistencies due to different import paths:

the import cache is considered the relative and absolute path of the same module as two different file.
You can observe we are using dynamic import with absolute path, and you are using relative path.

Additional context


Reproduction

Code: https://github.com/marcalexiei/fastify-autoload-vitest-instanceof-bug

GitHub action run: https://github.com/marcalexiei/fastify-autoload-vitest-instanceof-bug/actions/runs/16197482983/job/45727664415


The SignatureError class is always imported from the same file:

it's used in:

System Info
System:
    OS: macOS 15.5
    CPU: (12) arm64 Apple M2 Max
    Memory: 5.34 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.3.0 - ~/.nvm/versions/node/v24.3.0/bin/node
    npm: 11.4.2 - ~/.nvm/versions/node/v24.3.0/bin/npm
    bun: 1.0.17 - ~/.bun/bin/bun
  Browsers:
    Chrome: 138.0.7204.93
    Safari: 18.5
  npmPackages:
    vitest: ^3.2.4 => 3.2.4
Used Package Manager

npm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the linked minimal reproduction first, then inspect src/SignatureError.ts, src/app.ts, and src/routes/debug/index.ts, including the dynamic loading path through fastify-autoload. Trace how Vitest resolves and caches the module in this scenario. Done means the reproduction's instanceof check recognizes the generated SignatureError under Vitest without regressing the Node runtime or test runner behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
devtools, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.