forked from vitest-dev/vitest
/
mocked.test.ts
65 lines (51 loc) · 1.88 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
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 } from '../src/mockedC'
import * as globalMock from '../src/global-mock'
vitest.mock('../src/submodule')
vitest.mock('virtual-module', () => ({ value: 'mock' }))
vitest.mock('../src/mockedC')
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')
})
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('async functions should be mocked', () => {
expect(asyncFunc()).toBeUndefined()
expect(vi.mocked(asyncFunc).mockResolvedValue).toBeDefined()
vi.mocked(asyncFunc).mockResolvedValue('foo')
expect(asyncFunc()).resolves.toBe('foo')
})