···20202121You should use mock assertions (e.g., [`toHaveBeenCalled`](/api/expect#tohavebeencalled)) on [`expect`](/api/expect) to assert mock results. This API reference describes available properties and methods to manipulate mock behavior.
22222323+::: warning IMPORTANT
2424+Vitest spies inherit implementation's `length` property. This means that `length` can be different from the original implementation:
2525+2626+```ts
2727+const example = {
2828+ fn(arg1, arg2) {
2929+ // ...
3030+ }
3131+}
3232+3333+const fn = vi.spyOn(example, 'fn')
3434+fn.length // == 2
3535+3636+fn.mockImplementation(() => {})
3737+fn.length // == 0
3838+```
3939+:::
4040+2341::: tip
2442The custom function implementation in the types below is marked with a generic `<T>`.
2543:::
···12121313 // creates a new mocked function with no formal arguments.
1414 expect(example.square.name).toEqual('square')
1515- expect(example.square.length).toEqual(0)
1515+ expect(example.square.length).toEqual(2)
16161717 // async functions get the same treatment as standard synchronous functions.
1818 expect(example.asyncSquare.name).toEqual('asyncSquare')
1919- expect(example.asyncSquare.length).toEqual(0)
1919+ expect(example.asyncSquare.length).toEqual(2)
20202121 // creates a new class with the same interface, member functions and properties are mocked.
2222 expect(example.someClasses.constructor.name).toEqual('Bar')
+2-2
test/core/test/mocking/automocking.spec.ts
···13131414 // creates a new mocked function with no formal arguments.
1515 expect(example.square.name).toEqual('square')
1616- expect(example.square.length).toEqual(0)
1616+ expect(example.square.length).toEqual(2)
17171818 // async functions get the same treatment as standard synchronous functions.
1919 expect(example.asyncSquare.name).toEqual('asyncSquare')
2020- expect(example.asyncSquare.length).toEqual(0)
2020+ expect(example.asyncSquare.length).toEqual(2)
21212222 // creates a new class with the same interface, member functions and properties are mocked.
2323 expect(example.someClasses.constructor.name).toEqual('Bar')