mirror of
https://github.com/actions/github-script.git
synced 2026-04-09 07:30:05 +00:00
- Rename context binding from getOctokit to createOctokit to avoid
SyntaxError when users write const { getOctokit } = require(...)
in their scripts (~10 public workflows affected)
- Strip undefined values from user options to prevent clobbering
defaults (e.g. GHES baseUrl)
- Deep-merge retry options alongside request options
- Use nullish coalescing (??) instead of logical OR (||)
- Shallow-copy opts to prevent shared reference mutation
- Add tests: undefined stripping, retry merge, falsy value preservation,
no mutation of defaults
- 32 tests passing, lint clean, dist rebuilt
118 lines
3.4 KiB
TypeScript
118 lines
3.4 KiB
TypeScript
/* 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')
|
|
})
|
|
})
|