Skip to content

Commit

Permalink
chore(aws-cdk-lib): use ts-jest (aws#25728)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MrArnoldPalmer committed Aug 17, 2023
1 parent 0bcc4b4 commit 7cee5b3
Show file tree
Hide file tree
Showing 25 changed files with 75 additions and 33 deletions.
4 changes: 4 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"^build"
],
"inputs": [
"{projectRoot}/**/bin/!(*.d|*.generated).ts",
"{projectRoot}/**/lib/!(*.d|*.generated).ts",
"{projectRoot}/**/test/!(*.d).ts",
"!{workspaceRoot}/**/tsconfig.json",
Expand Down Expand Up @@ -33,6 +34,9 @@
"test": {
"dependsOn": [
"build"
],
"inputs": [
"{projectRoot}/**/test/!(*.d|*.generated).ts"
]
}
},
Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/cloud-assembly-schema/jest.config.js
Original file line number Diff line number Diff line change
@@ -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: [
'<rootDir>/**/test/**/?(*.)+(test).ts',
],
coverageThreshold: {
global: {
branches: 70,
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/cloud-assembly-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
}
},
"jsii": {
"excludeTypescript": [
"**/test/**/*.ts"
],
"outdir": "dist",
"targets": {
"java": {
Expand Down
18 changes: 13 additions & 5 deletions packages/@aws-cdk/cx-api/jest.config.js
Original file line number Diff line number Diff line change
@@ -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: [
'<rootDir>/**/test/**/?(*.)+(test).ts',
],
coverageThreshold: {
global: {
...baseConfig.coverageThreshold.global,
branches: 70,
},
},
};
3 changes: 3 additions & 0 deletions packages/@aws-cdk/cx-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
}
},
"jsii": {
"excludeTypescript": [
"**/test/**/*.ts"
],
"outdir": "dist",
"targets": {
"java": {
Expand Down
12 changes: 11 additions & 1 deletion packages/@aws-cdk/region-info/jest.config.js
Original file line number Diff line number Diff line change
@@ -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: [
'<rootDir>/**/test/**/?(*.)+(test).ts',
],
};
3 changes: 3 additions & 0 deletions packages/@aws-cdk/region-info/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
}
},
"jsii": {
"excludeTypescript": [
"**/test/**/*.ts"
],
"outdir": "dist",
"targets": {
"java": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!index.js
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
Expand Down Expand Up @@ -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"',
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-lambda-nodejs/test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/aws-cdk-lib/aws-lambda/test/code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
},
Expand Down Expand Up @@ -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',
},
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-lambda/test/layers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!index.js
Empty file.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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('/'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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',
});
Expand Down Expand Up @@ -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',
});
Expand Down
16 changes: 13 additions & 3 deletions packages/aws-cdk-lib/jest.config.js
Original file line number Diff line number Diff line change
@@ -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: [
"<rootDir>/**/test/**/?(*.)+(test).js",
'<rootDir>/**/test/**/?(*.)+(test).ts',
],
testEnvironment: 'node',
coverageThreshold: {
coverageThreshold: {
global: {
branches: 35,
statements: 55,
Expand Down
3 changes: 2 additions & 1 deletion packages/aws-cdk-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -70,6 +70,7 @@
"jsii": {
"excludeTypescript": [
"scripts",
"**/test/**/*.ts",
"**/*.d.ts"
],
"outdir": "dist",
Expand Down

0 comments on commit 7cee5b3

Please sign in to comment.