Skip to content

Commit

Permalink
chore(core): backport runtime-info test simplification from v2 branch (
Browse files Browse the repository at this point in the history
…#13665)

These changes (plus import changes) were necessary to make these tests pass on
the `v2-main` branch. Back-porting to `master` to (hopefully) reduce future
forward-merge pain.

The 'returns base construct info if no more specific info is present' test was
removed as is wasn't truly necessary, and does not work with V2, where Construct
comes from the `constructs` library instead of `core.Construct`.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
njlynch committed Mar 18, 2021
1 parent 95438b5 commit 4934937
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/test/metadata-resource.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as zlib from 'zlib';
import { App, Stack } from '../lib';
import { formatAnalytics } from '../lib/private/metadata-resource';
import { ConstructInfo } from '../lib/private/runtime-info';

// eslint-disable-next-line no-duplicate-imports, import/order
import { Construct } from '../lib';
import { ConstructInfo } from '../lib/private/runtime-info';

describe('MetadataResource', () => {
let app: App;
Expand Down
51 changes: 17 additions & 34 deletions packages/@aws-cdk/core/test/runtime-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const JSII_RUNTIME_SYMBOL = Symbol.for('jsii.rtti');
let app: App;
let stack: Stack;
let _cdkVersion: string | undefined = undefined;
const modulePrefix = cdkMajorVersion() === 1 ? '@aws-cdk/core' : 'aws-cdk-lib';

// The runtime metadata this test relies on is only available if the most
// recent compile has happened using 'jsii', as the jsii compiler injects
Expand Down Expand Up @@ -40,24 +39,15 @@ describeTscSafe('constructInfoFromConstruct', () => {
test('returns fqn and version for core constructs', () => {
const constructInfo = constructInfoFromConstruct(stack);
expect(constructInfo).toBeDefined();
expect(constructInfo?.fqn).toEqual(`${modulePrefix}.Stack`);
expect(constructInfo?.fqn).toEqual('@aws-cdk/core.Stack');
expect(constructInfo?.version).toEqual(localCdkVersion());
});

test('returns base construct info if no more specific info is present', () => {
const simpleConstruct = new class extends Construct { }(stack, 'Simple');
const constructInfo = constructInfoFromConstruct(simpleConstruct);
expect(constructInfo?.fqn).toEqual(`${modulePrefix}.Construct`);
});

test('returns more specific subclass info if present', () => {
const construct = new class extends Construct {
// @ts-ignore
private static readonly [JSII_RUNTIME_SYMBOL] = { fqn: 'aws-cdk-lib.TestConstruct', version: localCdkVersion() }
}(stack, 'TestConstruct');
test('returns jsii runtime info if present', () => {
const construct = new TestConstruct(stack, 'TestConstruct');

const constructInfo = constructInfoFromConstruct(construct);
expect(constructInfo?.fqn).toEqual('aws-cdk-lib.TestConstruct');
expect(constructInfo?.fqn).toEqual('@aws-cdk/test.TestConstruct');
});

test('throws if the jsii runtime info is not as expected', () => {
Expand Down Expand Up @@ -89,56 +79,49 @@ describeTscSafe('constructInfoForStack', () => {

const stackInfo = constructInfos.find(i => /Stack/.test(i.fqn));
const jsiiInfo = constructInfos.find(i => i.fqn === 'jsii-runtime.Runtime');
expect(stackInfo?.fqn).toEqual(`${modulePrefix}.Stack`);
expect(stackInfo?.fqn).toEqual('@aws-cdk/core.Stack');
expect(stackInfo?.version).toEqual(localCdkVersion());
expect(jsiiInfo?.version).toMatch(/node.js/);
});

test('returns info for constructs added to the stack', () => {
new class extends Construct { }(stack, 'Simple');
new TestConstruct(stack, 'TestConstruct');

const constructInfos = constructInfoFromStack(stack);

expect(constructInfos.length).toEqual(3);
expect(constructInfos.map(info => info.fqn)).toContain(`${modulePrefix}.Construct`);
expect(constructInfos.map(info => info.fqn)).toContain('@aws-cdk/test.TestConstruct');
});

test('returns unique info (no duplicates)', () => {
new class extends Construct { }(stack, 'Simple1');
new class extends Construct { }(stack, 'Simple2');
new TestConstruct(stack, 'TestConstruct1');
new TestConstruct(stack, 'TestConstruct2');

const constructInfos = constructInfoFromStack(stack);

expect(constructInfos.length).toEqual(3);
expect(constructInfos.map(info => info.fqn)).toContain(`${modulePrefix}.Construct`);
expect(constructInfos.map(info => info.fqn)).toContain('@aws-cdk/test.TestConstruct');
});

test('returns info from nested constructs', () => {
new class extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
return new class extends Construct {
// @ts-ignore
private static readonly [JSII_RUNTIME_SYMBOL] = { fqn: '@aws-cdk/test.TestV1Construct', version: localCdkVersion() }
}(this, 'TestConstruct');
new TestConstruct(this, 'TestConstruct');
}
}(stack, 'Nested');

const constructInfos = constructInfoFromStack(stack);

expect(constructInfos.length).toEqual(4);
expect(constructInfos.map(info => info.fqn)).toContain('@aws-cdk/test.TestV1Construct');
expect(constructInfos.map(info => info.fqn)).toContain('@aws-cdk/test.TestConstruct');
});

test('does not return info from nested stacks', () => {
new class extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);

new class extends Construct {
// @ts-ignore
private static readonly [JSII_RUNTIME_SYMBOL] = { fqn: '@aws-cdk/test.TestV1Construct', version: localCdkVersion() }
}(this, 'TestConstruct');
new TestConstruct(this, 'TestConstruct');

new class extends Stack {
// @ts-ignore
Expand All @@ -160,16 +143,16 @@ describeTscSafe('constructInfoForStack', () => {
const constructInfos = constructInfoFromStack(stack);

const fqns = constructInfos.map(info => info.fqn);
expect(fqns).toContain('@aws-cdk/test.TestV1Construct');
expect(fqns).toContain('@aws-cdk/test.TestConstruct');
expect(fqns).not.toContain('@aws-cdk/test.TestStackInsideStack');
expect(fqns).not.toContain('@aws-cdk/test.TestNestedStackInsideStack');
expect(fqns).not.toContain('@aws-cdk/test.TestStageInsideStack');
});
});

function cdkMajorVersion(): number {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require('../../../../release.json').majorVersion;
class TestConstruct extends Construct {
// @ts-ignore
private static readonly [JSII_RUNTIME_SYMBOL] = { fqn: '@aws-cdk/test.TestConstruct', version: localCdkVersion() }
}

/**
Expand Down

0 comments on commit 4934937

Please sign in to comment.