Skip to content

Commit

Permalink
added Jest Tests to lib Template (#3948)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhboyd authored and mergify[bot] committed Sep 9, 2019
1 parent 77105ab commit 9db7fff
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* `npm run build` compile typescript to js
* `npm run watch` watch for changes and compile
* `npm run test` perform the jest unit tests
* `cdk deploy` deploy this stack to your default AWS account/region
* `cdk diff` compare deployed stack with current state
* `cdk synth` emits the synthesized CloudFormation template
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
"roots": [
"<rootDir>/test"
],
testMatch: [ '**/*.test.ts'],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"test": "jest",
"cdk": "cdk"
},
"devDependencies": {
"@aws-cdk/assert": "^%cdk-version%",
"@types/jest": "^24.0.18",
"jest": "^24.9.0",
"ts-jest": "^24.0.2",
"aws-cdk": "^%cdk-version%",
"ts-node": "^8.1.0",
"typescript": "~3.6.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect as expectCDK, matchTemplate, MatchStyle } from '@aws-cdk/assert';
import cdk = require('@aws-cdk/core');
import %name.PascalCased% = require('../lib/%name%-stack');

test('Empty Stack', () => {
const app = new cdk.App();
// WHEN
const stack = new %name.PascalCased%.%name.PascalCased%Stack(app, 'MyTestStack');
// THEN
expectCDK(stack).to(matchTemplate({
"Resources": {}
}, MatchStyle.EXACT))
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Useful commands

* `npm run build` compile typescript to js
* `npm run test` perform the jest unit tests
* `npm run watch` watch for changes and compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
"roots": [
"<rootDir>/test"
],
testMatch: [ '**/*.test.ts'],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
"types": "lib/index.d.ts",
"scripts": {
"build": "tsc",
"watch": "tsc -w"
"watch": "tsc -w",
"test": "jest"
},
"devDependencies": {
"@aws-cdk/assert": "^%cdk-version%",
"@types/jest": "^24.0.18",
"jest": "^24.9.0",
"ts-jest": "^24.0.2",
"typescript": "~3.6.2"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect as expectCDK, haveResource, SynthUtils } from '@aws-cdk/assert';
import cdk = require('@aws-cdk/core');
import %name.PascalCased% = require('../lib/index');

test('SQS Queue Created', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, "TestStack");
// WHEN
new %name.PascalCased%.%name.PascalCased%(stack, 'MyTestConstruct');
// THEN
expectCDK(stack).to(haveResource("AWS::SQS::Queue"));
});

test('SNS Topic Created', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, "TestStack");
// WHEN
new %name.PascalCased%.%name.PascalCased%(stack, 'MyTestConstruct');
// THEN
expectCDK(stack).to(haveResource("AWS::SNS::Topic"));
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* `npm run build` compile typescript to js
* `npm run watch` watch for changes and compile
* `npm run test` perform the jest unit tests
* `cdk deploy` deploy this stack to your default AWS account/region
* `cdk diff` compare deployed stack with current state
* `cdk synth` emits the synthesized CloudFormation template
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
"roots": [
"<rootDir>/test"
],
testMatch: [ '**/*.test.ts'],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"test": "jest",
"cdk": "cdk"
},
"devDependencies": {
"aws-cdk": "^%cdk-version%",
"@aws-cdk/assert": "^1.6.1",
"@types/jest": "^24.0.18",
"jest": "^24.9.0",
"ts-jest": "^24.0.2",
"ts-node": "^8.1.0",
"typescript": "~3.6.2"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect as expectCDK, haveResource } from '@aws-cdk/assert';
import cdk = require('@aws-cdk/core');
import %name.PascalCased% = require('../lib/%name%-stack');

test('SQS Queue Created', () => {
const app = new cdk.App();
// WHEN
const stack = new %name.PascalCased%.%name.PascalCased%Stack(app, 'MyTestStack');
// THEN
expectCDK(stack).to(haveResource("AWS::SQS::Queue",{
VisibilityTimeout: 300
}));
});

test('SNS Topic Created', () => {
const app = new cdk.App();
// WHEN
const stack = new %name.PascalCased%.%name.PascalCased%Stack(app, 'MyTestStack');
// THEN
expectCDK(stack).to(haveResource("AWS::SNS::Topic"));
});
1 change: 1 addition & 0 deletions packages/aws-cdk/test/integ/init/test-typescript.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ for template in app sample-app lib; do
cdk init -l typescript -t $template
npm ls # this will fail if we have unmet peer dependencies
npm run build
npm run test
cdk synth
done

0 comments on commit 9db7fff

Please sign in to comment.