Skip to content

Commit 4934937

Browse files
authored
chore(core): backport runtime-info test simplification from v2 branch (#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*
1 parent 95438b5 commit 4934937

File tree

2 files changed

+18
-35
lines changed

2 files changed

+18
-35
lines changed

packages/@aws-cdk/core/test/metadata-resource.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as zlib from 'zlib';
22
import { App, Stack } from '../lib';
33
import { formatAnalytics } from '../lib/private/metadata-resource';
4+
import { ConstructInfo } from '../lib/private/runtime-info';
45

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

99
describe('MetadataResource', () => {
1010
let app: App;

packages/@aws-cdk/core/test/runtime-info.test.ts

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const JSII_RUNTIME_SYMBOL = Symbol.for('jsii.rtti');
1010
let app: App;
1111
let stack: Stack;
1212
let _cdkVersion: string | undefined = undefined;
13-
const modulePrefix = cdkMajorVersion() === 1 ? '@aws-cdk/core' : 'aws-cdk-lib';
1413

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

47-
test('returns base construct info if no more specific info is present', () => {
48-
const simpleConstruct = new class extends Construct { }(stack, 'Simple');
49-
const constructInfo = constructInfoFromConstruct(simpleConstruct);
50-
expect(constructInfo?.fqn).toEqual(`${modulePrefix}.Construct`);
51-
});
52-
53-
test('returns more specific subclass info if present', () => {
54-
const construct = new class extends Construct {
55-
// @ts-ignore
56-
private static readonly [JSII_RUNTIME_SYMBOL] = { fqn: 'aws-cdk-lib.TestConstruct', version: localCdkVersion() }
57-
}(stack, 'TestConstruct');
46+
test('returns jsii runtime info if present', () => {
47+
const construct = new TestConstruct(stack, 'TestConstruct');
5848

5949
const constructInfo = constructInfoFromConstruct(construct);
60-
expect(constructInfo?.fqn).toEqual('aws-cdk-lib.TestConstruct');
50+
expect(constructInfo?.fqn).toEqual('@aws-cdk/test.TestConstruct');
6151
});
6252

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

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

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

10090
const constructInfos = constructInfoFromStack(stack);
10191

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

10696
test('returns unique info (no duplicates)', () => {
107-
new class extends Construct { }(stack, 'Simple1');
108-
new class extends Construct { }(stack, 'Simple2');
97+
new TestConstruct(stack, 'TestConstruct1');
98+
new TestConstruct(stack, 'TestConstruct2');
10999

110100
const constructInfos = constructInfoFromStack(stack);
111101

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

116106
test('returns info from nested constructs', () => {
117107
new class extends Construct {
118108
constructor(scope: Construct, id: string) {
119109
super(scope, id);
120-
return new class extends Construct {
121-
// @ts-ignore
122-
private static readonly [JSII_RUNTIME_SYMBOL] = { fqn: '@aws-cdk/test.TestV1Construct', version: localCdkVersion() }
123-
}(this, 'TestConstruct');
110+
new TestConstruct(this, 'TestConstruct');
124111
}
125112
}(stack, 'Nested');
126113

127114
const constructInfos = constructInfoFromStack(stack);
128115

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

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

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

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

162145
const fqns = constructInfos.map(info => info.fqn);
163-
expect(fqns).toContain('@aws-cdk/test.TestV1Construct');
146+
expect(fqns).toContain('@aws-cdk/test.TestConstruct');
164147
expect(fqns).not.toContain('@aws-cdk/test.TestStackInsideStack');
165148
expect(fqns).not.toContain('@aws-cdk/test.TestNestedStackInsideStack');
166149
expect(fqns).not.toContain('@aws-cdk/test.TestStageInsideStack');
167150
});
168151
});
169152

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

175158
/**

0 commit comments

Comments
 (0)