Skip to content

Static Functions on ES6 Classes are stripped from the mock #8948

@mattpen

Description

@mattpen

Describe the bug

If you mock a Class with vi.fn(), the mocked class has all static functions removed. Similar to #8850, except that typescript can find the functions but the runtime cannot.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-mjassekr?file=test%2Fbasic.test.ts

class Dog {
    public static speak(): string {
      return 'Woof!';
    }
  }
  const MockDog = vi.fn( Dog );
  console.log( 'Dog.speak()', Dog.speak );
  console.log( 'MockDog.speak()', MockDog.speak );

Actual Result:

stdout | dogtest.ts
Dog.speak() [Function: speak]
MockDog.speak() undefined

Expected Result:

stdout | dogtest.ts
Dog.speak() [Function: speak]
MockDog.speak() [Function: speak]

System Info

$ npx envinfo --system --npmPackages '{vitest*,@vitest/*,vite,@vitejs/*,playwright,webdriverio}' --binaries --browsers

  System:
    OS: macOS 26.0.1
    CPU: (8) arm64 Apple M1
    Memory: 92.83 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.21.0 - /Users/matt/.nvm/versions/node/v22.21.0/bin/node
    npm: 10.5.2 - /usr/local/bin/npm
  Browsers:
    Chrome: 142.0.7444.60
    Firefox: 143.0.1
    Safari: 26.0.1
  npmPackages:
    @vitest/coverage-v8: ^3.2.4 => 3.2.4 
    vitest: ^4.0.7 => 4.0.7

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions