Skip to content

Commit

Permalink
chore(cognito): switch to jest
Browse files Browse the repository at this point in the history
Migrate all existing unit tests from nodeunit to jest.

As part of this, centralize jest configuration in the `cdk-build-tools`
package with the ability for individual packages to override these via
the `package.json`.

Dropping the `JestCoverageTarget` pkglint rule since this change
ensures that a standard set of rules, that include coverage target,
will be applied to any package that is configured to use `jest`. 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*

<!-- 
Please read the contribution guidelines and follow the pull-request checklist:
https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md
 -->
  • Loading branch information
nija-at authored Feb 5, 2020
1 parent 38e9865 commit 2c5c88d
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 219 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ cdk.out/

# Yarn error log
yarn-error.log

# Generated jest config
jest.config.gen.json
13 changes: 1 addition & 12 deletions packages/@aws-cdk/assert/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,12 @@
"build+test": "npm run build && npm test"
},
"jest": {
"collectCoverage": true,
"coverageReporters": [
"lcov",
"html",
"text-summary"
],
"coverageThreshold": {
"global": {
"statements": 75,
"branches": 65
}
},
"preset": "ts-jest",
"testMatch": [
"**/__tests__/**/*.ts?(x)",
"**/?(*.)+(spec|test).ts?(x)"
]
}
},
"author": {
"name": "Amazon Web Services",
Expand Down
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-cognito/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"cdk-build-tools": "1.22.0",
"cfn2ts": "1.22.0",
"nodeunit": "^0.11.3",
"pkglint": "1.22.0"
"pkglint": "1.22.0",
"jest": "^24.9.0"
},
"dependencies": {
"@aws-cdk/aws-iam": "1.22.0",
Expand All @@ -80,6 +81,7 @@
"@aws-cdk/aws-lambda": "1.22.0",
"@aws-cdk/core": "1.22.0"
},
"jest": {},
"engines": {
"node": ">= 10.3.0"
},
Expand Down
24 changes: 0 additions & 24 deletions packages/@aws-cdk/aws-cognito/test/test.user-pool-client.ts

This file was deleted.

21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-cognito/test/user-pool-client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import '@aws-cdk/assert/jest';
import { Stack } from '@aws-cdk/core';
import { UserPool, UserPoolClient } from '../lib';

describe('User Pool Client', () => {
test('default setup', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'Pool', { });

// WHEN
new UserPoolClient(stack, 'Client', {
userPool: pool
});

// THEN
expect(stack).toHaveResourceLike('AWS::Cognito::UserPoolClient', {
UserPoolId: stack.resolve(pool.userPoolId)
});
});
});
Original file line number Diff line number Diff line change
@@ -1,78 +1,72 @@
import { expect, haveResourceLike } from '@aws-cdk/assert';
import '@aws-cdk/assert/jest';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import { Test } from 'nodeunit';
import * as cognito from '../lib';
import { Stack, Tag } from '@aws-cdk/core';
import { SignInType, UserPool, UserPoolAttribute } from '../lib';

export = {
'default setup'(test: Test) {
describe('User Pool', () => {
test('default setup', () => {
// GIVEN
const stack = new cdk.Stack();
const stack = new Stack();

// WHEN
new cognito.UserPool(stack, 'Pool', {
new UserPool(stack, 'Pool', {
userPoolName: 'myPool',
});

// THEN
expect(stack).to(haveResourceLike('AWS::Cognito::UserPool', {
expect(stack).toHaveResourceLike('AWS::Cognito::UserPool', {
UserPoolName: 'myPool'
}));
});
});

test.done();
},
'support tags'(test: Test) {
test('support tags', () => {
// GIVEN
const stack = new cdk.Stack();
const stack = new Stack();

// WHEN
const pool = new cognito.UserPool(stack, 'Pool', {
const pool = new UserPool(stack, 'Pool', {
userPoolName: 'myPool',
});
cdk.Tag.add(pool, "PoolTag", "PoolParty");
Tag.add(pool, "PoolTag", "PoolParty");

// THEN
expect(stack).to(haveResourceLike('AWS::Cognito::UserPool', {
expect(stack).toHaveResourceLike('AWS::Cognito::UserPool', {
UserPoolName: 'myPool',
UserPoolTags: {
PoolTag: "PoolParty",
}
}));

test.done();
},
});
});

'lambda triggers are defined'(test: Test) {
test('lambda triggers are defined', () => {
// GIVEN
const stack = new cdk.Stack();
const stack = new Stack();
const fn = new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_10_X,
});

// WHEN
const pool = new cognito.UserPool(stack, 'Pool', {
const pool = new UserPool(stack, 'Pool', {
lambdaTriggers: {
preSignUp: fn
}
});
pool.addCustomMessageTrigger(fn);

// THEN
expect(stack).to(haveResourceLike('AWS::Cognito::UserPool', {
expect(stack).toHaveResourceLike('AWS::Cognito::UserPool', {
LambdaConfig: {
PreSignUp: stack.resolve(fn.functionArn),
CustomMessage: stack.resolve(fn.functionArn)
}
}));

test.done();
},
});
});

'on* API correctly appends triggers'(test: Test) {
test('on* API correctly appends triggers', () => {
// GIVEN
const stack = new cdk.Stack();
const stack = new Stack();

const createAuthChallengeLambdaFn = new lambda.Function(stack, 'createAuthChallengeLambda', {
code: new lambda.InlineCode('foo'),
Expand Down Expand Up @@ -135,7 +129,7 @@ export = {
});

// WHEN
const pool = new cognito.UserPool(stack, 'Pool', { });
const pool = new UserPool(stack, 'Pool', { });
pool.addCreateAuthChallengeTrigger(createAuthChallengeLambdaFn);
pool.addCustomMessageTrigger(customMessageLambdaFn);
pool.addDefineAuthChallengeTrigger(defineAuthChallengeLambdaFn);
Expand All @@ -148,7 +142,7 @@ export = {
pool.addVerifyAuthChallengeResponseTrigger(verifyAuthChallengeResponseLambdaFn);

// THEN
expect(stack).to(haveResourceLike('AWS::Cognito::UserPool', {
expect(stack).toHaveResourceLike('AWS::Cognito::UserPool', {
LambdaConfig: {
CreateAuthChallenge: stack.resolve(createAuthChallengeLambdaFn.functionArn),
CustomMessage: stack.resolve(customMessageLambdaFn.functionArn),
Expand All @@ -161,103 +155,79 @@ export = {
UserMigration: stack.resolve(userMigrationLambdaFn.functionArn),
VerifyAuthChallengeResponse: stack.resolve(verifyAuthChallengeResponseLambdaFn.functionArn)
}
}));

test.done();
},
});
});

'lambdas are given cognito service grant'(test: Test) {
test('lambdas are given cognito service grant', () => {
// GIVEN
const stack = new cdk.Stack();
const stack = new Stack();
const fn = new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_10_X,
});

// WHEN
new cognito.UserPool(stack, 'Pool', {
new UserPool(stack, 'Pool', {
lambdaTriggers: {
preSignUp: fn
}
});

// THEN
expect(stack).to(haveResourceLike('AWS::Lambda::Permission', {
expect(stack).toHaveResourceLike('AWS::Lambda::Permission', {
FunctionName: stack.resolve(fn.functionArn),
Principal: 'cognito-idp.amazonaws.com'
}));

test.done();
},
});
});

'set sign in type'(test: Test) {
test('set sign in type', () => {
// GIVEN
const stack = new cdk.Stack();
const stack = new Stack();

// WHEN
new cognito.UserPool(stack, 'Pool', {
signInType: cognito.SignInType.EMAIL,
autoVerifiedAttributes: [cognito.UserPoolAttribute.EMAIL]
new UserPool(stack, 'Pool', {
signInType: SignInType.EMAIL,
autoVerifiedAttributes: [ UserPoolAttribute.EMAIL ]
});

// THEN
expect(stack).to(haveResourceLike('AWS::Cognito::UserPool', {
UsernameAttributes: ['email'],
AutoVerifiedAttributes: ['email']
}));

test.done();
},
expect(stack).toHaveResourceLike('AWS::Cognito::UserPool', {
UsernameAttributes: [ 'email' ],
AutoVerifiedAttributes: [ 'email' ]
});
});

'usernameAliasAttributes require signInType of USERNAME'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
test('usernameAliasAttributes require signInType of USERNAME', () => {
const stack = new Stack();

// WHEN
const toThrow = () => {
new cognito.UserPool(stack, 'Pool', {
signInType: cognito.SignInType.EMAIL,
usernameAliasAttributes: [cognito.UserPoolAttribute.PREFERRED_USERNAME]
expect(() => {
new UserPool(stack, 'Pool', {
signInType: SignInType.EMAIL,
usernameAliasAttributes: [ UserPoolAttribute.PREFERRED_USERNAME ]
});
};

// THEN
test.throws(() => toThrow(), /'usernameAliasAttributes' can only be set with a signInType of 'USERNAME'/);
test.done();
},
}).toThrow(/'usernameAliasAttributes' can only be set with a signInType of 'USERNAME'/);
});

'usernameAliasAttributes must be one or more of EMAIL, PHONE_NUMBER, or PREFERRED_USERNAME'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
test('usernameAliasAttributes must be one or more of EMAIL, PHONE_NUMBER, or PREFERRED_USERNAME', () => {
const stack = new Stack();

// WHEN
const toThrow = () => {
new cognito.UserPool(stack, 'Pool', {
signInType: cognito.SignInType.USERNAME,
usernameAliasAttributes: [cognito.UserPoolAttribute.GIVEN_NAME]
expect(() => {
new UserPool(stack, 'Pool', {
signInType: SignInType.USERNAME,
usernameAliasAttributes: [ UserPoolAttribute.GIVEN_NAME ]
});
};
}).toThrow(/'usernameAliasAttributes' can only include EMAIL, PHONE_NUMBER, or PREFERRED_USERNAME/);
});

// THEN
test.throws(() => toThrow(), /'usernameAliasAttributes' can only include EMAIL, PHONE_NUMBER, or PREFERRED_USERNAME/);
test.done();
},
test('autoVerifiedAttributes must be one or more of EMAIL or PHONE_NUMBER', () => {
const stack = new Stack();

'autoVerifiedAttributes must be one or more of EMAIL or PHONE_NUMBER'(test: Test) {
// GIVEN
const stack = new cdk.Stack();

// WHEN
const toThrow = () => {
new cognito.UserPool(stack, 'Pool', {
signInType: cognito.SignInType.EMAIL,
autoVerifiedAttributes: [cognito.UserPoolAttribute.EMAIL, cognito.UserPoolAttribute.GENDER]
expect(() => {
new UserPool(stack, 'Pool', {
signInType: SignInType.EMAIL,
autoVerifiedAttributes: [ UserPoolAttribute.EMAIL, UserPoolAttribute.GENDER ]
});
};

// THEN
test.throws(() => toThrow(), /'autoVerifiedAttributes' can only include EMAIL or PHONE_NUMBER/);
test.done();
}
};
}).toThrow(/'autoVerifiedAttributes' can only include EMAIL or PHONE_NUMBER/);
});
});
19 changes: 1 addition & 18 deletions packages/@aws-cdk/aws-sam/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,9 @@
"@aws-cdk/core": "1.22.0"
},
"jest": {
"collectCoverage": true,
"coverageReporters": [
"lcov",
"html",
"text-summary"
],
"coverageThreshold": {
"global": {
"statements": 0,
"branches": 0
}
},
"preset": "ts-jest",
"testMatch": [
"**/__tests__/**/*.ts?(x)",
"**/?(*.)+(spec|test).ts?(x)"
]
},
"engines": {
"node": ">= 10.3.0"
},
"stability": "experimental"
}
}
Loading

0 comments on commit 2c5c88d

Please sign in to comment.