Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/api/mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ getApplesSpy.mock.calls.length === 1

You 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.

::: warning IMPORTANT
Vitest spies inherit implementation's `length` property. This means that `length` can be different from the original implementation:

```ts
const example = {
fn(arg1, arg2) {
// ...
}
}

const fn = vi.spyOn(example, 'fn')
fn.length // == 2

fn.mockImplementation(() => {})
fn.length // == 0
```
:::

::: tip
The custom function implementation in the types below is marked with a generic `<T>`.
:::
Expand Down
19 changes: 19 additions & 0 deletions packages/spy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,25 @@ function createMock(
if (original) {
copyOriginalStaticProperties(namedObject[name], original)
}
let overrideLength: number | undefined
Object.defineProperty(namedObject[name], 'length', {
configurable: true,
get: () => {
if (overrideLength != null) {
return overrideLength
}

const implementation = config.onceMockImplementations[0]
|| config.mockImplementation
|| prototypeConfig?.onceMockImplementations[0]
|| prototypeConfig?.mockImplementation
|| original
return implementation?.length ?? 0
},
set: (length: number) => {
overrideLength = length
},
})
return namedObject[name]
}

Expand Down
4 changes: 2 additions & 2 deletions test/browser/fixtures/mocking/import-mock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ test('all mocked are valid', async () => {

// creates a new mocked function with no formal arguments.
expect(example.square.name).toEqual('square')
expect(example.square.length).toEqual(0)
expect(example.square.length).toEqual(2)

// async functions get the same treatment as standard synchronous functions.
expect(example.asyncSquare.name).toEqual('asyncSquare')
expect(example.asyncSquare.length).toEqual(0)
expect(example.asyncSquare.length).toEqual(2)

// creates a new class with the same interface, member functions and properties are mocked.
expect(example.someClasses.constructor.name).toEqual('Bar')
Expand Down
4 changes: 2 additions & 2 deletions test/core/test/mocking/automocking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ test('all mocked are valid', async () => {

// creates a new mocked function with no formal arguments.
expect(example.square.name).toEqual('square')
expect(example.square.length).toEqual(0)
expect(example.square.length).toEqual(2)

// async functions get the same treatment as standard synchronous functions.
expect(example.asyncSquare.name).toEqual('asyncSquare')
expect(example.asyncSquare.length).toEqual(0)
expect(example.asyncSquare.length).toEqual(2)

// creates a new class with the same interface, member functions and properties are mocked.
expect(example.someClasses.constructor.name).toEqual('Bar')
Expand Down
24 changes: 24 additions & 0 deletions test/core/test/mocking/vi-fn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ test('vi.fn().mock cannot be overriden', () => {
}).toThrowError()
})

test('vi.fn() has correct length', () => {
const fn0 = vi.fn(() => {})
expect(fn0.length).toBe(0)

const fnArgs = vi.fn((..._args) => {})
expect(fnArgs.length).toBe(0)

const fn1 = vi.fn((_arg1) => {})
expect(fn1.length).toBe(1)

const fn2 = vi.fn((_arg1, _arg2) => {})
expect(fn2.length).toBe(2)

const fn3 = vi.fn((_arg1, _arg2, _arg3) => {})
expect(fn3.length).toBe(3)
})

test('vi.fn() has overridable length', () => {
const fn0 = vi.fn(() => {})
// @ts-expect-error TS doesn't allow override
fn0.length = 5
expect(fn0.length).toBe(5)
})

describe('vi.fn() state', () => {
// TODO: test when calls is not empty
test('vi.fn() clears calls without a custom implementation', () => {
Expand Down
17 changes: 17 additions & 0 deletions test/core/test/mocking/vi-spyOn.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import type { MockContext } from 'vitest'
import { describe, expect, test, vi } from 'vitest'

test('vi.fn() has correct length', () => {
const fn0 = vi.spyOn({ fn: () => {} }, 'fn')
expect(fn0.length).toBe(0)

const fnArgs = vi.spyOn({ fn: (..._args: any[]) => {} }, 'fn')
expect(fnArgs.length).toBe(0)

const fn1 = vi.spyOn({ fn: (_arg1: any) => {} }, 'fn')
expect(fn1.length).toBe(1)

const fn2 = vi.spyOn({ fn: (_arg1: any, _arg2: any) => {} }, 'fn')
expect(fn2.length).toBe(2)

const fn3 = vi.spyOn({ fn: (_arg1: any, _arg2: any, _arg3: any) => {} }, 'fn')
expect(fn3.length).toBe(3)
})

describe('vi.spyOn() state', () => {
test('vi.spyOn() spies on an object and tracks the calls', () => {
const object = createObject()
Expand Down
Loading