-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
mocked-class-restore-explicit.test.ts
86 lines (78 loc) · 1.86 KB
/
mocked-class-restore-explicit.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
import { expect, test, vi } from 'vitest'
import { MockedE } from '../src/mockedE'
vi.mock('../src/mockedE')
// this behavior looks odd but jest also doesn't seem to support this use case properly
test(`mocked class methods are not restorable by explicit mockRestore calls`, () => {
const instance1 = new MockedE()
expect(instance1.testFn('a')).toMatchInlineSnapshot(`undefined`)
expect(vi.mocked(instance1.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"a",
],
]
`)
expect(vi.mocked(MockedE.prototype.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"a",
],
]
`)
// restoring instance method
vi.mocked(instance1.testFn).mockRestore()
expect(vi.mocked(instance1.testFn).mock.calls).toMatchInlineSnapshot(`[]`)
expect(vi.mocked(MockedE.prototype.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"a",
],
]
`)
expect(instance1.testFn('b')).toMatchInlineSnapshot(`undefined`)
expect(vi.mocked(instance1.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"b",
],
]
`)
expect(vi.mocked(MockedE.prototype.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"a",
],
[
"b",
],
]
`)
// restoring prototype method
vi.mocked(MockedE.prototype.testFn).mockRestore()
expect(vi.mocked(instance1.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"b",
],
]
`)
expect(vi.mocked(MockedE.prototype.testFn).mock.calls).toMatchInlineSnapshot(`[]`)
expect(instance1.testFn('c')).toMatchInlineSnapshot(`undefined`)
expect(vi.mocked(instance1.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"b",
],
[
"c",
],
]
`)
expect(vi.mocked(MockedE.prototype.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"c",
],
]
`)
})