Skip to content

Commit

Permalink
feat(cloud-function): add unit tests for function engine;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Nov 4, 2021
1 parent e5ecf6d commit 40a9ae8
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 8 deletions.
14 changes: 7 additions & 7 deletions packages/cloud-function/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cloud-function/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"devDependencies": {
"@types/express": "^4.17.11",
"@types/node": "^14.14.37",
"@types/node": "^16.11.6",
"typescript": "^4.2.3"
}
}
131 changes: 131 additions & 0 deletions packages/cloud-function/tests/engine.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

const assert = require('assert')
const { FunctionEngine } = require('../dist/engine')
const vm = require('vm')

describe('FunctionEngine', () => {

const options = {
filename: 'CloudFunction.test_name',
timeout: 1000,
microtaskMode: 'afterEvaluate',
contextCodeGeneration: {
strings: false
}
}

it('constructor()', async () => {
const engine = new FunctionEngine(require)

assert.ok(engine)
})

it('run() should be ok', async () => {
const engine = new FunctionEngine(require)

const code = `
exports.main = async function(ctx) {
console.log('hi test log')
return 123
}
`

const res = await engine.run(code, {}, options)

assert.strictEqual(res.data, 123)
assert.ok(res.logs[0].indexOf('hi test log') > 0)
assert.ok(res.time_usage > 0)
})

it('run() with synchronous main() should be ok', async () => {
const engine = new FunctionEngine(require)

const code = `
exports.main = function(ctx) {
return 123
}
`

const res = await engine.run(code, {}, options)

assert.strictEqual(res.data, 123)
assert.ok(res.time_usage > 0)
})

it('run() with error code should be ok', async () => {
const engine = new FunctionEngine(require)

const code = `
exports.main = async function(ctx) {
error code
return 123
}
`
const res = await engine.run(code, {}, options)
console.log(res.error.message)

assert.strictEqual(res.data, undefined)
assert.ok(res.error instanceof Error)
assert.strictEqual(res.error.message, 'Unexpected identifier')
assert.ok(res.logs.length)
})

it('run() with timeout should be ok', async () => {
const engine = new FunctionEngine(require)

const code = `
exports.main = async function(ctx) {
while(true) {}
}
`
const res = await engine.run(code, {}, options)

assert.ok(res.error)
assert.strictEqual(res.error.code, 'ERR_SCRIPT_EXECUTION_TIMEOUT')
assert.ok(res.time_usage > 1000)
})

it('run() with async timeout & microtaskMode === "afterEvaluate" should be ok', async () => {
const engine = new FunctionEngine(require)

const code = `
exports.main = async function(ctx) {
Promise.resolve().then(() => { while(true) { }})
}
`
const res = await engine.run(code, {}, options)

assert.ok(res.error)
assert.strictEqual(res.error.code, 'ERR_SCRIPT_EXECUTION_TIMEOUT')
assert.ok(res.time_usage > 1000)
})

it('run() with timeout & microtaskMode === "afterEvaluate" should be ok', async () => {
const engine = new FunctionEngine(require)

const code = `
exports.main = async function(ctx) {
return 123
}
`
const res = await engine.run(code, {}, options)

assert.strictEqual(res.data, 123)
})

it('run() with contextCodeGeneration.strings === false should be ok', async () => {
const engine = new FunctionEngine(require)

const code = `
exports.main = async function(ctx) {
const r = eval('1234')
return r
}
`
const res = await engine.run(code, {}, options)

assert.strictEqual(res.error.name, 'EvalError')
assert.strictEqual(res.error.message, 'Code generation from strings disallowed for this context')
})

})

0 comments on commit 40a9ae8

Please sign in to comment.