Skip to content

Commit 7cee5b3

Browse files
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.
1 parent 0bcc4b4 commit 7cee5b3

File tree

25 files changed

+75
-33
lines changed

25 files changed

+75
-33
lines changed

nx.json

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"^build"
66
],
77
"inputs": [
8+
"{projectRoot}/**/bin/!(*.d|*.generated).ts",
89
"{projectRoot}/**/lib/!(*.d|*.generated).ts",
910
"{projectRoot}/**/test/!(*.d).ts",
1011
"!{workspaceRoot}/**/tsconfig.json",
@@ -33,6 +34,9 @@
3334
"test": {
3435
"dependsOn": [
3536
"build"
37+
],
38+
"inputs": [
39+
"{projectRoot}/**/test/!(*.d|*.generated).ts"
3640
]
3741
}
3842
},

packages/@aws-cdk/cloud-assembly-schema/jest.config.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
const baseConfig = require('@aws-cdk/cdk-build-tools/config/jest.config');
22
module.exports = {
33
...baseConfig,
4+
moduleFileExtensions: [
5+
'js',
6+
'ts',
7+
],
8+
preset: 'ts-jest',
9+
testMatch: [
10+
'<rootDir>/**/test/**/?(*.)+(test).ts',
11+
],
412
coverageThreshold: {
513
global: {
614
branches: 70,

packages/@aws-cdk/cloud-assembly-schema/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
}
2424
},
2525
"jsii": {
26+
"excludeTypescript": [
27+
"**/test/**/*.ts"
28+
],
2629
"outdir": "dist",
2730
"targets": {
2831
"java": {
+13-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
const baseConfig = require('@aws-cdk/cdk-build-tools/config/jest.config');
22
module.exports = {
33
...baseConfig,
4-
coverageThreshold: {
5-
global: {
6-
...baseConfig.coverageThreshold.global,
7-
branches: 70,
8-
},
4+
moduleFileExtensions: [
5+
'js',
6+
'ts',
7+
],
8+
preset: 'ts-jest',
9+
testMatch: [
10+
'<rootDir>/**/test/**/?(*.)+(test).ts',
11+
],
12+
coverageThreshold: {
13+
global: {
14+
...baseConfig.coverageThreshold.global,
15+
branches: 70,
916
},
17+
},
1018
};

packages/@aws-cdk/cx-api/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
}
2323
},
2424
"jsii": {
25+
"excludeTypescript": [
26+
"**/test/**/*.ts"
27+
],
2528
"outdir": "dist",
2629
"targets": {
2730
"java": {
+11-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
const baseConfig = require('@aws-cdk/cdk-build-tools/config/jest.config');
2-
module.exports = baseConfig;
2+
module.exports = {
3+
...baseConfig,
4+
moduleFileExtensions: [
5+
'js',
6+
'ts',
7+
],
8+
preset: 'ts-jest',
9+
testMatch: [
10+
'<rootDir>/**/test/**/?(*.)+(test).ts',
11+
],
12+
};

packages/@aws-cdk/region-info/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
}
2323
},
2424
"jsii": {
25+
"excludeTypescript": [
26+
"**/test/**/*.ts"
27+
],
2528
"outdir": "dist",
2629
"targets": {
2730
"java": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!index.js

packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ test('esbuild bundling with externals and dependencies', () => {
171171
command: [
172172
'bash', '-c',
173173
[
174-
'esbuild --bundle "/asset-input/test/bundling.test.js" --target=node14 --platform=node --outfile="/asset-output/index.js" --external:abc --external:delay',
174+
'esbuild --bundle "/asset-input/test/bundling.test.ts" --target=node14 --platform=node --outfile="/asset-output/index.js" --external:abc --external:delay',
175175
`echo \'{\"dependencies\":{\"delay\":\"${delayVersion}\"}}\' > "/asset-output/package.json"`,
176176
'cp "/asset-input/package-lock.json" "/asset-output/package-lock.json"',
177177
'cd "/asset-output"',
@@ -619,7 +619,7 @@ test('esbuild bundling with projectRoot and externals and dependencies', () => {
619619
command: [
620620
'bash', '-c',
621621
[
622-
'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',
622+
'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',
623623
`echo \'{\"dependencies\":{\"delay\":\"${delayVersion}\"}}\' > "/asset-output/package.json"`,
624624
'cp "/asset-input/common/package-lock.json" "/asset-output/package-lock.json"',
625625
'cd "/asset-output"',

packages/aws-cdk-lib/aws-lambda-nodejs/test/util.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ beforeEach(() => {
99
});
1010

1111
describe('callsites', () => {
12-
expect(callsites()[0].getFileName()).toMatch(/\/test\/util.test.js$/);
12+
expect(callsites()[0].getFileName()).toMatch(/\/test\/util.test.ts$/);
1313
});
1414

1515
describe('findUp helpers', () => {

packages/aws-cdk-lib/aws-lambda/test/code.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ describe('code', () => {
349349
// then
350350
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
351351
Metadata: {
352-
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.30b57ded32316be9aa6553a1d81689f1e0cb475a94306c557e05048f9f56bd79',
352+
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.a8922a1fd787021d071844bc2b1fe3622372bbdfda823528c00983a806ba0e26',
353353
[cxapi.ASSET_RESOURCE_METADATA_DOCKERFILE_PATH_KEY]: dockerfilePath,
354354
[cxapi.ASSET_RESOURCE_METADATA_DOCKER_BUILD_ARGS_KEY]: dockerBuildArgs,
355355
[cxapi.ASSET_RESOURCE_METADATA_DOCKER_BUILD_TARGET_KEY]: dockerBuildTarget,
@@ -373,7 +373,7 @@ describe('code', () => {
373373
// then
374374
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
375375
Metadata: {
376-
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.768d7b6c1d41b85135f498fe0cca69fea410be3c3322c69cf08690aaad29a610',
376+
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.9aa0b55c7d044a059d82665d34dd01705b055e9f691643e9606783faa9c92c54',
377377
[cxapi.ASSET_RESOURCE_METADATA_DOCKERFILE_PATH_KEY]: 'Dockerfile',
378378
[cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY]: 'Code.ImageUri',
379379
},
@@ -437,7 +437,7 @@ describe('code', () => {
437437
// then
438438
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
439439
Metadata: {
440-
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.fbafdbb9ae8d1bae0def415b791a93c486d18ebc63270c748abecc3ac0ab9533',
440+
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.7bbf7edf9881819a1b91e5b02acae3e3973f96fa93325c676a1285351ddacc62',
441441
[cxapi.ASSET_RESOURCE_METADATA_IS_BUNDLED_KEY]: false,
442442
[cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY]: 'Code',
443443
},

packages/aws-cdk-lib/aws-lambda/test/layers.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('layers', () => {
8585
// THEN
8686
Template.fromStack(stack).hasResource('AWS::Lambda::LayerVersion', {
8787
Metadata: {
88-
'aws:asset:path': 'asset.8811a2632ac5564a08fd269e159298f7e497f259578b0dc5e927a1f48ab24d34',
88+
'aws:asset:path': 'asset.77023498ccd2a36a67da602f712470107a3e7476646238ed135367d435d0fa43',
8989
'aws:asset:is-bundled': false,
9090
'aws:asset:property': 'Content',
9191
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!index.js

packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/index.js

Whitespace-only changes.

packages/aws-cdk-lib/core/test/custom-resource-provider/mock-provider/index.ts

-10
This file was deleted.

packages/aws-cdk-lib/core/test/stack-synthesis/clicreds-synthesis.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('CLI creds synthesis', () => {
6464

6565
// THEN - we have a fixed asset location with region placeholders
6666
expect(evalCFN(location.bucketName)).toEqual('cdk-hnb659fds-assets-the_account-the_region');
67-
expect(evalCFN(location.s3Url)).toEqual('https://s3.the_region.domain.aws/cdk-hnb659fds-assets-the_account-the_region/abcdef.js');
67+
expect(evalCFN(location.s3Url)).toEqual('https://s3.the_region.domain.aws/cdk-hnb659fds-assets-the_account-the_region/abcdef.ts');
6868

6969
// THEN - object key contains source hash somewhere
7070
expect(location.objectKey.indexOf('abcdef')).toBeGreaterThan(-1);
@@ -135,7 +135,7 @@ describe('CLI creds synthesis', () => {
135135

136136
expect(manifest.files?.['file-asset-hash']?.destinations?.['current_account-current_region']).toEqual({
137137
bucketName: 'file-asset-bucket',
138-
objectKey: 'file-asset-hash.js',
138+
objectKey: 'file-asset-hash.ts',
139139
});
140140

141141
expect(manifest.dockerImages?.['docker-asset-hash']?.destinations?.['current_account-current_region']).toEqual({
@@ -174,7 +174,7 @@ describe('CLI creds synthesis', () => {
174174
// THEN
175175
expect(manifest.files?.['file-asset-hash-with-prefix']?.destinations?.['current_account-current_region']).toEqual({
176176
bucketName: 'file-asset-bucket',
177-
objectKey: '000000000000/file-asset-hash-with-prefix.js',
177+
objectKey: '000000000000/file-asset-hash-with-prefix.ts',
178178
});
179179

180180
const templateHash = last(stackArtifact.stackTemplateAssetObjectUrl?.split('/'));

packages/aws-cdk-lib/core/test/stack-synthesis/new-style-synthesis.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ describe('new style synthesis', () => {
216216

217217
// THEN - we have a fixed asset location with region placeholders
218218
expect(evalCFN(location.bucketName)).toEqual('cdk-hnb659fds-assets-the_account-the_region');
219-
expect(evalCFN(location.s3Url)).toEqual('https://s3.the_region.domain.aws/cdk-hnb659fds-assets-the_account-the_region/abcdef.js');
219+
expect(evalCFN(location.s3Url)).toEqual('https://s3.the_region.domain.aws/cdk-hnb659fds-assets-the_account-the_region/abcdef.ts');
220220

221221
// THEN - object key contains source hash somewhere
222222
expect(location.objectKey.indexOf('abcdef')).toBeGreaterThan(-1);
@@ -331,7 +331,7 @@ describe('new style synthesis', () => {
331331

332332
expect(manifest.files?.['file-asset-hash']?.destinations?.['current_account-current_region']).toEqual({
333333
bucketName: 'file-asset-bucket',
334-
objectKey: 'file-asset-hash.js',
334+
objectKey: 'file-asset-hash.ts',
335335
assumeRoleArn: 'file:role:arn',
336336
assumeRoleExternalId: 'file-external-id',
337337
});
@@ -396,7 +396,7 @@ describe('new style synthesis', () => {
396396
// THEN
397397
expect(manifest.files?.['file-asset-hash-with-prefix']?.destinations?.['current_account-current_region']).toEqual({
398398
bucketName: 'file-asset-bucket',
399-
objectKey: '000000000000/file-asset-hash-with-prefix.js',
399+
objectKey: '000000000000/file-asset-hash-with-prefix.ts',
400400
assumeRoleArn: 'file:role:arn',
401401
assumeRoleExternalId: 'file-external-id',
402402
});

packages/aws-cdk-lib/jest.config.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
const baseConfig = require('@aws-cdk/cdk-build-tools/config/jest.config');
22

3+
const cpuCount = require('os').cpus().length;
4+
5+
/** @type {import('ts-jest').JestConfigWithTsJest} */
36
module.exports = {
47
...baseConfig,
8+
moduleFileExtensions: [
9+
'js',
10+
'ts',
11+
],
12+
// Limit workers to a reasonable fixed number. If we scale in the number of available CPUs, we will explode
13+
// our memory limit on the CodeBuild instance that has 72 CPUs.
14+
maxWorkers: Math.min(8, cpuCount - 1),
15+
preset: 'ts-jest',
516
testMatch: [
6-
"<rootDir>/**/test/**/?(*.)+(test).js",
17+
'<rootDir>/**/test/**/?(*.)+(test).ts',
718
],
8-
testEnvironment: 'node',
9-
coverageThreshold: {
19+
coverageThreshold: {
1020
global: {
1121
branches: 35,
1222
statements: 55,

packages/aws-cdk-lib/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"gen": "ts-node -P tsconfig.dev.json scripts/gen.ts",
1616
"build": "cdk-build",
1717
"lint": "cdk-lint",
18-
"test": "jest --maxWorkers=50%",
18+
"test": "jest",
1919
"package": "cdk-package",
2020
"pkglint": "pkglint -f",
2121
"build+test": "yarn build && yarn test",
@@ -70,6 +70,7 @@
7070
"jsii": {
7171
"excludeTypescript": [
7272
"scripts",
73+
"**/test/**/*.ts",
7374
"**/*.d.ts"
7475
],
7576
"outdir": "dist",

0 commit comments

Comments
 (0)