From 7cee5b346b38c7a962a8decaf04c53d2224932d5 Mon Sep 17 00:00:00 2001 From: Mitchell Valine Date: Thu, 17 Aug 2023 14:55:03 -0700 Subject: [PATCH] chore(aws-cdk-lib): use ts-jest (#25728) Switch to using ts-jest for unit tests in `aws-cdk-lib` instead of requiring all tests to be compiled. This lets us ignore test files when building which speeds up the build marginally, and allows for better integration with various tools, such as IDE plugins for jest so you can run individual tests without rebuilding. The workflow I have been using is running `jest --watch` either for all tests or only the directory or individual test I'm working on. When running all tests, the watch mode can be set to only rerun failed tests on change, which makes iterating on failures across multiple modules much easier. --- nx.json | 4 ++++ .../cloud-assembly-schema/jest.config.js | 8 ++++++++ .../cloud-assembly-schema/package.json | 3 +++ packages/@aws-cdk/cx-api/jest.config.js | 18 +++++++++++++----- packages/@aws-cdk/cx-api/package.json | 3 +++ packages/@aws-cdk/region-info/jest.config.js | 12 +++++++++++- packages/@aws-cdk/region-info/package.json | 3 +++ ...render.test.js.snap => render.test.ts.snap} | 0 .../aws-iam/test/custom-resource/.gitignore | 1 + .../custom-resource/{index.ts => index.js} | 0 ...ling.test.js.snap => bundling.test.ts.snap} | 0 .../aws-lambda-nodejs/test/bundling.test.ts | 4 ++-- .../aws-lambda-nodejs/test/util.test.ts | 2 +- .../aws-cdk-lib/aws-lambda/test/code.test.ts | 6 +++--- .../aws-cdk-lib/aws-lambda/test/layers.test.ts | 2 +- ...fest.test.js.snap => manifest.test.ts.snap} | 0 .../mock-provider/.gitignore | 1 + .../mock-provider/index.js | 0 .../mock-provider/index.ts | 10 ---------- .../stack-synthesis/clicreds-synthesis.test.ts | 6 +++--- .../new-style-synthesis.test.ts | 6 +++--- ...est.js.snap => cloud-assembly.test.ts.snap} | 0 packages/aws-cdk-lib/jest.config.js | 16 +++++++++++++--- packages/aws-cdk-lib/package.json | 3 ++- ...o.test.js.snap => region-info.test.ts.snap} | 0 25 files changed, 75 insertions(+), 33 deletions(-) rename packages/aws-cdk-lib/assertions/test/__snapshots__/{render.test.js.snap => render.test.ts.snap} (100%) create mode 100644 packages/aws-cdk-lib/aws-iam/test/custom-resource/.gitignore rename packages/aws-cdk-lib/aws-iam/test/custom-resource/{index.ts => index.js} (100%) rename packages/aws-cdk-lib/aws-lambda-nodejs/test/__snapshots__/{bundling.test.js.snap => bundling.test.ts.snap} (100%) rename packages/aws-cdk-lib/cloud-assembly-schema/test/__snapshots__/{manifest.test.js.snap => manifest.test.ts.snap} (100%) create mode 100644 packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/.gitignore create mode 100644 packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/index.js delete mode 100644 packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/index.ts rename packages/aws-cdk-lib/cx-api/test/__snapshots__/{cloud-assembly.test.js.snap => cloud-assembly.test.ts.snap} (100%) rename packages/aws-cdk-lib/region-info/test/__snapshots__/{region-info.test.js.snap => region-info.test.ts.snap} (100%) diff --git a/nx.json b/nx.json index 382134e82895e..e8872e82abceb 100644 --- a/nx.json +++ b/nx.json @@ -5,6 +5,7 @@ "^build" ], "inputs": [ + "{projectRoot}/**/bin/!(*.d|*.generated).ts", "{projectRoot}/**/lib/!(*.d|*.generated).ts", "{projectRoot}/**/test/!(*.d).ts", "!{workspaceRoot}/**/tsconfig.json", @@ -33,6 +34,9 @@ "test": { "dependsOn": [ "build" + ], + "inputs": [ + "{projectRoot}/**/test/!(*.d|*.generated).ts" ] } }, diff --git a/packages/@aws-cdk/cloud-assembly-schema/jest.config.js b/packages/@aws-cdk/cloud-assembly-schema/jest.config.js index 6478d8fb36426..1700f0873cc01 100644 --- a/packages/@aws-cdk/cloud-assembly-schema/jest.config.js +++ b/packages/@aws-cdk/cloud-assembly-schema/jest.config.js @@ -1,6 +1,14 @@ const baseConfig = require('@aws-cdk/cdk-build-tools/config/jest.config'); module.exports = { ...baseConfig, + moduleFileExtensions: [ + 'js', + 'ts', + ], + preset: 'ts-jest', + testMatch: [ + '/**/test/**/?(*.)+(test).ts', + ], coverageThreshold: { global: { branches: 70, diff --git a/packages/@aws-cdk/cloud-assembly-schema/package.json b/packages/@aws-cdk/cloud-assembly-schema/package.json index 0156681da010e..949bb02bb4816 100644 --- a/packages/@aws-cdk/cloud-assembly-schema/package.json +++ b/packages/@aws-cdk/cloud-assembly-schema/package.json @@ -23,6 +23,9 @@ } }, "jsii": { + "excludeTypescript": [ + "**/test/**/*.ts" + ], "outdir": "dist", "targets": { "java": { diff --git a/packages/@aws-cdk/cx-api/jest.config.js b/packages/@aws-cdk/cx-api/jest.config.js index 751c263a6e75c..37c818dbced5f 100644 --- a/packages/@aws-cdk/cx-api/jest.config.js +++ b/packages/@aws-cdk/cx-api/jest.config.js @@ -1,10 +1,18 @@ const baseConfig = require('@aws-cdk/cdk-build-tools/config/jest.config'); module.exports = { ...baseConfig, - coverageThreshold: { - global: { - ...baseConfig.coverageThreshold.global, - branches: 70, - }, + moduleFileExtensions: [ + 'js', + 'ts', + ], + preset: 'ts-jest', + testMatch: [ + '/**/test/**/?(*.)+(test).ts', + ], + coverageThreshold: { + global: { + ...baseConfig.coverageThreshold.global, + branches: 70, }, + }, }; diff --git a/packages/@aws-cdk/cx-api/package.json b/packages/@aws-cdk/cx-api/package.json index 31a552cece62f..1ab29c951658d 100644 --- a/packages/@aws-cdk/cx-api/package.json +++ b/packages/@aws-cdk/cx-api/package.json @@ -22,6 +22,9 @@ } }, "jsii": { + "excludeTypescript": [ + "**/test/**/*.ts" + ], "outdir": "dist", "targets": { "java": { diff --git a/packages/@aws-cdk/region-info/jest.config.js b/packages/@aws-cdk/region-info/jest.config.js index 3a2fd93a1228a..036d2133dfe23 100644 --- a/packages/@aws-cdk/region-info/jest.config.js +++ b/packages/@aws-cdk/region-info/jest.config.js @@ -1,2 +1,12 @@ const baseConfig = require('@aws-cdk/cdk-build-tools/config/jest.config'); -module.exports = baseConfig; +module.exports = { + ...baseConfig, + moduleFileExtensions: [ + 'js', + 'ts', + ], + preset: 'ts-jest', + testMatch: [ + '/**/test/**/?(*.)+(test).ts', + ], +}; diff --git a/packages/@aws-cdk/region-info/package.json b/packages/@aws-cdk/region-info/package.json index febbd32615f2e..8f976481aba98 100644 --- a/packages/@aws-cdk/region-info/package.json +++ b/packages/@aws-cdk/region-info/package.json @@ -22,6 +22,9 @@ } }, "jsii": { + "excludeTypescript": [ + "**/test/**/*.ts" + ], "outdir": "dist", "targets": { "java": { diff --git a/packages/aws-cdk-lib/assertions/test/__snapshots__/render.test.js.snap b/packages/aws-cdk-lib/assertions/test/__snapshots__/render.test.ts.snap similarity index 100% rename from packages/aws-cdk-lib/assertions/test/__snapshots__/render.test.js.snap rename to packages/aws-cdk-lib/assertions/test/__snapshots__/render.test.ts.snap diff --git a/packages/aws-cdk-lib/aws-iam/test/custom-resource/.gitignore b/packages/aws-cdk-lib/aws-iam/test/custom-resource/.gitignore new file mode 100644 index 0000000000000..033e6722bb6e0 --- /dev/null +++ b/packages/aws-cdk-lib/aws-iam/test/custom-resource/.gitignore @@ -0,0 +1 @@ +!index.js diff --git a/packages/aws-cdk-lib/aws-iam/test/custom-resource/index.ts b/packages/aws-cdk-lib/aws-iam/test/custom-resource/index.js similarity index 100% rename from packages/aws-cdk-lib/aws-iam/test/custom-resource/index.ts rename to packages/aws-cdk-lib/aws-iam/test/custom-resource/index.js diff --git a/packages/aws-cdk-lib/aws-lambda-nodejs/test/__snapshots__/bundling.test.js.snap b/packages/aws-cdk-lib/aws-lambda-nodejs/test/__snapshots__/bundling.test.ts.snap similarity index 100% rename from packages/aws-cdk-lib/aws-lambda-nodejs/test/__snapshots__/bundling.test.js.snap rename to packages/aws-cdk-lib/aws-lambda-nodejs/test/__snapshots__/bundling.test.ts.snap diff --git a/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts b/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts index 718bc1167aa9b..d865c8be34a0a 100644 --- a/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts +++ b/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts @@ -171,7 +171,7 @@ test('esbuild bundling with externals and dependencies', () => { command: [ 'bash', '-c', [ - 'esbuild --bundle "/asset-input/test/bundling.test.js" --target=node14 --platform=node --outfile="/asset-output/index.js" --external:abc --external:delay', + 'esbuild --bundle "/asset-input/test/bundling.test.ts" --target=node14 --platform=node --outfile="/asset-output/index.js" --external:abc --external:delay', `echo \'{\"dependencies\":{\"delay\":\"${delayVersion}\"}}\' > "/asset-output/package.json"`, 'cp "/asset-input/package-lock.json" "/asset-output/package-lock.json"', 'cd "/asset-output"', @@ -619,7 +619,7 @@ test('esbuild bundling with projectRoot and externals and dependencies', () => { command: [ 'bash', '-c', [ - 'esbuild --bundle "/asset-input/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.js" --target=node14 --platform=node --outfile="/asset-output/index.js" --external:abc --external:delay', + 'esbuild --bundle "/asset-input/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts" --target=node14 --platform=node --outfile="/asset-output/index.js" --external:abc --external:delay', `echo \'{\"dependencies\":{\"delay\":\"${delayVersion}\"}}\' > "/asset-output/package.json"`, 'cp "/asset-input/common/package-lock.json" "/asset-output/package-lock.json"', 'cd "/asset-output"', diff --git a/packages/aws-cdk-lib/aws-lambda-nodejs/test/util.test.ts b/packages/aws-cdk-lib/aws-lambda-nodejs/test/util.test.ts index 90e3c067c02a0..71c0171897d57 100644 --- a/packages/aws-cdk-lib/aws-lambda-nodejs/test/util.test.ts +++ b/packages/aws-cdk-lib/aws-lambda-nodejs/test/util.test.ts @@ -9,7 +9,7 @@ beforeEach(() => { }); describe('callsites', () => { - expect(callsites()[0].getFileName()).toMatch(/\/test\/util.test.js$/); + expect(callsites()[0].getFileName()).toMatch(/\/test\/util.test.ts$/); }); describe('findUp helpers', () => { diff --git a/packages/aws-cdk-lib/aws-lambda/test/code.test.ts b/packages/aws-cdk-lib/aws-lambda/test/code.test.ts index 2808ac4fcefea..4c6da0a8b6e86 100644 --- a/packages/aws-cdk-lib/aws-lambda/test/code.test.ts +++ b/packages/aws-cdk-lib/aws-lambda/test/code.test.ts @@ -349,7 +349,7 @@ describe('code', () => { // then Template.fromStack(stack).hasResource('AWS::Lambda::Function', { Metadata: { - [cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.30b57ded32316be9aa6553a1d81689f1e0cb475a94306c557e05048f9f56bd79', + [cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.a8922a1fd787021d071844bc2b1fe3622372bbdfda823528c00983a806ba0e26', [cxapi.ASSET_RESOURCE_METADATA_DOCKERFILE_PATH_KEY]: dockerfilePath, [cxapi.ASSET_RESOURCE_METADATA_DOCKER_BUILD_ARGS_KEY]: dockerBuildArgs, [cxapi.ASSET_RESOURCE_METADATA_DOCKER_BUILD_TARGET_KEY]: dockerBuildTarget, @@ -373,7 +373,7 @@ describe('code', () => { // then Template.fromStack(stack).hasResource('AWS::Lambda::Function', { Metadata: { - [cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.768d7b6c1d41b85135f498fe0cca69fea410be3c3322c69cf08690aaad29a610', + [cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.9aa0b55c7d044a059d82665d34dd01705b055e9f691643e9606783faa9c92c54', [cxapi.ASSET_RESOURCE_METADATA_DOCKERFILE_PATH_KEY]: 'Dockerfile', [cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY]: 'Code.ImageUri', }, @@ -437,7 +437,7 @@ describe('code', () => { // then Template.fromStack(stack).hasResource('AWS::Lambda::Function', { Metadata: { - [cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.fbafdbb9ae8d1bae0def415b791a93c486d18ebc63270c748abecc3ac0ab9533', + [cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.7bbf7edf9881819a1b91e5b02acae3e3973f96fa93325c676a1285351ddacc62', [cxapi.ASSET_RESOURCE_METADATA_IS_BUNDLED_KEY]: false, [cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY]: 'Code', }, diff --git a/packages/aws-cdk-lib/aws-lambda/test/layers.test.ts b/packages/aws-cdk-lib/aws-lambda/test/layers.test.ts index 4f04e61d0f23f..9c8ac9c694978 100644 --- a/packages/aws-cdk-lib/aws-lambda/test/layers.test.ts +++ b/packages/aws-cdk-lib/aws-lambda/test/layers.test.ts @@ -85,7 +85,7 @@ describe('layers', () => { // THEN Template.fromStack(stack).hasResource('AWS::Lambda::LayerVersion', { Metadata: { - 'aws:asset:path': 'asset.8811a2632ac5564a08fd269e159298f7e497f259578b0dc5e927a1f48ab24d34', + 'aws:asset:path': 'asset.77023498ccd2a36a67da602f712470107a3e7476646238ed135367d435d0fa43', 'aws:asset:is-bundled': false, 'aws:asset:property': 'Content', }, diff --git a/packages/aws-cdk-lib/cloud-assembly-schema/test/__snapshots__/manifest.test.js.snap b/packages/aws-cdk-lib/cloud-assembly-schema/test/__snapshots__/manifest.test.ts.snap similarity index 100% rename from packages/aws-cdk-lib/cloud-assembly-schema/test/__snapshots__/manifest.test.js.snap rename to packages/aws-cdk-lib/cloud-assembly-schema/test/__snapshots__/manifest.test.ts.snap diff --git a/packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/.gitignore b/packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/.gitignore new file mode 100644 index 0000000000000..033e6722bb6e0 --- /dev/null +++ b/packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/.gitignore @@ -0,0 +1 @@ +!index.js diff --git a/packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/index.js b/packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/index.js new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/index.ts b/packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/index.ts deleted file mode 100644 index 7ed7ebf8df0b3..0000000000000 --- a/packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -/* eslint-disable no-console */ - -export function handler(event: any) { - console.log('I am a custom resource'); - console.log({ ...event, ResponseURL: undefined }); - return { - PhysicalResourceId: event.ResourceProperties.physicalResourceId, - Data: event.ResourceProperties.attributes, - }; -} diff --git a/packages/aws-cdk-lib/core/test/stack-synthesis/clicreds-synthesis.test.ts b/packages/aws-cdk-lib/core/test/stack-synthesis/clicreds-synthesis.test.ts index 07f9b8bb10db8..b47e65ba74f62 100644 --- a/packages/aws-cdk-lib/core/test/stack-synthesis/clicreds-synthesis.test.ts +++ b/packages/aws-cdk-lib/core/test/stack-synthesis/clicreds-synthesis.test.ts @@ -64,7 +64,7 @@ describe('CLI creds synthesis', () => { // THEN - we have a fixed asset location with region placeholders expect(evalCFN(location.bucketName)).toEqual('cdk-hnb659fds-assets-the_account-the_region'); - expect(evalCFN(location.s3Url)).toEqual('https://s3.the_region.domain.aws/cdk-hnb659fds-assets-the_account-the_region/abcdef.js'); + expect(evalCFN(location.s3Url)).toEqual('https://s3.the_region.domain.aws/cdk-hnb659fds-assets-the_account-the_region/abcdef.ts'); // THEN - object key contains source hash somewhere expect(location.objectKey.indexOf('abcdef')).toBeGreaterThan(-1); @@ -135,7 +135,7 @@ describe('CLI creds synthesis', () => { expect(manifest.files?.['file-asset-hash']?.destinations?.['current_account-current_region']).toEqual({ bucketName: 'file-asset-bucket', - objectKey: 'file-asset-hash.js', + objectKey: 'file-asset-hash.ts', }); expect(manifest.dockerImages?.['docker-asset-hash']?.destinations?.['current_account-current_region']).toEqual({ @@ -174,7 +174,7 @@ describe('CLI creds synthesis', () => { // THEN expect(manifest.files?.['file-asset-hash-with-prefix']?.destinations?.['current_account-current_region']).toEqual({ bucketName: 'file-asset-bucket', - objectKey: '000000000000/file-asset-hash-with-prefix.js', + objectKey: '000000000000/file-asset-hash-with-prefix.ts', }); const templateHash = last(stackArtifact.stackTemplateAssetObjectUrl?.split('/')); diff --git a/packages/aws-cdk-lib/core/test/stack-synthesis/new-style-synthesis.test.ts b/packages/aws-cdk-lib/core/test/stack-synthesis/new-style-synthesis.test.ts index 0a61f07537a09..6c8fee21b79fd 100644 --- a/packages/aws-cdk-lib/core/test/stack-synthesis/new-style-synthesis.test.ts +++ b/packages/aws-cdk-lib/core/test/stack-synthesis/new-style-synthesis.test.ts @@ -216,7 +216,7 @@ describe('new style synthesis', () => { // THEN - we have a fixed asset location with region placeholders expect(evalCFN(location.bucketName)).toEqual('cdk-hnb659fds-assets-the_account-the_region'); - expect(evalCFN(location.s3Url)).toEqual('https://s3.the_region.domain.aws/cdk-hnb659fds-assets-the_account-the_region/abcdef.js'); + expect(evalCFN(location.s3Url)).toEqual('https://s3.the_region.domain.aws/cdk-hnb659fds-assets-the_account-the_region/abcdef.ts'); // THEN - object key contains source hash somewhere expect(location.objectKey.indexOf('abcdef')).toBeGreaterThan(-1); @@ -331,7 +331,7 @@ describe('new style synthesis', () => { expect(manifest.files?.['file-asset-hash']?.destinations?.['current_account-current_region']).toEqual({ bucketName: 'file-asset-bucket', - objectKey: 'file-asset-hash.js', + objectKey: 'file-asset-hash.ts', assumeRoleArn: 'file:role:arn', assumeRoleExternalId: 'file-external-id', }); @@ -396,7 +396,7 @@ describe('new style synthesis', () => { // THEN expect(manifest.files?.['file-asset-hash-with-prefix']?.destinations?.['current_account-current_region']).toEqual({ bucketName: 'file-asset-bucket', - objectKey: '000000000000/file-asset-hash-with-prefix.js', + objectKey: '000000000000/file-asset-hash-with-prefix.ts', assumeRoleArn: 'file:role:arn', assumeRoleExternalId: 'file-external-id', }); diff --git a/packages/aws-cdk-lib/cx-api/test/__snapshots__/cloud-assembly.test.js.snap b/packages/aws-cdk-lib/cx-api/test/__snapshots__/cloud-assembly.test.ts.snap similarity index 100% rename from packages/aws-cdk-lib/cx-api/test/__snapshots__/cloud-assembly.test.js.snap rename to packages/aws-cdk-lib/cx-api/test/__snapshots__/cloud-assembly.test.ts.snap diff --git a/packages/aws-cdk-lib/jest.config.js b/packages/aws-cdk-lib/jest.config.js index 3e40b01eb4e1d..cea26228ba11f 100644 --- a/packages/aws-cdk-lib/jest.config.js +++ b/packages/aws-cdk-lib/jest.config.js @@ -1,12 +1,22 @@ const baseConfig = require('@aws-cdk/cdk-build-tools/config/jest.config'); +const cpuCount = require('os').cpus().length; + +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { ...baseConfig, + moduleFileExtensions: [ + 'js', + 'ts', + ], + // Limit workers to a reasonable fixed number. If we scale in the number of available CPUs, we will explode + // our memory limit on the CodeBuild instance that has 72 CPUs. + maxWorkers: Math.min(8, cpuCount - 1), + preset: 'ts-jest', testMatch: [ - "/**/test/**/?(*.)+(test).js", + '/**/test/**/?(*.)+(test).ts', ], - testEnvironment: 'node', - coverageThreshold: { + coverageThreshold: { global: { branches: 35, statements: 55, diff --git a/packages/aws-cdk-lib/package.json b/packages/aws-cdk-lib/package.json index eb5ac86bdb618..dbbac2914c953 100644 --- a/packages/aws-cdk-lib/package.json +++ b/packages/aws-cdk-lib/package.json @@ -15,7 +15,7 @@ "gen": "ts-node -P tsconfig.dev.json scripts/gen.ts", "build": "cdk-build", "lint": "cdk-lint", - "test": "jest --maxWorkers=50%", + "test": "jest", "package": "cdk-package", "pkglint": "pkglint -f", "build+test": "yarn build && yarn test", @@ -70,6 +70,7 @@ "jsii": { "excludeTypescript": [ "scripts", + "**/test/**/*.ts", "**/*.d.ts" ], "outdir": "dist", diff --git a/packages/aws-cdk-lib/region-info/test/__snapshots__/region-info.test.js.snap b/packages/aws-cdk-lib/region-info/test/__snapshots__/region-info.test.ts.snap similarity index 100% rename from packages/aws-cdk-lib/region-info/test/__snapshots__/region-info.test.js.snap rename to packages/aws-cdk-lib/region-info/test/__snapshots__/region-info.test.ts.snap