-
Notifications
You must be signed in to change notification settings - Fork 553
Expand file tree
/
Copy pathasync-function.test.ts
More file actions
118 lines (97 loc) · 3.43 KB
/
async-function.test.ts
File metadata and controls
118 lines (97 loc) · 3.43 KB
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
/* eslint-disable @typescript-eslint/no-explicit-any */
import {callAsyncFunction} from '../src/async-function'
describe('callAsyncFunction', () => {
test('calls the function with its arguments', async () => {
const result = await callAsyncFunction({foo: 'bar'} as any, 'return foo')
expect(result).toEqual('bar')
})
test('passes createOctokit through the script context', async () => {
const createOctokit = jest.fn().mockReturnValue('secondary-client')
const result = await callAsyncFunction(
{createOctokit} as any,
"return createOctokit('token')"
)
expect(createOctokit).toHaveBeenCalledWith('token')
expect(result).toEqual('secondary-client')
})
test('createOctokit creates client independent from github', async () => {
const github = {rest: {issues: 'primary'}}
const createOctokit = jest
.fn()
.mockReturnValue({rest: {issues: 'secondary'}})
const result = await callAsyncFunction(
{github, createOctokit} as any,
`
const secondary = createOctokit('other-token')
return {
primary: github.rest.issues,
secondary: secondary.rest.issues,
different: github !== secondary
}
`
)
expect(result).toEqual({
primary: 'primary',
secondary: 'secondary',
different: true
})
expect(createOctokit).toHaveBeenCalledWith('other-token')
})
test('createOctokit passes options through', async () => {
const createOctokit = jest.fn().mockReturnValue('client-with-opts')
const result = await callAsyncFunction(
{createOctokit} as any,
`return createOctokit('my-token', { baseUrl: 'https://ghes.example.com/api/v3' })`
)
expect(createOctokit).toHaveBeenCalledWith('my-token', {
baseUrl: 'https://ghes.example.com/api/v3'
})
expect(result).toEqual('client-with-opts')
})
test('createOctokit supports plugins', async () => {
const createOctokit = jest.fn().mockReturnValue('client-with-plugins')
const result = await callAsyncFunction(
{createOctokit} as any,
`return createOctokit('my-token', { previews: ['v3'] }, 'pluginA', 'pluginB')`
)
expect(createOctokit).toHaveBeenCalledWith(
'my-token',
{previews: ['v3']},
'pluginA',
'pluginB'
)
expect(result).toEqual('client-with-plugins')
})
test('multiple createOctokit calls produce independent clients', async () => {
const createOctokit = jest
.fn()
.mockReturnValueOnce({id: 'client-a'})
.mockReturnValueOnce({id: 'client-b'})
const result = await callAsyncFunction(
{createOctokit} as any,
`
const a = createOctokit('token-a')
const b = createOctokit('token-b')
return { a: a.id, b: b.id, different: a !== b }
`
)
expect(createOctokit).toHaveBeenCalledTimes(2)
expect(createOctokit).toHaveBeenNthCalledWith(1, 'token-a')
expect(createOctokit).toHaveBeenNthCalledWith(2, 'token-b')
expect(result).toEqual({a: 'client-a', b: 'client-b', different: true})
})
test('throws on ReferenceError', async () => {
expect.assertions(1)
try {
await callAsyncFunction({} as any, 'proces')
} catch (err) {
expect(err).toBeInstanceOf(ReferenceError)
}
})
test('can access process', async () => {
await callAsyncFunction({} as any, 'process')
})
test('can access console', async () => {
await callAsyncFunction({} as any, 'console')
})
})