Skip to content

Commit

Permalink
feat: add snapshot capability for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martin.mueller committed Jan 3, 2021
1 parent 1de36c8 commit e3dd4f7
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 12 deletions.
7 changes: 4 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ describe('Get', () => {
// account: 'us-east-1', //process.env.CDK_DEFAULT_ACCOUNT,
// };
const app = new core.App();
const stack = new core.Stack(app, 'testing-stack');
new PipelineStack(stack, 'PipelineStack', {
const synthesizer = new core.DefaultStackSynthesizer({ qualifier: 'pipeline-stack' });
const stack = new PipelineStack(app, 'PipelineStack', {
synthesizer: synthesizer,
stageAccounts: [{
account: {
id: '1233334',
Expand All @@ -30,7 +31,7 @@ describe('Get', () => {

describe('successful', () => {
test('which exist', () => {
// expect(stack).toHaveResourceLike('Custom::AWS');
expect(stack).toHaveResourceLike('Custom::AutoDeleteBucket');
});
});
});
Expand Down
11 changes: 6 additions & 5 deletions src/integ.default.ts → test/integ.default.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import * as core from '@aws-cdk/core';
import { CustomStack } from './custom-stack';
import { PipelineStack } from './index';
import { CustomStack } from '../src/custom-stack';
import { PipelineStack } from '../src/index';
import { PIPELINE_ENV, TestApp } from './testutil';

export class IntegTesting {
readonly stack: core.Stack[];
constructor() {
const app = new core.App();
const app = new TestApp();

// const env = {
// region: process.env.CDK_DEFAULT_REGION,
// account: process.env.CDK_DEFAULT_ACCOUNT,
// };

const synthesizer = new core.DefaultStackSynthesizer({ qualifier: 'pipeline-stack', deployRoleArn: '*' });
// const synthesizer = new core.DefaultStackSynthesizer({ qualifier: 'pipeline-stack' });

// Create a loose coupled SSM Parameter from type String
const stack = new PipelineStack(app, 'PipelineStack', {
synthesizer: synthesizer,
env: PIPELINE_ENV,
stageAccounts: [{
account: {
id: '981237193288',
Expand Down
8 changes: 4 additions & 4 deletions test/integ.snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import '@aws-cdk/assert/jest';
// import { SynthUtils } from '@aws-cdk/assert';
import { IntegTesting } from '../src/integ.default';
import { SynthUtils } from '@aws-cdk/assert';
import { IntegTesting } from './integ.default';

test('integ snapshot validation', () => {
const integ = new IntegTesting();
integ.stack.forEach(() => {
integ.stack.forEach((stack) => {
// ATM probably not possible to create a snapshot as it uses a newer bootstrap. I get that snapshot manually from the AWS Console
// expect(SynthUtils.toCloudFormation(stack)).toMatchSnapshot();
expect(SynthUtils.toCloudFormation(stack)).toMatchSnapshot();
});
});
117 changes: 117 additions & 0 deletions test/testutil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// copied from https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/pipelines/test/testutil.ts

// import * as fs from 'fs';
// import * as path from 'path';
// import * as codepipeline from '@aws-cdk/aws-codepipeline';
// import * as codepipeline_actions from '@aws-cdk/aws-codepipeline-actions';
// import * as ec2 from '@aws-cdk/aws-ec2';
// import * as s3 from '@aws-cdk/aws-s3';
import { App, AppProps, Environment } from '@aws-cdk/core';
// import { Construct } from 'constructs';
// import * as cdkp from '../lib';
// import { assemblyBuilderOf } from '../lib/private/construct-internals';

export const PIPELINE_ENV: Environment = {
account: '123pipeline',
region: 'us-pipeline',
};

export class TestApp extends App {
constructor(props?: Partial<AppProps>) {
super({
context: {
'@aws-cdk/core:newStyleStackSynthesis': '1',
},
stackTraces: false,
autoSynth: false,
treeMetadata: false,
...props,
});
}

// public cleanup() {
// rimraf(assemblyBuilderOf(this).outdir);
// }
}

// export class TestGitHubNpmPipeline extends cdkp.CdkPipeline {
// public readonly sourceArtifact: codepipeline.Artifact;
// public readonly cloudAssemblyArtifact: codepipeline.Artifact;

// constructor(scope: Construct, id: string, props?: Partial<cdkp.CdkPipelineProps> & { readonly sourceArtifact?: codepipeline.Artifact }) {
// const sourceArtifact = props?.sourceArtifact ?? new codepipeline.Artifact();
// const cloudAssemblyArtifact = props?.cloudAssemblyArtifact ?? new codepipeline.Artifact();

// super(scope, id, {
// sourceAction: new TestGitHubAction(sourceArtifact),
// synthAction: cdkp.SimpleSynthAction.standardNpmSynth({
// sourceArtifact,
// cloudAssemblyArtifact,
// }),
// vpc: new ec2.Vpc(scope, 'TestVpc'),
// cloudAssemblyArtifact,
// ...props,
// });

// this.sourceArtifact = sourceArtifact;
// this.cloudAssemblyArtifact = cloudAssemblyArtifact;
// }
// }


// export class TestGitHubAction extends codepipeline_actions.GitHubSourceAction {
// constructor(sourceArtifact: codepipeline.Artifact) {
// super({
// actionName: 'GitHub',
// output: sourceArtifact,
// oauthToken: SecretValue.plainText('$3kr1t'),
// owner: 'test',
// repo: 'test',
// trigger: codepipeline_actions.GitHubTrigger.POLL,
// });
// }
// }

// /**
// * A test stack
// *
// * It contains a single Bucket. Such robust. Much uptime.
// */
// export class BucketStack extends Stack {
// public readonly bucket: s3.IBucket;

// constructor(scope: Construct, id: string, props?: StackProps) {
// super(scope, id, props);
// this.bucket = new s3.Bucket(this, 'Bucket');
// }
// }

// /**
// * rm -rf reimplementation, don't want to depend on an NPM package for this
// */
// export function rimraf(fsPath: string) {
// try {
// const isDir = fs.lstatSync(fsPath).isDirectory();

// if (isDir) {
// for (const file of fs.readdirSync(fsPath)) {
// rimraf(path.join(fsPath, file));
// }
// fs.rmdirSync(fsPath);
// } else {
// fs.unlinkSync(fsPath);
// }
// } catch (e) {
// // We will survive ENOENT
// if (e.code !== 'ENOENT') { throw e; }
// }
// }

// /**
// * Because 'expect(stack)' doesn't work correctly for stacks in nested assemblies
// */
// export function stackTemplate(stack: Stack) {
// const stage = Stage.of(stack);
// if (!stage) { throw new Error('stack not in a Stage'); }
// return stage.synth().getStackArtifact(stack.artifactId);
// }

0 comments on commit e3dd4f7

Please sign in to comment.