Describe the bug
TypeScript allows accessing private and protected methods using bracket notation. This is useful for unit testing, if you want to check a private/protected method works as expected.
Vitest does not allow using spyOn on a private/protected method (e.g. you want to test a public method that uses a private method to load data) unless you cast to any. So vi.spyOn(myObject, 'privateMethod') does not work while vi.spyOn(myObject as any, 'privateMethod') works as expected. But this comes with the caveat that Vitest does not know about the types this function returns.
See the linked StackBlitz for an example.
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-ucls7umq
System Info
System:
OS: macOS 26.3.1
CPU: (8) arm64 Apple M2
Memory: 257.30 MB / 24.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 24.6.0 - /Users/jakob/.nvm/versions/node/v24.6.0/bin/node
npm: 11.12.1 - /Users/jakob/.nvm/versions/node/v24.6.0/bin/npm
bun: 1.2.18 - /Users/jakob/.bun/bin/bun
Browsers:
Chrome: 147.0.7727.56
Edge: 147.0.3912.72
Firefox: 149.0.2
Firefox Nightly: 149.0a1
Safari: 26.3.1
npmPackages:
@vitest/browser-playwright: ^4.1.2 => 4.1.3
@vitest/coverage-v8: ^4.1.2 => 4.1.3
playwright: ^1.58.0 => 1.59.1
vitest: ^4.1.2 => 4.1.3
Used Package Manager
npm
Validations
Describe the bug
TypeScript allows accessing private and protected methods using bracket notation. This is useful for unit testing, if you want to check a private/protected method works as expected.
Vitest does not allow using
spyOnon a private/protected method (e.g. you want to test a public method that uses a private method to load data) unless you cast to any. Sovi.spyOn(myObject, 'privateMethod')does not work whilevi.spyOn(myObject as any, 'privateMethod')works as expected. But this comes with the caveat that Vitest does not know about the types this function returns.See the linked StackBlitz for an example.
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-ucls7umq
System Info
System: OS: macOS 26.3.1 CPU: (8) arm64 Apple M2 Memory: 257.30 MB / 24.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 24.6.0 - /Users/jakob/.nvm/versions/node/v24.6.0/bin/node npm: 11.12.1 - /Users/jakob/.nvm/versions/node/v24.6.0/bin/npm bun: 1.2.18 - /Users/jakob/.bun/bin/bun Browsers: Chrome: 147.0.7727.56 Edge: 147.0.3912.72 Firefox: 149.0.2 Firefox Nightly: 149.0a1 Safari: 26.3.1 npmPackages: @vitest/browser-playwright: ^4.1.2 => 4.1.3 @vitest/coverage-v8: ^4.1.2 => 4.1.3 playwright: ^1.58.0 => 1.59.1 vitest: ^4.1.2 => 4.1.3Used Package Manager
npm
Validations