Skip to content

Commit

Permalink
feat(type-safe-api): generate handler tests for typescript
Browse files Browse the repository at this point in the history
Generate example jest tests for typescript handlers.

Fixes #570
  • Loading branch information
cogwirrel committed Nov 7, 2023
1 parent cdbc717 commit 3af631f
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ This will give you generated lambda handler stubs which look like the following:

You can implement your lambda handlers in any of the supported languages, or mix and match languages for different operations if you prefer.

An example unit test will also be generated for each handler. These unit tests are only generated when the corresponding handler is initially generated, so you can safely delete the generated test if you do not want it.

## Function CDK Constructs

As well as generating lambda handler stubs, when you use the `@handler` Smithy trait or `x-handler` OpenAPI vendor extension, your generated CDK infrastructure project will include lambda function CDK constructs with preconfigured paths to your handler distributables. This allows you to quickly add lambda integrations to your API:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ files:
handlers.handlebars:
destinationFilename: {{src}}/__all_handlers.ts
templateType: SupportingFiles
tests.handlebars:
destinationFilename: {{tst}}/__all_tests.ts
templateType: SupportingFiles
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{{#startsWith vendorExtensions.x-handler.language 'typescript'}}
###TSAPI_WRITE_FILE###
{
"id": "{{nickname}}",
"dir": ".",
"name": "{{nickname}}",
"ext": ".ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
###TSAPI_SPLIT_FILE###
{{#apiInfo ~}}
{{#apis ~}}
{{#operations ~}}
{{#operation ~}}
{{#if vendorExtensions.x-handler}}
{{#startsWith vendorExtensions.x-handler.language 'typescript'}}
###TSAPI_WRITE_FILE###
{
"id": "{{nickname}}Test",
"dir": ".",
"name": "{{nickname}}",
"ext": ".test.ts",
"overwrite": false,
"kebabCaseFileName": true,
"generateConditionallyId": "{{nickname}}"
}
###/TSAPI_WRITE_FILE###import {
InternalFailureErrorResponseContent,
{{operationIdCamelCase}}ChainedRequestInput,
} from "{{#apiInfo}}{{#apis.0}}{{vendorExtensions.x-runtime-package-name}}{{/apis.0}}{{/apiInfo}}";
import {
{{nickname}}
} from "../src/###TSAPI_FN###{ "function": "kebabCase", "args": ["{{nickname}}"] }###/TSAPI_FN###";

// Common request arguments
const requestArguments = {
chain: undefined as never,
event: {} as any,
context: {} as any,
interceptorContext: {
logger: {
info: jest.fn(),
},
},
} satisfies Omit<{{operationIdCamelCase}}ChainedRequestInput, 'input'>;

describe('{{operationIdCamelCase}}', () => {

it('should return not implemented error', async () => {
// TODO: Update the test as appropriate when you implement your handler
const response = await {{nickname}}({
...requestArguments,
input: {
// TODO: remove the "as any" below and fill in test values for the requestParameters{{#if bodyParam}} and body{{/if}}
requestParameters: {} as any,
body: {} as {{#if bodyParam}}any{{else}}never{{/if}},
},
});

expect(response.statusCode).toBe(500);
expect((response.body as InternalFailureErrorResponseContent).message).toEqual('Not Implemented!');
});

});

{{~/startsWith}}
{{~/if}}
{{~/operation}}
{{~/operations}}
{{~/apis}}
{{~/apiInfo}}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export class GeneratedTypescriptHandlersProject extends TypeScriptProject {
"**/*",
"*",
// This will be split into a file per targeted handler
`!${this.srcdir}/__all_handlers.ts`
`!${this.srcdir}/__all_handlers.ts`,
`!${this.testdir}/__all_tests.ts`
);

// Add OpenAPI Generator cli configuration
Expand Down Expand Up @@ -169,6 +170,7 @@ export class GeneratedTypescriptHandlersProject extends TypeScriptProject {
specPath: this.options.specPath,
generatorDirectory: OtherGenerators.TYPESCRIPT_LAMBDA_HANDLERS,
srcDir: this.srcdir,
tstDir: this.testdir,
normalizers: {
KEEP_ONLY_FIRST_TAG_IN_OPERATION: true,
},
Expand Down

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ describe("Typescript Handlers Code Generation Script Unit Tests", () => {

expect(snapshot["handlers/src/typescript-one.ts"]).toMatchSnapshot();
expect(snapshot["handlers/src/typescript-two.ts"]).toMatchSnapshot();
expect(snapshot["handlers/test/typescript-one.test.ts"]).toMatchSnapshot();
expect(snapshot["handlers/test/typescript-two.test.ts"]).toMatchSnapshot();

// Other language handlers should be skipped
expect(
Expand Down

0 comments on commit 3af631f

Please sign in to comment.