/
mocked.test.ts
169 lines (138 loc) · 5.2 KB
/
mocked.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { assert, describe, expect, test, vi, vitest } from 'vitest'
// @ts-expect-error not typed module
import { value as virtualValue } from 'virtual-module'
import { two } from '../src/submodule'
import * as mocked from '../src/mockedA'
import { mockedB } from '../src/mockedB'
import { MockedC, asyncFunc, exportedStream } from '../src/mockedC'
import MockedDefault, { MockedC as MockedD } from '../src/mockedD'
import * as globalMock from '../src/global-mock'
vitest.mock('../src/submodule')
vitest.mock('virtual-module', () => ({ value: 'mock' }))
vitest.mock('../src/mockedC')
vitest.mock('../src/mockedD')
/**
* Get a property descriptor from an object.
*
* This is different from `Object.getOwnPropertyDescriptor` because it recurses
* into the prototype chain until it either finds a match or reaches the end.
*
* @param object The object that contains the property.
* @param property The property.
* @returns The property's descriptor, or undefined if no matching property was found.
*/
function getPropertyDescriptor(object: any, property: PropertyKey) {
for (let o = object; o; o = Object.getPrototypeOf(o)) {
const descriptor = Object.getOwnPropertyDescriptor(o, property)
if (descriptor)
return descriptor
}
return undefined
}
test('submodule is mocked to return "two" as 3', () => {
assert.equal(3, two)
})
test('globally mocked files are mocked', () => {
// Mocked in setup.ts
expect(globalMock.mocked).toBe(true)
})
test('can mock esm', () => {
const spy = vi.spyOn(mocked, 'mockedA')
mockedB()
expect(spy).toHaveBeenCalled()
expect(spy).toHaveReturnedWith('A')
})
test('mocked exports should override original exports', () => {
expect(virtualValue).toBe('mock')
})
describe('mocked classes', () => {
test('should not delete the prototype', () => {
expect(MockedC).toBeTypeOf('function')
expect(MockedC.prototype.doSomething).toBeTypeOf('function')
expect(MockedC.prototype.constructor).toBe(MockedC)
})
test('should mock the constructor', () => {
const instance = new MockedC()
expect(instance.value).not.toBe(42)
expect(MockedC).toHaveBeenCalledOnce()
})
test('should mock functions in the prototype', () => {
const instance = new MockedC()
expect(instance.doSomething).toBeTypeOf('function')
expect(instance.doSomething()).not.toBe('A')
expect(MockedC.prototype.doSomething).toHaveBeenCalledOnce()
expect(MockedC.prototype.doSomething).not.toHaveReturnedWith('A')
})
test('should mock getters', () => {
const instance = new MockedC()
expect(instance).toHaveProperty('getOnlyProp')
const descriptor = getPropertyDescriptor(instance, 'getOnlyProp')
expect(descriptor?.get).toBeDefined()
expect(descriptor?.set).not.toBeDefined()
expect(instance.getOnlyProp).toBe(42)
// @ts-expect-error Assign to the read-only prop to ensure it errors.
expect(() => instance.getOnlyProp = 4).toThrow()
const getterSpy = vi.spyOn(instance, 'getOnlyProp', 'get').mockReturnValue(456)
expect(instance.getOnlyProp).toEqual(456)
expect(getterSpy).toHaveBeenCalledOnce()
})
test('should mock getters and setters', () => {
const instance = new MockedC()
expect(instance).toHaveProperty('getSetProp')
const descriptor = getPropertyDescriptor(instance, 'getSetProp')
expect(descriptor?.get).toBeDefined()
expect(descriptor?.set).toBeDefined()
expect(instance.getSetProp).toBe(123)
expect(() => instance.getSetProp = 4).not.toThrow()
const getterSpy = vi.spyOn(instance, 'getSetProp', 'get').mockReturnValue(789)
expect(instance.getSetProp).toEqual(789)
expect(getterSpy).toHaveBeenCalledOnce()
const setterSpy = vi.spyOn(instance, 'getSetProp', 'set')
instance.getSetProp = 159
expect(setterSpy).toHaveBeenCalledWith(159)
})
})
describe('default exported classes', () => {
test('should preserve equality for re-exports', () => {
expect(MockedDefault).toEqual(MockedD)
})
test('should preserve prototype', () => {
expect(MockedDefault.prototype.constructor).toBe(MockedDefault)
expect(MockedD.prototype.constructor).toBe(MockedD)
})
})
test('async functions should be mocked', () => {
expect(asyncFunc()).toBeUndefined()
expect(vi.mocked(asyncFunc).mockResolvedValue).toBeDefined()
vi.mocked(asyncFunc).mockResolvedValue('foo')
expect(asyncFunc()).resolves.toBe('foo')
})
describe('mocked function which fails on toReturnWith', () => {
test('zero call', () => {
const mock = vi.fn(() => 1)
expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingSnapshot()
})
test('just one call', () => {
const mock = vi.fn(() => 1)
mock()
expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingSnapshot()
})
test('multi calls', () => {
const mock = vi.fn(() => 1)
mock()
mock()
mock()
expect(() => expect(mock).toReturnWith(2)).toThrowErrorMatchingSnapshot()
})
test('oject type', () => {
const mock = vi.fn(() => { return { a: '1' } })
mock()
mock()
mock()
expect(() => expect(mock).toReturnWith({ a: '4' })).toThrowErrorMatchingSnapshot()
})
})
// This is here because mocking streams previously caused some problems (#1671).
test('streams', () => {
expect(exportedStream).toBeDefined()
})