Skip to content

Commit

Permalink
chore(aws-cdk-lib): increase jest test speed, remove warnings (#26854)
Browse files Browse the repository at this point in the history
Two changes to the `jest` tests:

- We are currently running the tests directly from `.ts` source files using `ts-jest`. Disable type checking (by setting `isolatedModules: true`) to improve the speed. This brings down running 4 test suites from an arbitrary service (`aws-servicecatalog`) down from `79s` to `19s` on my machine.
- Change the "coverage ignore files" a little to get rid of the "Note: The code generator has deoptimised the styling" errors produced by jest when it wants to read the coverage of the generated source files.

----

*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 Aug 25, 2023
1 parent 356f302 commit 6227c94
Show file tree
Hide file tree
Showing 37 changed files with 203 additions and 160 deletions.
3 changes: 2 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"!{workspaceRoot}/**/tsconfig.json",
"!{workspaceRoot}/**/tsconfig.json",
"!{workspaceRoot}/tsconfig.base.json",
"!{workspaceRoot}/**/tsconfig.tsbuildinfo"
"!{workspaceRoot}/**/tsconfig.tsbuildinfo",
"!{workspaceRoot}/**/jest.config.js"
],
"outputs": [
"{projectRoot}/**/*.integ.*.js.snapshot/*",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"jsii-pacmak": "1.87.0",
"jsii-reflect": "1.87.0",
"jsii-rosetta": "~5.1.10",
"ts-jest": "^29.1.1",
"lerna": "^7.1.5",
"nx": "^16.7.3",
"patch-package": "^6.5.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { sleep } from '../lib';
import { ResourcePool } from '../lib/resource-pool';

jest.setTimeout(30_000);

const POOL_NAME = 'resource-pool.test';

test('take and dispose', async () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/@aws-cdk-testing/framework-integ/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ nyc.config.js

!**/*.snapshot/**/asset.*/**

**/*.ts.snapshot
**/*.ts.snapshot

!jest.config.js
14 changes: 14 additions & 0 deletions packages/@aws-cdk-testing/framework-integ/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
// Purposely only run .js files, not .ts files. This is so that the unit tests
// here will use the jsii-compiled version of `aws-cdk-lib`, and not the live-interpreted
// .ts files.
moduleFileExtensions: [
'js',
],
testMatch: [
'<rootDir>/test/**/?(*.)+(test).js',
],

testEnvironment: 'node',
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"32.0.0"}
{"version":"33.0.0"}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "32.0.0",
"version": "33.0.0",
"files": {
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
"source": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"version": "32.0.0",
"version": "33.0.0",
"testCases": {
"iam-test-condition-with-ref/DefaultTest": {
"stacks": [
"test-condition-with-ref"
],
"diffAssets": true,
"assertionStack": "iam-test-condition-with-ref/DefaultTest/DeployAssert",
"assertionStackName": "iamtestconditionwithrefDefaultTestDeployAssert5E44A64B"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "32.0.0",
"version": "33.0.0",
"artifacts": {
"test-condition-with-ref.assets": {
"type": "cdk:asset-manifest",
Expand All @@ -17,7 +17,7 @@
"validateOnSynth": false,
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/5d543b44583359346e7d4e1d73209c742ec533540ce929f2f2493fb27b0e738d.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/c78c907e2231879b958741f82c29330bcd35d5778c279f9e497f7e8565e6e2cc.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"version": "32.0.0",
"version": "33.0.0",
"files": {
"a913a2614f4e118cb83fa3c0dd17575c9adbbdbad4da17aecd505282a25513f8": {
"1e14e895fcbdf65feb0a29e4aa74c6c92a6fb0e41f228bef7ab23627ed409cde": {
"source": {
"path": "asset.a913a2614f4e118cb83fa3c0dd17575c9adbbdbad4da17aecd505282a25513f8",
"path": "asset.1e14e895fcbdf65feb0a29e4aa74c6c92a6fb0e41f228bef7ab23627ed409cde",
"packaging": "zip"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "a913a2614f4e118cb83fa3c0dd17575c9adbbdbad4da17aecd505282a25513f8.zip",
"objectKey": "1e14e895fcbdf65feb0a29e4aa74c6c92a6fb0e41f228bef7ab23627ed409cde.zip",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
},
"5d543b44583359346e7d4e1d73209c742ec533540ce929f2f2493fb27b0e738d": {
"c78c907e2231879b958741f82c29330bcd35d5778c279f9e497f7e8565e6e2cc": {
"source": {
"path": "test-condition-with-ref.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "5d543b44583359346e7d4e1d73209c742ec533540ce929f2f2493fb27b0e738d.json",
"objectKey": "c78c907e2231879b958741f82c29330bcd35d5778c279f9e497f7e8565e6e2cc.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "a913a2614f4e118cb83fa3c0dd17575c9adbbdbad4da17aecd505282a25513f8.zip"
"S3Key": "1e14e895fcbdf65feb0a29e4aa74c6c92a6fb0e41f228bef7ab23627ed409cde.zip"
},
"Timeout": 900,
"MemorySize": 128,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
"path": "iam-test-condition-with-ref/DefaultTest/Default",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.55"
"version": "10.2.69"
}
},
"DeployAssert": {
Expand Down Expand Up @@ -225,7 +225,7 @@
"path": "Tree",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.55"
"version": "10.2.69"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
/**
* Unit tests that depend on 'aws-cdk-lib' having been compiled using jsii
*/
import * as fs from 'fs';
import * as path from 'path';
import { Construct } from 'constructs';
import { App, NestedStack, Stack, Stage, IPolicyValidationPluginBeta1, PolicyViolationBeta1, PolicyValidationPluginReportBeta1, IPolicyValidationContextBeta1 } from '../lib';
import { constructInfoFromConstruct, constructInfoFromStack } from '../lib/private/runtime-info';
import { App, NestedStack, Stack, Stage, IPolicyValidationPluginBeta1, PolicyViolationBeta1, PolicyValidationPluginReportBeta1, IPolicyValidationContextBeta1 } from 'aws-cdk-lib';
import { constructInfoFromConstruct, constructInfoFromStack } from 'aws-cdk-lib/core/lib/helpers-internal';

const JSII_RUNTIME_SYMBOL = Symbol.for('jsii.rtti');

let app: App;
let stack: Stack;
let _cdkVersion: string | undefined = undefined;

// The runtime metadata this test relies on is only available if the most
// recent compile has happened using 'jsii', as the jsii compiler injects
// this metadata.
//
// If the most recent compile was using 'tsc', the metadata will not have
// been injected, and the test suite will fail.
//
// Tolerate `tsc` builds locally, but not on CodeBuild.
const codeBuild = !!process.env.CODEBUILD_BUILD_ID;
const moduleCompiledWithTsc = constructInfoFromConstruct(new Stack())?.fqn === 'constructs.Construct';
let describeTscSafe = describe;
if (moduleCompiledWithTsc && !codeBuild) {
// eslint-disable-next-line
console.error('It appears this module was compiled with `tsc` instead of `jsii` in a local build. Skipping this test suite.');
describeTscSafe = describe.skip;
}

beforeEach(() => {
app = new App();
stack = new Stack(app, 'Stack', {
analyticsReporting: true,
});
});

describeTscSafe('constructInfoFromConstruct', () => {
describe('constructInfoFromConstruct', () => {
test('returns fqn and version for core constructs', () => {
const constructInfo = constructInfoFromConstruct(stack);
expect(constructInfo).toBeDefined();
Expand Down Expand Up @@ -70,7 +56,7 @@ describeTscSafe('constructInfoFromConstruct', () => {
});
});

describeTscSafe('constructInfoForStack', () => {
describe('constructInfoForStack', () => {
test('returns stack itself and jsii runtime if stack is empty', () => {
const constructInfos = constructInfoFromStack(stack);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/**
* Unit tests that depend on 'aws-cdk-lib' having been compiled using jsii
*/
import * as fs from 'fs';
import * as path from 'path';
import { Construct } from 'constructs';
import * as cxschema from '../../../cloud-assembly-schema';
import { App, CfnParameter, CfnResource, Lazy, Stack, TreeInspector } from '../../lib/index';
import * as cxschema from 'aws-cdk-lib/cloud-assembly-schema';
import { App, CfnParameter, CfnResource, Lazy, Stack, TreeInspector } from 'aws-cdk-lib';

abstract class AbstractCfnResource extends CfnResource {
constructor(scope: Construct, id: string) {
Expand Down Expand Up @@ -160,21 +163,19 @@ describe('tree metadata', () => {
const treeArtifact = assembly.tree();
expect(treeArtifact).toBeDefined();

const codeBuild = !!process.env.CODEBUILD_BUILD_ID;

expect(readJson(assembly.directory, treeArtifact!.file)).toEqual({
version: 'tree-0.1',
tree: expect.objectContaining({
children: expect.objectContaining({
mystack: expect.objectContaining({
constructInfo: {
fqn: expect.stringMatching(codeBuild ? /\bStack$/ : /\bStack$|^constructs.Construct$/),
fqn: expect.stringMatching(/\bStack$/),
version: expect.any(String),
},
children: expect.objectContaining({
myconstruct: expect.objectContaining({
constructInfo: {
fqn: expect.stringMatching(codeBuild ? /\bCfnResource$/ : /\bCfnResource$|^constructs.Construct$/),
fqn: expect.stringMatching(/\bCfnResource$/),
version: expect.any(String),
},
}),
Expand Down Expand Up @@ -398,4 +399,4 @@ describe('tree metadata', () => {

function readJson(outdir: string, file: string) {
return JSON.parse(fs.readFileSync(path.join(outdir, file), 'utf-8'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe(AppStagingSynthesizer, () => {

// THEN - we have a fixed asset location
expect(evalCFN(location.bucketName)).toEqual(`cdk-${APP_ID}-staging-000000000000-us-east-1`);
expect(evalCFN(location.httpUrl)).toEqual(`https://s3.us-east-1.domain.aws/cdk-${APP_ID}-staging-000000000000-us-east-1/abcdef.js`);
expect(evalCFN(location.httpUrl)).toEqual(`https://s3.us-east-1.domain.aws/cdk-${APP_ID}-staging-000000000000-us-east-1/abcdef.ts`);

// THEN - object key contains source hash somewhere
expect(location.objectKey.indexOf('abcdef')).toBeGreaterThan(-1);
Expand Down Expand Up @@ -187,7 +187,7 @@ describe(AppStagingSynthesizer, () => {
});

// THEN - asset has deploy time prefix
expect(evalCFN(location.objectKey)).toEqual(`${DEPLOY_TIME_PREFIX}abcdef.js`);
expect(evalCFN(location.objectKey)).toEqual(`${DEPLOY_TIME_PREFIX}abcdef.ts`);
});

test('lambda assets are by default deploy time assets', () => {
Expand Down
5 changes: 0 additions & 5 deletions packages/@aws-cdk/cloud-assembly-schema/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
const baseConfig = require('@aws-cdk/cdk-build-tools/config/jest.config');
module.exports = {
...baseConfig,
moduleFileExtensions: [
'js',
'ts',
],
preset: 'ts-jest',
testMatch: [
'<rootDir>/**/test/**/?(*.)+(test).ts',
],
Expand Down
10 changes: 1 addition & 9 deletions packages/@aws-cdk/cx-api/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
const baseConfig = require('@aws-cdk/cdk-build-tools/config/jest.config');
module.exports = {
...baseConfig,
moduleFileExtensions: [
'js',
'ts',
],
preset: 'ts-jest',
testMatch: [
'<rootDir>/**/test/**/?(*.)+(test).ts',
],
...baseConfig,
coverageThreshold: {
global: {
...baseConfig.coverageThreshold.global,
Expand Down
Loading

0 comments on commit 6227c94

Please sign in to comment.