Skip to content

Commit

Permalink
chore: move 'core' tests to jest via nodeunit-shim (#10661)
Browse files Browse the repository at this point in the history
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rix0rrr authored Oct 5, 2020
1 parent 9e11e5a commit 63088e1
Show file tree
Hide file tree
Showing 54 changed files with 238 additions and 208 deletions.
3 changes: 2 additions & 1 deletion packages/@aws-cdk/core/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ nyc.config.js
*.snk
!.eslintrc.js

junit.xml
junit.xml
!jest.config.js
3 changes: 2 additions & 1 deletion packages/@aws-cdk/core/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ tsconfig.json

# exclude cdk artifacts
**/cdk.out
junit.xml
junit.xml
jest.config.js
10 changes: 10 additions & 0 deletions packages/@aws-cdk/core/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const baseConfig = require('cdk-build-tools/config/jest.config');
module.exports = {
...baseConfig,
coverageThreshold: {
global: {
branches: 60,
statements: 75,
}
}
};
24 changes: 17 additions & 7 deletions packages/@aws-cdk/core/lib/tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,22 @@ class NoFormat implements ITagFormatter {
}
}

const TAG_FORMATTERS: {[key: string]: ITagFormatter} = {
[TagType.AUTOSCALING_GROUP]: new AsgFormatter(),
[TagType.STANDARD]: new StandardFormatter(),
[TagType.MAP]: new MapFormatter(),
[TagType.KEY_VALUE]: new KeyValueFormatter(),
[TagType.NOT_TAGGABLE]: new NoFormat(),

let _tagFormattersCache: {[key: string]: ITagFormatter} | undefined;

/**
* Access tag formatters table
*
* In a function because we're in a load cycle with cfn-resource that defines `TagType`.
*/
function TAG_FORMATTERS(): {[key: string]: ITagFormatter} {
return _tagFormattersCache ?? (_tagFormattersCache = {
[TagType.AUTOSCALING_GROUP]: new AsgFormatter(),
[TagType.STANDARD]: new StandardFormatter(),
[TagType.MAP]: new MapFormatter(),
[TagType.KEY_VALUE]: new KeyValueFormatter(),
[TagType.NOT_TAGGABLE]: new NoFormat(),
});
};

/**
Expand Down Expand Up @@ -245,7 +255,7 @@ export class TagManager {

constructor(tagType: TagType, resourceTypeName: string, tagStructure?: any, options: TagManagerOptions = { }) {
this.resourceTypeName = resourceTypeName;
this.tagFormatter = TAG_FORMATTERS[tagType];
this.tagFormatter = TAG_FORMATTERS()[tagType];
if (tagStructure !== undefined) {
this._setTag(...this.tagFormatter.parseTags(tagStructure, this.initialTagPriority));
}
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"cdk-build": {
"cloudformation": "AWS::CloudFormation",
"cfn2ts-core-import": ".",
"jest": true,
"pre": [
"rm -rf test/fs/fixtures && cd test/fs && tar -xzf fixtures.tar.gz"
],
Expand Down Expand Up @@ -167,13 +168,12 @@
"@types/lodash": "^4.14.161",
"@types/minimatch": "^3.0.3",
"@types/node": "^10.17.35",
"@types/nodeunit": "^0.0.31",
"@types/sinon": "^9.0.7",
"cdk-build-tools": "0.0.0",
"cfn2ts": "0.0.0",
"fast-check": "^2.4.0",
"lodash": "^4.17.20",
"nodeunit": "^0.11.3",
"nodeunit-shim": "0.0.0",
"pkglint": "0.0.0",
"sinon": "^9.1.0",
"ts-mock-imports": "^1.3.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { CloudAssembly } from '@aws-cdk/cx-api';
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { Construct, App, Stack } from '../lib';
import { Annotations } from '../lib/annotations';

const restore = process.env.CDK_BLOCK_DEPRECATIONS;

export = {
nodeunitShim({
'tearDown'(cb: any) {
process.env.CDK_BLOCK_DEPRECATIONS = restore; // restore to the original value
cb();
Expand Down Expand Up @@ -78,7 +78,7 @@ export = {
test.throws(() => Annotations.of(c1).addDeprecation('foo', 'bar'), /MyStack\/Hello: The API foo is deprecated: bar\. This API will be removed in the next major release/);
test.done();
},
};
});

function getWarnings(casm: CloudAssembly) {
const result = new Array<{ path: string, message: string }>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ContextProvider } from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { CfnResource, Construct, Stack, StackProps } from '../lib';
import { Annotations } from '../lib/annotations';
import { App, AppProps } from '../lib/app';
Expand Down Expand Up @@ -47,7 +47,7 @@ function synthStack(name: string, includeMetadata: boolean = false, context?: an
return stack;
}

export = {
nodeunitShim({
'synthesizes all stacks and returns synthesis result'(test: Test) {
const response = synth();
delete (response as any).dir;
Expand All @@ -57,7 +57,7 @@ export = {
const stack1 = response.stacks[0];
test.deepEqual(stack1.stackName, 'stack1');
test.deepEqual(stack1.id, 'stack1');
test.deepEqual(stack1.environment.account, 12345);
test.deepEqual(stack1.environment.account, '12345');
test.deepEqual(stack1.environment.region, 'us-east-1');
test.deepEqual(stack1.environment.name, 'aws://12345/us-east-1');
test.deepEqual(stack1.template, {
Expand Down Expand Up @@ -408,7 +408,7 @@ export = {

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

class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
Expand Down Expand Up @@ -444,4 +444,4 @@ function withCliVersion<A>(block: () => A): A {
} finally {
delete process.env[cxapi.CLI_VERSION_ENV];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { ArnComponents, Aws, CfnOutput, ScopedAws, Stack } from '../lib';
import { Intrinsic } from '../lib/private/intrinsic';
import { toCloudFormation } from './util';

export = {
nodeunitShim({
'create from components with defaults'(test: Test) {
const stack = new Stack();

Expand Down Expand Up @@ -277,4 +277,4 @@ export = {

test.done();
},
};
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { App } from '../lib';
import { IAspect, Aspects } from '../lib/aspect';
import { Construct, IConstruct } from '../lib/construct-compat';
Expand All @@ -25,7 +25,7 @@ class MyAspect implements IAspect {
}
}

export = {
nodeunitShim({
'Aspects are invoked only once'(test: Test) {
const app = new App();
const root = new MyConstruct(app, 'MyConstruct');
Expand Down Expand Up @@ -74,4 +74,4 @@ export = {
test.done();
},

};
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { FileAssetPackaging, Stack } from '../lib';
import { toCloudFormation } from './util';

export = {
nodeunitShim({
'addFileAsset correctly sets metadata and creates S3 parameters'(test: Test) {
// GIVEN
const stack = new Stack();
Expand Down Expand Up @@ -156,4 +156,4 @@ export = {
test.deepEqual(toCloudFormation(stack), { });
test.done();
},
};
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as child_process from 'child_process';
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as sinon from 'sinon';
import { BundlingDockerImage, FileSystem } from '../lib';

export = {
nodeunitShim({
'tearDown'(callback: any) {
sinon.restore();
callback();
Expand Down Expand Up @@ -152,4 +152,4 @@ export = {
test.ok(fingerprintStub.calledWith('docker-path', sinon.match({ extraHash: JSON.stringify({}) })));
test.done();
},
};
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { App, CfnResource, Lazy, Stack } from '../lib';
import { CfnJson } from '../lib/cfn-json';
import { CfnUtilsResourceType } from '../lib/private/cfn-utils-provider/consts';
import { handler } from '../lib/private/cfn-utils-provider/index';

export = {
nodeunitShim({

'resolves to a fn::getatt'(test: Test) {
// GIVEN
Expand Down Expand Up @@ -89,4 +89,4 @@ export = {
test.deepEqual(input, response.Data.Value);
test.done();
},
};
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { CfnParameter, Stack } from '../lib';

export = {
nodeunitShim({
'valueAsString supports both string and number types'(test: Test) {
// GIVEN
const stack = new Stack();
Expand Down Expand Up @@ -29,4 +29,4 @@ export = {

test.done();
},
}
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as nodeunit from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as core from '../lib';

export = nodeunit.testCase({
nodeunitShim({
'._toCloudFormation': {
'does not call renderProperties with an undefined value'(test: nodeunit.Test) {
'does not call renderProperties with an undefined value'(test: Test) {
const app = new core.App();
const stack = new core.Stack(app, 'TestStack');
const resource = new core.CfnResource(stack, 'DefaultResource', { type: 'Test::Resource::Fake' });
Expand All @@ -26,7 +26,7 @@ export = nodeunit.testCase({
test.done();
},

'renders "Properties" for a resource that has only properties set to "false"'(test: nodeunit.Test) {
'renders "Properties" for a resource that has only properties set to "false"'(test: Test) {
const app = new core.App();
const stack = new core.Stack(app, 'TestStack');
new core.CfnResource(stack, 'Resource', {
Expand All @@ -51,7 +51,7 @@ export = nodeunit.testCase({
},
},

'applyRemovalPolicy default includes Update policy'(test: nodeunit.Test) {
'applyRemovalPolicy default includes Update policy'(test: Test) {
// GIVEN
const app = new core.App();
const stack = new core.Stack(app, 'TestStack');
Expand All @@ -74,7 +74,7 @@ export = nodeunit.testCase({
test.done();
},

'can switch off updating Update policy'(test: nodeunit.Test) {
'can switch off updating Update policy'(test: Test) {
// GIVEN
const app = new core.App();
const stack = new core.Stack(app, 'TestStack');
Expand All @@ -98,7 +98,7 @@ export = nodeunit.testCase({
test.done();
},

'can add metadata'(test: nodeunit.Test) {
'can add metadata'(test: Test) {
// GIVEN
const app = new core.App();
const stack = new core.Stack(app, 'TestStack');
Expand All @@ -122,7 +122,7 @@ export = nodeunit.testCase({
test.done();
},

'subclasses can override "shouldSynthesize" to lazy-determine if the resource should be included'(test: nodeunit.Test) {
'subclasses can override "shouldSynthesize" to lazy-determine if the resource should be included'(test: Test) {
// GIVEN
class HiddenCfnResource extends core.CfnResource {
protected shouldSynthesize() {
Expand Down Expand Up @@ -151,7 +151,7 @@ export = nodeunit.testCase({
test.done();
},

'CfnResource cannot be created outside Stack'(test: nodeunit.Test) {
'CfnResource cannot be created outside Stack'(test: Test) {
const app = new core.App();
test.throws(() => {
new core.CfnResource(app, 'Resource', {
Expand All @@ -166,7 +166,7 @@ export = nodeunit.testCase({
/**
* Stages start a new scope, which does not count as a Stack anymore
*/
'CfnResource cannot be in Stage in Stack'(test: nodeunit.Test) {
'CfnResource cannot be in Stage in Stack'(test: Test) {
const app = new core.App();
const stack = new core.Stack(app, 'Stack');
const stage = new core.Stage(stack, 'Stage');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { App, CfnOutput, Fn, Lazy, Stack, Token } from '../lib';
import { Intrinsic } from '../lib/private/intrinsic';
import { evaluateCFN } from './evaluate-cfn';

export = {
nodeunitShim({
'string tokens can be JSONified and JSONification can be reversed'(test: Test) {
const stack = new Stack();

Expand Down Expand Up @@ -235,7 +235,7 @@ export = {

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

/**
* Return two Tokens, one of which evaluates to a Token directly, one which evaluates to it lazily
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as cdk from '../lib';
import { toCloudFormation } from './util';

export = {
nodeunitShim({
'chain conditions'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -63,4 +63,4 @@ export = {
});
test.done();
},
};
});
Loading

0 comments on commit 63088e1

Please sign in to comment.