@@ -8,6 +8,94 @@ describe('callAsyncFunction', () => {
88 expect ( result ) . toEqual ( 'bar' )
99 } )
1010
11+ test ( 'passes getOctokit through the script context' , async ( ) => {
12+ const getOctokit = jest . fn ( ) . mockReturnValue ( 'secondary-client' )
13+
14+ const result = await callAsyncFunction (
15+ { getOctokit} as any ,
16+ "return getOctokit('token')"
17+ )
18+
19+ expect ( getOctokit ) . toHaveBeenCalledWith ( 'token' )
20+ expect ( result ) . toEqual ( 'secondary-client' )
21+ } )
22+
23+ test ( 'getOctokit creates client independent from github' , async ( ) => {
24+ const github = { rest : { issues : 'primary' } }
25+ const getOctokit = jest . fn ( ) . mockReturnValue ( { rest : { issues : 'secondary' } } )
26+
27+ const result = await callAsyncFunction (
28+ { github, getOctokit} as any ,
29+ `
30+ const secondary = getOctokit('other-token')
31+ return {
32+ primary: github.rest.issues,
33+ secondary: secondary.rest.issues,
34+ different: github !== secondary
35+ }
36+ `
37+ )
38+
39+ expect ( result ) . toEqual ( {
40+ primary : 'primary' ,
41+ secondary : 'secondary' ,
42+ different : true
43+ } )
44+ expect ( getOctokit ) . toHaveBeenCalledWith ( 'other-token' )
45+ } )
46+
47+ test ( 'getOctokit passes options through' , async ( ) => {
48+ const getOctokit = jest . fn ( ) . mockReturnValue ( 'client-with-opts' )
49+
50+ const result = await callAsyncFunction (
51+ { getOctokit} as any ,
52+ `return getOctokit('my-token', { baseUrl: 'https://ghes.example.com/api/v3' })`
53+ )
54+
55+ expect ( getOctokit ) . toHaveBeenCalledWith ( 'my-token' , {
56+ baseUrl : 'https://ghes.example.com/api/v3'
57+ } )
58+ expect ( result ) . toEqual ( 'client-with-opts' )
59+ } )
60+
61+ test ( 'getOctokit supports plugins' , async ( ) => {
62+ const getOctokit = jest . fn ( ) . mockReturnValue ( 'client-with-plugins' )
63+
64+ const result = await callAsyncFunction (
65+ { getOctokit} as any ,
66+ `return getOctokit('my-token', { previews: ['v3'] }, 'pluginA', 'pluginB')`
67+ )
68+
69+ expect ( getOctokit ) . toHaveBeenCalledWith (
70+ 'my-token' ,
71+ { previews : [ 'v3' ] } ,
72+ 'pluginA' ,
73+ 'pluginB'
74+ )
75+ expect ( result ) . toEqual ( 'client-with-plugins' )
76+ } )
77+
78+ test ( 'multiple getOctokit calls produce independent clients' , async ( ) => {
79+ const getOctokit = jest
80+ . fn ( )
81+ . mockReturnValueOnce ( { id : 'client-a' } )
82+ . mockReturnValueOnce ( { id : 'client-b' } )
83+
84+ const result = await callAsyncFunction (
85+ { getOctokit} as any ,
86+ `
87+ const a = getOctokit('token-a')
88+ const b = getOctokit('token-b')
89+ return { a: a.id, b: b.id, different: a !== b }
90+ `
91+ )
92+
93+ expect ( getOctokit ) . toHaveBeenCalledTimes ( 2 )
94+ expect ( getOctokit ) . toHaveBeenNthCalledWith ( 1 , 'token-a' )
95+ expect ( getOctokit ) . toHaveBeenNthCalledWith ( 2 , 'token-b' )
96+ expect ( result ) . toEqual ( { a : 'client-a' , b : 'client-b' , different : true } )
97+ } )
98+
1199 test ( 'throws on ReferenceError' , async ( ) => {
12100 expect . assertions ( 1 )
13101
@@ -25,4 +113,22 @@ describe('callAsyncFunction', () => {
25113 test ( 'can access console' , async ( ) => {
26114 await callAsyncFunction ( { } as any , 'console' )
27115 } )
116+
117+ test ( 'injected names are accessible when not redeclared' , async ( ) => {
118+ const getOctokit = jest . fn ( ) . mockReturnValue ( 'from-injected' )
119+
120+ const result = await callAsyncFunction (
121+ { getOctokit} as any ,
122+ `return getOctokit('token')`
123+ )
124+
125+ expect ( result ) . toEqual ( 'from-injected' )
126+ expect ( getOctokit ) . toHaveBeenCalledWith ( 'token' )
127+ } )
128+
129+ test ( 'syntax errors in user code still throw' , ( ) => {
130+ expect ( ( ) => callAsyncFunction ( { } as any , 'const x = {' ) ) . toThrow (
131+ SyntaxError
132+ )
133+ } )
28134} )
0 commit comments