Skip to content

Commit

Permalink
chore(codepipeline): migrate pipeline.test.ts to use jest
Browse files Browse the repository at this point in the history
Move `pipeline.test.ts` to use native jest APIs instead of
`nodeunitshim`.

Motivation
As part of the work for feature flags in v2, it's easier to inject
conditional testing using jest's native APIs than to work with
nodeunitshim.

Nevertheless, we should be using native jest APIs everywhere.
  • Loading branch information
Niranjan Jayakar committed Feb 8, 2021
1 parent 9983fb1 commit d40bc41
Showing 1 changed file with 44 additions and 45 deletions.
89 changes: 44 additions & 45 deletions packages/@aws-cdk/aws-codepipeline/test/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import * as kms from '@aws-cdk/aws-kms';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as codepipeline from '../lib';
import { FakeBuildAction } from './fake-build-action';
import { FakeSourceAction } from './fake-source-action';

/* eslint-disable quote-props */

nodeunitShim({
'Pipeline': {
'can be passed an IAM role during pipeline creation'(test: Test) {
describe('', () => {
describe('Pipeline', () => {
test('can be passed an IAM role during pipeline creation', () => {
const stack = new cdk.Stack();
const role = new iam.Role(stack, 'Role', {
assumedBy: new iam.ServicePrincipal('codepipeline.amazonaws.com'),
Expand All @@ -32,23 +31,23 @@ nodeunitShim({
},
}));

test.done();
},

'can be imported by ARN'(test: Test) {
});

test('can be imported by ARN', () => {
const stack = new cdk.Stack();

const pipeline = codepipeline.Pipeline.fromPipelineArn(stack, 'Pipeline',
'arn:aws:codepipeline:us-east-1:123456789012:MyPipeline');

test.equal(pipeline.pipelineArn, 'arn:aws:codepipeline:us-east-1:123456789012:MyPipeline');
test.equal(pipeline.pipelineName, 'MyPipeline');
expect(pipeline.pipelineArn).toEqual('arn:aws:codepipeline:us-east-1:123456789012:MyPipeline');
expect(pipeline.pipelineName).toEqual('MyPipeline');


test.done();
},
});

'that is cross-region': {
'validates that source actions are in the same region as the pipeline'(test: Test) {
describe('that is cross-region', () => {
test('validates that source actions are in the same region as the pipeline', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'PipelineStack', { env: { region: 'us-west-1', account: '123456789012' } });
const pipeline = new codepipeline.Pipeline(stack, 'Pipeline');
Expand All @@ -61,14 +60,14 @@ nodeunitShim({
region: 'ap-southeast-1',
});

test.throws(() => {
expect(() => {
sourceStage.addAction(sourceAction);
}, /Source action 'FakeSource' must be in the same region as the pipeline/);
}).toThrow(/Source action 'FakeSource' must be in the same region as the pipeline/);


test.done();
},
});

'allows passing an Alias in place of the KMS Key in the replication Bucket'(test: Test) {
test('allows passing an Alias in place of the KMS Key in the replication Bucket', () => {
const app = new cdk.App();

const replicationRegion = 'us-west-1';
Expand Down Expand Up @@ -173,10 +172,10 @@ nodeunitShim({
},
});

test.done();
},

"generates ArtifactStores with the alias' ARN as the KeyID"(test: Test) {
});

test('generates ArtifactStores with the alias ARN as the KeyID', () => {
const app = new cdk.App();
const replicationRegion = 'us-west-1';

Expand Down Expand Up @@ -239,10 +238,10 @@ nodeunitShim({
'UpdateReplacePolicy': 'Delete',
}, ResourcePart.CompleteDefinition);

test.done();
},

'allows passing an imported Bucket and Key for the replication Bucket'(test: Test) {
});

test('allows passing an imported Bucket and Key for the replication Bucket', () => {
const replicationRegion = 'us-west-1';

const pipelineRegion = 'us-west-2';
Expand Down Expand Up @@ -296,10 +295,10 @@ nodeunitShim({
],
});

test.done();
},

'generates the support stack containing the replication Bucket without the need to bootstrap in that environment'(test: Test) {
});

test('generates the support stack containing the replication Bucket without the need to bootstrap in that environment', () => {
const app = new cdk.App({
treeMetadata: false, // we can't set the context otherwise, because App will have a child
});
Expand Down Expand Up @@ -331,17 +330,17 @@ nodeunitShim({

const assembly = app.synth();
const supportStackArtifact = assembly.getStackByName('PipelineStack-support-eu-south-1');
test.equal(supportStackArtifact.assumeRoleArn,
expect(supportStackArtifact.assumeRoleArn).toEqual(
'arn:${AWS::Partition}:iam::123456789012:role/cdk-hnb659fds-deploy-role-123456789012-us-west-2');
test.equal(supportStackArtifact.cloudFormationExecutionRoleArn,
expect(supportStackArtifact.cloudFormationExecutionRoleArn).toEqual(
'arn:${AWS::Partition}:iam::123456789012:role/cdk-hnb659fds-cfn-exec-role-123456789012-us-west-2');

test.done();
},
},

'that is cross-account': {
'does not allow passing a dynamic value in the Action account property'(test: Test) {
});
});

describe('that is cross-account', () => {
test('does not allow passing a dynamic value in the Action account property', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'PipelineStack', { env: { account: '123456789012' } });
const sourceOutput = new codepipeline.Artifact();
Expand All @@ -355,18 +354,18 @@ nodeunitShim({
});
const buildStage = pipeline.addStage({ stageName: 'Build' });

test.throws(() => {
expect(() => {
buildStage.addAction(new FakeBuildAction({
actionName: 'FakeBuild',
input: sourceOutput,
account: cdk.Aws.ACCOUNT_ID,
}));
}, /The 'account' property must be a concrete value \(action: 'FakeBuild'\)/);
}).toThrow(/The 'account' property must be a concrete value \(action: 'FakeBuild'\)/);


test.done();
},
});

'does not allow an env-agnostic Pipeline Stack if an Action account has been provided'(test: Test) {
test('does not allow an env-agnostic Pipeline Stack if an Action account has been provided', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'PipelineStack');
const sourceOutput = new codepipeline.Artifact();
Expand All @@ -380,18 +379,18 @@ nodeunitShim({
});
const buildStage = pipeline.addStage({ stageName: 'Build' });

test.throws(() => {
expect(() => {
buildStage.addAction(new FakeBuildAction({
actionName: 'FakeBuild',
input: sourceOutput,
account: '123456789012',
}));
}, /Pipeline stack which uses cross-environment actions must have an explicitly set account/);
}).toThrow(/Pipeline stack which uses cross-environment actions must have an explicitly set account/);


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

describe('test with shared setup', () => {
Expand Down

0 comments on commit d40bc41

Please sign in to comment.