Skip to content

Commit

Permalink
fix: runtime mock support initHandler (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar authored Dec 11, 2020
1 parent 18c32b9 commit 0b993be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages-serverless/runtime-mock/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface MockRuntimeOptions {
bootstrap?;
events?: FunctionEvent[];
handler?: any;
initHandler?: any;
initContext?: any;
runtime?: Runtime;
}

Expand Down Expand Up @@ -60,8 +62,11 @@ export class MockRuntime {
await this.bootstrap.start();
await sleep(500);
this.runtime = this.bootstrap.getRuntime();
return this.runtime;
}
if (this.options.initHandler) {
await this.options.initHandler(this.options.initContext);
}
return this.runtime;
}

async invoke(...args) {
Expand Down
6 changes: 6 additions & 0 deletions packages-serverless/runtime-mock/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ const assert = require('assert');

describe('/test/index.test.ts', () => {
it('should create runtime', async () => {
let initHanlderResult;
const runtime = createRuntime({
functionDir: join(__dirname, './code'),
events: [new HTTPEvent()],
initHandler: arg => {
initHanlderResult = arg;
},
initContext: 'test'
});
await runtime.start();
const result = await runtime.invoke({
Expand All @@ -18,5 +23,6 @@ describe('/test/index.test.ts', () => {
});
assert.equal(result, 'hello Alan');
await runtime.close();
assert.equal(initHanlderResult, 'test');
});
});

0 comments on commit 0b993be

Please sign in to comment.