vitest-dev / vitest-dev/vitest
Custom Type not recognized with `instanceof`
Nobody has claimed this yet.
- 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
- This issue is similar to https://github.com/vitest-dev/vitest/issues/3676
- Might be related to https://github.com/vitest-dev/vitest/issues/4559
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:
- The
instanceofis in src/app.ts - The error is generated in src/routes/debug/index.ts.
This file is not explicitly imported. It is loaded dynamically by fastify-autoload in src/app.ts:
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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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