Skip to content

Commit a0060e3

Browse files
committed
chore(tests): Finally add a unit test for RestAPI
1 parent b968095 commit a0060e3

File tree

4 files changed

+165
-3
lines changed

4 files changed

+165
-3
lines changed

src/constructs/load-yaml.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import fs from 'fs';
2+
import * as yaml from 'js-yaml';
3+
import { OpenAPI3 } from 'openapi-typescript';
4+
5+
export const loadYaml = (filename: string) => yaml.load(fs.readFileSync(filename).toString()) as OpenAPI3;

src/constructs/rest-api.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as fs from 'fs';
21
import {
32
aws_certificatemanager,
43
aws_iam,
@@ -8,11 +7,11 @@ import {
87
} from 'aws-cdk-lib';
98
import * as cdk from 'aws-cdk-lib';
109
import { Construct } from 'constructs';
11-
import * as yaml from 'js-yaml';
1210
import { OpenAPI3, OperationObject, PathItemObject } from 'openapi-typescript';
1311
import { ICognitoAuthentication, IJwtAuthentication } from './authentication';
1412
import { BaseApi, BaseApiProps } from './base-api';
1513
import { LambdaFunction, LambdaOptions } from './func';
14+
import { loadYaml } from './load-yaml';
1615

1716
export interface RestApiProps<OPS> extends BaseApiProps {
1817

@@ -52,7 +51,7 @@ export class RestApi<PATHS, OPS> extends BaseApi {
5251
constructor(scope: Construct, id: string, private props: RestApiProps<OPS>) {
5352
super(scope, id, props);
5453

55-
this.apiSpec = yaml.load(fs.readFileSync(props.definitionFileName).toString()) as OpenAPI3;
54+
this.apiSpec = loadYaml(props.definitionFileName);
5655

5756
let customDomainName: aws_apigateway.DomainNameOptions | undefined;
5857
if (this.apiFQDN) {
@@ -356,3 +355,4 @@ export class RestApi<PATHS, OPS> extends BaseApi {
356355
}
357356

358357
}
358+

test/constructs/__snapshots__/rest-api.test.ts.snap

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/constructs/rest-api.test.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { App, Stack } from 'aws-cdk-lib';
2+
import { Template } from 'aws-cdk-lib/assertions';
3+
import { Metric, MetricOptions } from 'aws-cdk-lib/aws-cloudwatch';
4+
import { Permission } from 'aws-cdk-lib/aws-lambda';
5+
import { Construct } from 'constructs';
6+
import { OpenAPI3 } from 'openapi-typescript';
7+
import { LambdaFunction, LambdaFunctionProps, RestApi } from '../../src/constructs';
8+
import { loadYaml } from '../../src/constructs/load-yaml';
9+
10+
jest.mock('../../src/constructs/load-yaml');
11+
jest.mock('../../src/constructs/func');
12+
13+
14+
describe('A generated RestApi', () => {
15+
16+
describe('when instantiated with monitoring active', () =>{
17+
let stack: Stack;
18+
beforeEach(() => {
19+
const app = new App();
20+
stack = new Stack(app);
21+
});
22+
23+
test('should create a Cloudwatch Dashboard', () => {
24+
interface Operations {
25+
testEndpoint: {
26+
responses: {
27+
200: {};
28+
};
29+
};
30+
}
31+
32+
interface Paths {
33+
'/test': {
34+
get: Operations['testEndpoint'];
35+
};
36+
}
37+
38+
const buffer = {
39+
openapi: '3.0.1',
40+
paths: {
41+
'/test': {
42+
get: {
43+
operationId: 'testEndpoint',
44+
responses: {
45+
200: {
46+
content: {
47+
'application/json': {},
48+
},
49+
description: '',
50+
summary: '',
51+
},
52+
},
53+
},
54+
},
55+
},
56+
info: {
57+
title: 'Existing API definition',
58+
version: '1.0',
59+
},
60+
} as OpenAPI3;
61+
62+
jest.mocked(loadYaml).mockReturnValue(buffer);
63+
jest.mocked(LambdaFunction).mockImplementation((_scope: Construct, _id: string, _mockProps: LambdaFunctionProps) => {
64+
return {
65+
metricDuration(_props?: MetricOptions): Metric {
66+
return new Metric({
67+
metricName: 'Duration',
68+
namespace: 'TestNamespace',
69+
});
70+
},
71+
metricInvocations(_props?: MetricOptions): Metric {
72+
return new Metric({
73+
metricName: 'Invocations',
74+
namespace: 'TestNamespace',
75+
});
76+
},
77+
metricErrors(_props?: MetricOptions): Metric {
78+
return new Metric({
79+
metricName: 'Errors',
80+
namespace: 'TestNamespace',
81+
});
82+
},
83+
metricThrottles(_props?: MetricOptions): Metric {
84+
return new Metric({
85+
metricName: 'Invocations',
86+
namespace: 'TestNamespace',
87+
});
88+
},
89+
addPermission(__id: string, __permission: Permission) {
90+
},
91+
} as LambdaFunction;
92+
});
93+
94+
new RestApi<Paths, Operations>(stack, 'TestSubject', {
95+
monitoring: true,
96+
apiName: 'TestSubjectApi',
97+
definitionFileName: 'someFile',
98+
cors: true,
99+
stageName: 'test',
100+
});
101+
102+
const template = Template.fromStack(stack);
103+
104+
expect(template.findResources('AWS::CloudWatch::Dashboard')).toMatchSnapshot();
105+
106+
});
107+
});
108+
109+
});

0 commit comments

Comments
 (0)