Add tests for the AsyncFunction
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
import {callAsyncFunction} from '../src/async-function'
|
||||||
|
|
||||||
|
describe('callAsyncFunction', () => {
|
||||||
|
test('calls the function with its arguments', async () => {
|
||||||
|
const result = await callAsyncFunction({foo: 'bar'}, 'return foo')
|
||||||
|
expect(result).toEqual('bar')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('throws on ReferenceError', async () => {
|
||||||
|
expect.assertions(1)
|
||||||
|
|
||||||
|
try {
|
||||||
|
await callAsyncFunction({}, 'proces')
|
||||||
|
} catch (err) {
|
||||||
|
expect(err).toBeInstanceOf(ReferenceError)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test('can access process', async () => {
|
||||||
|
await callAsyncFunction({}, 'process')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('can access console', async () => {
|
||||||
|
await callAsyncFunction({}, 'console')
|
||||||
|
})
|
||||||
|
})
|
||||||
Vendored
+101
-71
File diff suppressed because one or more lines are too long
Generated
+4502
-14
File diff suppressed because it is too large
Load Diff
+27
-8
@@ -1,24 +1,43 @@
|
|||||||
{
|
{
|
||||||
"name": "github-script",
|
"name": "github-script",
|
||||||
|
"description": "A GitHub action for executing a simple script",
|
||||||
"version": "0.6.0",
|
"version": "0.6.0",
|
||||||
"private": true,
|
|
||||||
"main": "dist/index.js",
|
|
||||||
"scripts": {
|
|
||||||
"build": "ncc build src/main.ts"
|
|
||||||
},
|
|
||||||
"author": "GitHub",
|
"author": "GitHub",
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.2.2",
|
"@actions/core": "^1.2.2",
|
||||||
"@actions/github": "^2.1.1"
|
"@actions/github": "^2.1.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@zeit/ncc": "^0.20.5",
|
"@types/jest": "^25.1.3",
|
||||||
"husky": "^4.0.1"
|
"@zeit/ncc": "^0.21.1",
|
||||||
|
"husky": "^4.2.3",
|
||||||
|
"jest": "^25.1.0",
|
||||||
|
"ts-jest": "^25.2.1",
|
||||||
|
"typescript": "^3.8.2"
|
||||||
},
|
},
|
||||||
"husky": {
|
"husky": {
|
||||||
"hooks": {
|
"hooks": {
|
||||||
"pre-commit": "npm run build && git add dist/"
|
"pre-commit": "npm run build && git add dist/"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"jest": {
|
||||||
|
"preset": "ts-jest",
|
||||||
|
"testEnvironment": "node",
|
||||||
|
"globals": {
|
||||||
|
"ts-jest": {
|
||||||
|
"diagnostics": {
|
||||||
|
"ignoreCodes": [
|
||||||
|
"151001"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"license": "MIT",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"build": "ncc build src/main.ts",
|
||||||
|
"test": "jest"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor
|
||||||
|
|
||||||
|
type AsyncFunctionArguments = {[key: string]: any}
|
||||||
|
|
||||||
|
export async function callAsyncFunction(
|
||||||
|
args: AsyncFunctionArguments,
|
||||||
|
source: string
|
||||||
|
): Promise<any> {
|
||||||
|
const fn = new AsyncFunction(...Object.keys(args), source)
|
||||||
|
return fn(...Object.values(args))
|
||||||
|
}
|
||||||
+8
-4
@@ -1,11 +1,11 @@
|
|||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import {context, GitHub} from '@actions/github'
|
import {context, GitHub} from '@actions/github'
|
||||||
|
import {callAsyncFunction} from './async-function'
|
||||||
|
|
||||||
process.on('unhandledRejection', handleError)
|
process.on('unhandledRejection', handleError)
|
||||||
main().catch(handleError)
|
main().catch(handleError)
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor
|
|
||||||
const token = core.getInput('github-token', {required: true})
|
const token = core.getInput('github-token', {required: true})
|
||||||
const debug = core.getInput('debug')
|
const debug = core.getInput('debug')
|
||||||
const userAgent = core.getInput('user-agent')
|
const userAgent = core.getInput('user-agent')
|
||||||
@@ -14,10 +14,14 @@ async function main() {
|
|||||||
if (debug === 'true') opts.log = console
|
if (debug === 'true') opts.log = console
|
||||||
if (userAgent != null) opts.userAgent = userAgent
|
if (userAgent != null) opts.userAgent = userAgent
|
||||||
if (previews != null) opts.previews = previews.split(',')
|
if (previews != null) opts.previews = previews.split(',')
|
||||||
const client = new GitHub(token, opts)
|
const github = new GitHub(token, opts)
|
||||||
const script = core.getInput('script', {required: true})
|
const script = core.getInput('script', {required: true})
|
||||||
const fn = new AsyncFunction('require', 'github', 'context', script)
|
|
||||||
const result = await fn(require, client, context)
|
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilatin errors.
|
||||||
|
const result = callAsyncFunction(
|
||||||
|
{require: require, github: github, context: context},
|
||||||
|
script
|
||||||
|
)
|
||||||
|
|
||||||
let encoding = core.getInput('result-encoding')
|
let encoding = core.getInput('result-encoding')
|
||||||
encoding = encoding ? encoding : 'json'
|
encoding = encoding ? encoding : 'json'
|
||||||
|
|||||||
+2
-1
@@ -4,5 +4,6 @@
|
|||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"forceConsistentCasingInFileNames": true
|
"forceConsistentCasingInFileNames": true
|
||||||
}
|
},
|
||||||
|
"exclude": ["__test__"]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user