From 1b5c2edb728a5b9118ad1325e78bf6862b4f805f Mon Sep 17 00:00:00 2001 From: Rico Hermans Date: Wed, 16 Apr 2025 16:57:37 +0200 Subject: [PATCH 01/11] chore: syntax check test files (#374) Currently, the test files are not syntax checked during TypeScript compilation, only during test running. This makes it hard to pick up on necessary changes during large-scale refactors. You will only notice them during test runs, and then every single test file will fail with a parse error (instead of potentially a single typechecking error during typechecking phase). Another complication with the trivial solution of just running `tsc -p tsconfig.dev.json` is that VSCode will not detect that `test/` files are included in a tsconfig file if it's not literally called `tsconfig.json`, so we have to do some trickery to type-check at compilation time AND convince VSCode to type check as well. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license --- .projen/deps.json | 5 ++ .projenrc.ts | 10 ++++ package.json | 1 + .../tmp-toolkit-helpers/.gitattributes | 1 + .../@aws-cdk/tmp-toolkit-helpers/.gitignore | 1 + .../tmp-toolkit-helpers/.projen/files.json | 1 + .../tmp-toolkit-helpers/.projen/tasks.json | 3 + .../src/context-providers/security-groups.ts | 3 +- .../test/_helpers/jest-bufferedconsole.ts | 2 +- .../test/api/diff/diff.test.ts | 8 --- .../tmp-toolkit-helpers/test/tsconfig.json | 16 +++++ packages/@aws-cdk/toolkit-lib/.gitattributes | 1 + packages/@aws-cdk/toolkit-lib/.gitignore | 1 + .../@aws-cdk/toolkit-lib/.projen/files.json | 1 + .../@aws-cdk/toolkit-lib/.projen/tasks.json | 3 + .../api/cloud-assembly/source-builder.test.ts | 10 ++-- .../@aws-cdk/toolkit-lib/test/tsconfig.json | 22 +++++++ packages/aws-cdk/.gitattributes | 1 + packages/aws-cdk/.gitignore | 1 + packages/aws-cdk/.projen/files.json | 1 + packages/aws-cdk/.projen/tasks.json | 3 + packages/aws-cdk/test/_helpers/mock-sdk.ts | 2 +- .../test/api/aws-auth/sdk-logger.test.ts | 2 +- .../test/api/aws-auth/sdk-provider.test.ts | 2 +- .../evaluate-cloudformation-template.test.ts | 1 + ...ping-templates-hotswap-deployments.test.ts | 8 +-- .../test/api/plugin/plugin-host.test.ts | 2 +- .../api/work-graph/work-graph-builder.test.ts | 8 +-- packages/aws-cdk/test/cli/cdk-toolkit.test.ts | 57 +++++++++--------- packages/aws-cdk/test/tsconfig.json | 34 +++++++++++ packages/cdk-assets/.gitattributes | 1 + packages/cdk-assets/.gitignore | 1 + packages/cdk-assets/.projen/files.json | 1 + packages/cdk-assets/.projen/tasks.json | 3 + packages/cdk-assets/test/tsconfig.json | 13 +++++ projenrc/TypecheckTests.ts | 58 +++++++++++++++++++ yarn.lock | 14 +---- 37 files changed, 234 insertions(+), 68 deletions(-) create mode 100644 packages/@aws-cdk/tmp-toolkit-helpers/test/tsconfig.json create mode 100644 packages/@aws-cdk/toolkit-lib/test/tsconfig.json create mode 100644 packages/aws-cdk/test/tsconfig.json create mode 100644 packages/cdk-assets/test/tsconfig.json create mode 100644 projenrc/TypecheckTests.ts diff --git a/.projen/deps.json b/.projen/deps.json index 77d3b751c..eee36469b 100644 --- a/.projen/deps.json +++ b/.projen/deps.json @@ -109,6 +109,11 @@ "version": "30.0.0-alpha.7", "type": "override" }, + { + "name": "@jest/types", + "version": "30.0.0-alpha.7", + "type": "override" + }, { "name": "jest-environment-node", "version": "30.0.0-alpha.7", diff --git a/.projenrc.ts b/.projenrc.ts index 07c58d42a..dbc939886 100644 --- a/.projenrc.ts +++ b/.projenrc.ts @@ -13,6 +13,7 @@ import { LargePrChecker } from './projenrc/large-pr-checker'; import { PrLabeler } from './projenrc/pr-labeler'; import { RecordPublishingTimestamp } from './projenrc/record-publishing-timestamp'; import { DocType, S3DocsPublishing } from './projenrc/s3-docs-publishing'; +import { TypecheckTests } from './projenrc/TypecheckTests'; // 5.7 sometimes gives a weird error in `ts-jest` in `@aws-cdk/cli-lib-alpha` // https://github.com/microsoft/TypeScript/issues/60159 @@ -270,6 +271,7 @@ const repoProject = new yarn.Monorepo({ repoProject.package.addPackageResolutions( 'jest-environment-node@30.0.0-alpha.7', '@jest/environment@30.0.0-alpha.7', + '@jest/types@30.0.0-alpha.7', ); new AdcPublishing(repoProject); @@ -654,6 +656,8 @@ const cdkAssets = configureProject( }), ); +new TypecheckTests(cdkAssets); + cdkAssets.addTask('shrinkwrap', { steps: [ { @@ -775,6 +779,8 @@ const tmpToolkitHelpers = configureProject( }), ); +new TypecheckTests(tmpToolkitHelpers); + // Prevent imports of private API surface tmpToolkitHelpers.package.addField('exports', { '.': './lib/index.js', @@ -924,6 +930,8 @@ const toolkitLib = configureProject( }), ); +new TypecheckTests(toolkitLib); + // TypeDoc documentation publishing new S3DocsPublishing(toolkitLib, { docsStream: 'toolkit-lib', @@ -1252,6 +1260,8 @@ const cli = configureProject( }), ); +new TypecheckTests(cli); + // Eslint rules cli.eslint?.addRules({ '@cdklabs/no-throw-default-error': 'error', diff --git a/package.json b/package.json index 49c9b5812..e63481365 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ }, "resolutions": { "@jest/environment": "30.0.0-alpha.7", + "@jest/types": "30.0.0-alpha.7", "jest-environment-node": "30.0.0-alpha.7" }, "engines": { diff --git a/packages/@aws-cdk/tmp-toolkit-helpers/.gitattributes b/packages/@aws-cdk/tmp-toolkit-helpers/.gitattributes index c1b26c9d0..f8b7d2522 100644 --- a/packages/@aws-cdk/tmp-toolkit-helpers/.gitattributes +++ b/packages/@aws-cdk/tmp-toolkit-helpers/.gitattributes @@ -15,6 +15,7 @@ /jest.config.json linguist-generated /LICENSE linguist-generated /package.json linguist-generated +/test/tsconfig.json linguist-generated /tsconfig.dev.json linguist-generated /tsconfig.json linguist-generated /yarn.lock linguist-generated \ No newline at end of file diff --git a/packages/@aws-cdk/tmp-toolkit-helpers/.gitignore b/packages/@aws-cdk/tmp-toolkit-helpers/.gitignore index 5945d3dc1..3e68013f5 100644 --- a/packages/@aws-cdk/tmp-toolkit-helpers/.gitignore +++ b/packages/@aws-cdk/tmp-toolkit-helpers/.gitignore @@ -41,4 +41,5 @@ jspm_packages/ /dist/ !/.eslintrc.json !/.eslintrc.js +!/test/tsconfig.json test/**/*.map diff --git a/packages/@aws-cdk/tmp-toolkit-helpers/.projen/files.json b/packages/@aws-cdk/tmp-toolkit-helpers/.projen/files.json index 493bbd87e..9b2047f9d 100644 --- a/packages/@aws-cdk/tmp-toolkit-helpers/.projen/files.json +++ b/packages/@aws-cdk/tmp-toolkit-helpers/.projen/files.json @@ -12,6 +12,7 @@ ".projen/tasks.json", "jest.config.json", "LICENSE", + "test/tsconfig.json", "tsconfig.dev.json", "tsconfig.json" ], diff --git a/packages/@aws-cdk/tmp-toolkit-helpers/.projen/tasks.json b/packages/@aws-cdk/tmp-toolkit-helpers/.projen/tasks.json index dc4de6664..47d86faae 100644 --- a/packages/@aws-cdk/tmp-toolkit-helpers/.projen/tasks.json +++ b/packages/@aws-cdk/tmp-toolkit-helpers/.projen/tasks.json @@ -48,6 +48,9 @@ { "exec": "tsc --build", "receiveArgs": true + }, + { + "exec": "tsc --build test" } ] }, diff --git a/packages/@aws-cdk/tmp-toolkit-helpers/src/context-providers/security-groups.ts b/packages/@aws-cdk/tmp-toolkit-helpers/src/context-providers/security-groups.ts index ef440558b..e820658ee 100644 --- a/packages/@aws-cdk/tmp-toolkit-helpers/src/context-providers/security-groups.ts +++ b/packages/@aws-cdk/tmp-toolkit-helpers/src/context-providers/security-groups.ts @@ -60,7 +60,8 @@ export class SecurityGroupContextProviderPlugin implements ContextProviderPlugin } /** - * @internal + * TODO: We intend this to be @*internal but a test in aws-cdk depends on it. + * Put the tag back later. */ export function hasAllTrafficEgress(securityGroup: SecurityGroup) { let hasAllTrafficCidrV4 = false; diff --git a/packages/@aws-cdk/tmp-toolkit-helpers/test/_helpers/jest-bufferedconsole.ts b/packages/@aws-cdk/tmp-toolkit-helpers/test/_helpers/jest-bufferedconsole.ts index 043b819ec..3569a60ca 100644 --- a/packages/@aws-cdk/tmp-toolkit-helpers/test/_helpers/jest-bufferedconsole.ts +++ b/packages/@aws-cdk/tmp-toolkit-helpers/test/_helpers/jest-bufferedconsole.ts @@ -24,7 +24,7 @@ export default class TestEnvironment extends NodeEnvironment implements JestEnvi // We need to set the event handler by assignment in the constructor, // because if we declare it as an async member TypeScript's type derivation // doesn't work properly. - (this as JestEnvironment).handleTestEvent = (async (event, _state) => { + (this as JestEnvironment).handleTestEvent = (async (event) => { if (event.name === 'test_done' && event.test.errors.length > 0 && this.log.length > 0) { this.stopCapture(); diff --git a/packages/@aws-cdk/tmp-toolkit-helpers/test/api/diff/diff.test.ts b/packages/@aws-cdk/tmp-toolkit-helpers/test/api/diff/diff.test.ts index 23e9db33d..bfe0bcd66 100644 --- a/packages/@aws-cdk/tmp-toolkit-helpers/test/api/diff/diff.test.ts +++ b/packages/@aws-cdk/tmp-toolkit-helpers/test/api/diff/diff.test.ts @@ -62,7 +62,6 @@ describe('formatStackDiff', () => { templateInfo: { oldTemplate: mockNewTemplate.template, newTemplate: mockNewTemplate, - stackName: 'test-stack', }, }); const result = formatter.formatStackDiff(); @@ -84,7 +83,6 @@ describe('formatStackDiff', () => { templateInfo: { oldTemplate: {}, newTemplate: mockNewTemplate, - stackName: 'test-stack', }, }); const result = formatter.formatStackDiff(); @@ -107,7 +105,6 @@ describe('formatStackDiff', () => { templateInfo: { oldTemplate: {}, newTemplate: mockNewTemplate, - stackName: 'test-stack', isImport: true, }, }); @@ -149,7 +146,6 @@ describe('formatStackDiff', () => { templateInfo: { oldTemplate: {}, newTemplate: mockNewTemplate, - stackName: 'test-stack', nestedStacks, }, }); @@ -225,7 +221,6 @@ describe('formatSecurityDiff', () => { templateInfo: { oldTemplate: mockNewTemplate.template, newTemplate: mockNewTemplate, - stackName: 'test-stack', }, }); const result = formatter.formatSecurityDiff({ @@ -244,7 +239,6 @@ describe('formatSecurityDiff', () => { templateInfo: { oldTemplate: {}, newTemplate: mockNewTemplate, - stackName: 'test-stack', }, }); const result = formatter.formatSecurityDiff({ @@ -279,7 +273,6 @@ describe('formatSecurityDiff', () => { templateInfo: { oldTemplate: {}, newTemplate: mockNewTemplate, - stackName: 'test-stack', }, }); const result = formatter.formatSecurityDiff({ @@ -317,7 +310,6 @@ describe('formatSecurityDiff', () => { templateInfo: { oldTemplate: {}, newTemplate: mockNewTemplate, - stackName: 'test-stack', }, }); const result = formatter.formatSecurityDiff({ diff --git a/packages/@aws-cdk/tmp-toolkit-helpers/test/tsconfig.json b/packages/@aws-cdk/tmp-toolkit-helpers/test/tsconfig.json new file mode 100644 index 000000000..fab093523 --- /dev/null +++ b/packages/@aws-cdk/tmp-toolkit-helpers/test/tsconfig.json @@ -0,0 +1,16 @@ +{ + "extends": "../tsconfig.dev.json", + "compilerOptions": { + "rootDir": "..", + "noEmit": true + }, + "references": [ + { + "path": "../../cloudformation-diff" + }, + { + "path": "../../../cdk-assets" + } + ], + "//": "~~ Generated by projen. To modify, edit .projenrc.js and run \"npx projen\"." +} diff --git a/packages/@aws-cdk/toolkit-lib/.gitattributes b/packages/@aws-cdk/toolkit-lib/.gitattributes index 1a8feca5e..b154bcbe3 100644 --- a/packages/@aws-cdk/toolkit-lib/.gitattributes +++ b/packages/@aws-cdk/toolkit-lib/.gitattributes @@ -16,6 +16,7 @@ /jest.config.json linguist-generated /LICENSE linguist-generated /package.json linguist-generated +/test/tsconfig.json linguist-generated /tsconfig.dev.json linguist-generated /tsconfig.json linguist-generated /yarn.lock linguist-generated \ No newline at end of file diff --git a/packages/@aws-cdk/toolkit-lib/.gitignore b/packages/@aws-cdk/toolkit-lib/.gitignore index 65b677450..cc61a9bb2 100644 --- a/packages/@aws-cdk/toolkit-lib/.gitignore +++ b/packages/@aws-cdk/toolkit-lib/.gitignore @@ -45,6 +45,7 @@ jspm_packages/ /dist/changelog.md /dist/version.txt !/.eslintrc.js +!/test/tsconfig.json !/api-extractor.json db.json.gz .init-version.json diff --git a/packages/@aws-cdk/toolkit-lib/.projen/files.json b/packages/@aws-cdk/toolkit-lib/.projen/files.json index fb4daac5e..fe16b7122 100644 --- a/packages/@aws-cdk/toolkit-lib/.projen/files.json +++ b/packages/@aws-cdk/toolkit-lib/.projen/files.json @@ -13,6 +13,7 @@ "api-extractor.json", "jest.config.json", "LICENSE", + "test/tsconfig.json", "tsconfig.dev.json", "tsconfig.json" ], diff --git a/packages/@aws-cdk/toolkit-lib/.projen/tasks.json b/packages/@aws-cdk/toolkit-lib/.projen/tasks.json index fedc81c11..9f65d744b 100644 --- a/packages/@aws-cdk/toolkit-lib/.projen/tasks.json +++ b/packages/@aws-cdk/toolkit-lib/.projen/tasks.json @@ -79,6 +79,9 @@ { "exec": "tsc --build", "receiveArgs": true + }, + { + "exec": "tsc --build test" } ] }, diff --git a/packages/@aws-cdk/toolkit-lib/test/api/cloud-assembly/source-builder.test.ts b/packages/@aws-cdk/toolkit-lib/test/api/cloud-assembly/source-builder.test.ts index 14472fd3c..e54dac9fd 100644 --- a/packages/@aws-cdk/toolkit-lib/test/api/cloud-assembly/source-builder.test.ts +++ b/packages/@aws-cdk/toolkit-lib/test/api/cloud-assembly/source-builder.test.ts @@ -82,7 +82,7 @@ describe('fromAssemblyBuilder', () => { // GIVEN const cx = await toolkit.fromAssemblyBuilder(async (props) => { lock = new RWLock(props.outdir!); - if (!await lock._currentWriter()) { + if (!await (lock as any)._currentWriter()) { throw new Error('Expected the directory to be locked during synth'); } throw new Error('a wild error appeared'); @@ -92,8 +92,8 @@ describe('fromAssemblyBuilder', () => { await expect(cx.produce()).rejects.toThrow(/wild error/); // THEN: Don't expect either a read or write lock on the directory afterwards - expect(await lock!._currentWriter()).toBeUndefined(); - expect(await lock!._currentReaders()).toEqual([]); + expect(await (lock! as any)._currentWriter()).toBeUndefined(); + expect(await (lock! as any)._currentReaders()).toEqual([]); }); }); @@ -162,8 +162,8 @@ describe('fromCdkApp', () => { await expect(cx.produce()).rejects.toThrow(/error 1/); // THEN: Don't expect either a read or write lock on the directory afterwards - expect(await lock!._currentWriter()).toBeUndefined(); - expect(await lock!._currentReaders()).toEqual([]); + expect(await (lock! as any)._currentWriter()).toBeUndefined(); + expect(await (lock! as any)._currentReaders()).toEqual([]); }); }); diff --git a/packages/@aws-cdk/toolkit-lib/test/tsconfig.json b/packages/@aws-cdk/toolkit-lib/test/tsconfig.json new file mode 100644 index 000000000..bf74a099c --- /dev/null +++ b/packages/@aws-cdk/toolkit-lib/test/tsconfig.json @@ -0,0 +1,22 @@ +{ + "extends": "../tsconfig.dev.json", + "compilerOptions": { + "rootDir": "..", + "noEmit": true + }, + "references": [ + { + "path": "../../cloud-assembly-schema" + }, + { + "path": "../../cloudformation-diff" + }, + { + "path": "../../../cdk-assets" + }, + { + "path": "../../tmp-toolkit-helpers" + } + ], + "//": "~~ Generated by projen. To modify, edit .projenrc.js and run \"npx projen\"." +} diff --git a/packages/aws-cdk/.gitattributes b/packages/aws-cdk/.gitattributes index c1b26c9d0..f8b7d2522 100644 --- a/packages/aws-cdk/.gitattributes +++ b/packages/aws-cdk/.gitattributes @@ -15,6 +15,7 @@ /jest.config.json linguist-generated /LICENSE linguist-generated /package.json linguist-generated +/test/tsconfig.json linguist-generated /tsconfig.dev.json linguist-generated /tsconfig.json linguist-generated /yarn.lock linguist-generated \ No newline at end of file diff --git a/packages/aws-cdk/.gitignore b/packages/aws-cdk/.gitignore index 53b4f9b53..f63afe223 100644 --- a/packages/aws-cdk/.gitignore +++ b/packages/aws-cdk/.gitignore @@ -44,6 +44,7 @@ jspm_packages/ /dist/changelog.md /dist/version.txt !/.eslintrc.js +!/test/tsconfig.json db.json.gz .init-version.json index_bg.wasm diff --git a/packages/aws-cdk/.projen/files.json b/packages/aws-cdk/.projen/files.json index 493bbd87e..9b2047f9d 100644 --- a/packages/aws-cdk/.projen/files.json +++ b/packages/aws-cdk/.projen/files.json @@ -12,6 +12,7 @@ ".projen/tasks.json", "jest.config.json", "LICENSE", + "test/tsconfig.json", "tsconfig.dev.json", "tsconfig.json" ], diff --git a/packages/aws-cdk/.projen/tasks.json b/packages/aws-cdk/.projen/tasks.json index daa8cae78..ded242da9 100644 --- a/packages/aws-cdk/.projen/tasks.json +++ b/packages/aws-cdk/.projen/tasks.json @@ -73,6 +73,9 @@ { "exec": "tsc --build", "receiveArgs": true + }, + { + "exec": "tsc --build test" } ] }, diff --git a/packages/aws-cdk/test/_helpers/mock-sdk.ts b/packages/aws-cdk/test/_helpers/mock-sdk.ts index e08c25292..976442eb3 100644 --- a/packages/aws-cdk/test/_helpers/mock-sdk.ts +++ b/packages/aws-cdk/test/_helpers/mock-sdk.ts @@ -156,7 +156,7 @@ export class MockSdkProvider extends SdkProvider { public defaultAccount(): Promise { const accountId = this.defaultAccounts.length === 0 ? '123456789012' - : this.defaultAccounts.shift(); + : this.defaultAccounts.shift()!; return Promise.resolve({ accountId, partition: 'aws' }); } } diff --git a/packages/aws-cdk/test/api/aws-auth/sdk-logger.test.ts b/packages/aws-cdk/test/api/aws-auth/sdk-logger.test.ts index 0ab033baf..c3593bc07 100644 --- a/packages/aws-cdk/test/api/aws-auth/sdk-logger.test.ts +++ b/packages/aws-cdk/test/api/aws-auth/sdk-logger.test.ts @@ -5,7 +5,7 @@ describe(SdkToCliLogger, () => { notify: jest.fn(), requestResponse: jest.fn(), }; - const logger = new SdkToCliLogger(ioHost); + const logger = new SdkToCliLogger(ioHost as any); beforeEach(() => { ioHost.notify.mockReset(); diff --git a/packages/aws-cdk/test/api/aws-auth/sdk-provider.test.ts b/packages/aws-cdk/test/api/aws-auth/sdk-provider.test.ts index daa6b11e8..53aa49940 100644 --- a/packages/aws-cdk/test/api/aws-auth/sdk-provider.test.ts +++ b/packages/aws-cdk/test/api/aws-auth/sdk-provider.test.ts @@ -24,7 +24,7 @@ import { AwsCliCompatible } from '../../../lib/api/aws-auth'; import { defaultCliUserAgent } from '../../../lib/api/aws-auth'; import { PluginHost } from '../../../lib/api/plugin'; import { Mode } from '../../../lib/api/plugin'; -import { instanceMockFrom, withMocked } from '../../_helpers/as-mock'; +import { withMocked } from '../../_helpers/as-mock'; import { undoAllSdkMocks } from '../../_helpers/mock-sdk'; import { TestIoHost } from '../../_helpers/io-host'; diff --git a/packages/aws-cdk/test/api/cloudformation/evaluate-cloudformation-template.test.ts b/packages/aws-cdk/test/api/cloudformation/evaluate-cloudformation-template.test.ts index 59586d8d4..3496c99f1 100644 --- a/packages/aws-cdk/test/api/cloudformation/evaluate-cloudformation-template.test.ts +++ b/packages/aws-cdk/test/api/cloudformation/evaluate-cloudformation-template.test.ts @@ -17,6 +17,7 @@ const createEvaluateCloudFormationTemplate = (template: Template) => region: 'ap-south-east-2', partition: 'aws', sdk, + stackArtifact: {} as any, }); describe('evaluateCfnExpression', () => { diff --git a/packages/aws-cdk/test/api/hotswap/appsync-mapping-templates-hotswap-deployments.test.ts b/packages/aws-cdk/test/api/hotswap/appsync-mapping-templates-hotswap-deployments.test.ts index 280f52330..e74f1b776 100644 --- a/packages/aws-cdk/test/api/hotswap/appsync-mapping-templates-hotswap-deployments.test.ts +++ b/packages/aws-cdk/test/api/hotswap/appsync-mapping-templates-hotswap-deployments.test.ts @@ -135,7 +135,7 @@ describe.each([HotswapMode.FALL_BACK, HotswapMode.HOTSWAP_ONLY])('%p mode', (hot // GIVEN const body = getBodyStream('template defined in s3'); mockS3Client.on(GetObjectCommand).resolves({ - Body: body, + Body: body as any, }); setup.setCurrentCfnStackTemplate({ Resources: { @@ -212,7 +212,7 @@ describe.each([HotswapMode.FALL_BACK, HotswapMode.HOTSWAP_ONLY])('%p mode', (hot // GIVEN const body = getBodyStream('code defined in s3'); mockS3Client.on(GetObjectCommand).resolves({ - Body: body, + Body: body as any, }); setup.setCurrentCfnStackTemplate({ Resources: { @@ -731,7 +731,7 @@ describe.each([HotswapMode.FALL_BACK, HotswapMode.HOTSWAP_ONLY])('%p mode', (hot async () => { // GIVEN mockS3Client.on(GetObjectCommand).resolves({ - Body: getBodyStream('template defined in s3'), + Body: getBodyStream('template defined in s3') as any, }); mockAppSyncClient .on(ListFunctionsCommand) @@ -1269,7 +1269,7 @@ describe.each([HotswapMode.FALL_BACK, HotswapMode.HOTSWAP_ONLY])('%p mode', (hot async () => { // GIVEN mockS3Client.on(GetObjectCommand).resolves({ - Body: getBodyStream('schema defined in s3'), + Body: getBodyStream('schema defined in s3') as any, }); setup.setCurrentCfnStackTemplate({ Resources: { diff --git a/packages/aws-cdk/test/api/plugin/plugin-host.test.ts b/packages/aws-cdk/test/api/plugin/plugin-host.test.ts index 6e990606e..90c4d2b51 100644 --- a/packages/aws-cdk/test/api/plugin/plugin-host.test.ts +++ b/packages/aws-cdk/test/api/plugin/plugin-host.test.ts @@ -93,7 +93,7 @@ test('plugin that registers an invalid Context Provider throws', () => { try { host.load(THE_PLUGIN); expect(true).toBe(false); // should not happen - } catch(e) { + } catch(e: any) { expect(e).toHaveProperty('cause'); expect(e.cause?.message).toMatch(/does not look like a ContextProviderPlugin/); } diff --git a/packages/aws-cdk/test/api/work-graph/work-graph-builder.test.ts b/packages/aws-cdk/test/api/work-graph/work-graph-builder.test.ts index 05c7b11c3..a9651446e 100644 --- a/packages/aws-cdk/test/api/work-graph/work-graph-builder.test.ts +++ b/packages/aws-cdk/test/api/work-graph/work-graph-builder.test.ts @@ -7,13 +7,11 @@ import { CloudAssemblyBuilder } from '@aws-cdk/cx-api'; import { expect } from '@jest/globals'; import { WorkGraph, WorkGraphBuilder } from '../../../lib/api/work-graph'; import type { AssetBuildNode, AssetPublishNode, StackNode, WorkNode } from '../../../lib/api/work-graph'; -import { CliIoHost, IoMessaging } from '../../../lib/cli/io-host'; +import { CliIoHost } from '../../../lib/cli/io-host'; +import { IoHelper } from '../../../lib/api-private'; let rootBuilder: CloudAssemblyBuilder; -let mockMsg: IoMessaging = { - ioHost: CliIoHost.instance(), - action: 'deploy' -}; +let mockMsg = IoHelper.fromIoHost(CliIoHost.instance(), 'deploy'); beforeEach(() => { rootBuilder = new CloudAssemblyBuilder(); diff --git a/packages/aws-cdk/test/cli/cdk-toolkit.test.ts b/packages/aws-cdk/test/cli/cdk-toolkit.test.ts index e92e7504f..024900cd6 100644 --- a/packages/aws-cdk/test/cli/cdk-toolkit.test.ts +++ b/packages/aws-cdk/test/cli/cdk-toolkit.test.ts @@ -99,6 +99,7 @@ import { import { asIoHelper } from '../../../@aws-cdk/tmp-toolkit-helpers/src/api/io/private'; import { StackActivityProgress } from '../../lib/commands/deploy'; import { Template } from '../../../@aws-cdk/tmp-toolkit-helpers/src/api'; +import { DestroyStackResult } from '@aws-cdk/tmp-toolkit-helpers/src/api/deployments/deploy-stack'; markTesting(); @@ -682,11 +683,11 @@ describe('deploy', () => { ], }); }); - + test('lookup role is used', async () => { // GIVEN mockSSMClient.on(GetParameterCommand).resolves({ Parameter: { Value: '6' } }); - + const cdkToolkit = new CdkToolkit({ cloudExecutable: mockCloudExecutable, configuration: mockCloudExecutable.configuration, @@ -696,13 +697,13 @@ describe('deploy', () => { ioHelper, }), }); - + // WHEN await cdkToolkit.deploy({ selector: { patterns: ['Test-Stack-C'] }, hotswap: HotswapMode.FULL_DEPLOYMENT, }); - + // THEN expect(mockSSMClient).toHaveReceivedCommandWith(GetParameterCommand, { Name: '/bootstrap/parameter', @@ -722,11 +723,11 @@ describe('deploy', () => { }, ); }); - + test('fallback to deploy role if bootstrap stack version is not valid', async () => { // GIVEN mockSSMClient.on(GetParameterCommand).resolves({ Parameter: { Value: '1' } }); - + const cdkToolkit = new CdkToolkit({ cloudExecutable: mockCloudExecutable, configuration: mockCloudExecutable.configuration, @@ -736,17 +737,17 @@ describe('deploy', () => { ioHelper, }), }); - + // WHEN await cdkToolkit.deploy({ selector: { patterns: ['Test-Stack-C'] }, hotswap: HotswapMode.FULL_DEPLOYMENT, }); - + // THEN expect(flatten(stderrMock.mock.calls)).toEqual( expect.arrayContaining([ - + expect.stringContaining( "Bootstrap stack version '5' is required, found version '1'. To get rid of this error, please upgrade to bootstrap version >= 5", ), @@ -783,7 +784,7 @@ describe('deploy', () => { }, ); }); - + test('fallback to deploy role if bootstrap version parameter not found', async () => { // GIVEN mockSSMClient.on(GetParameterCommand).callsFake(() => { @@ -791,7 +792,7 @@ describe('deploy', () => { e.code = e.name = 'ParameterNotFound'; throw e; }); - + const cdkToolkit = new CdkToolkit({ cloudExecutable: mockCloudExecutable, configuration: mockCloudExecutable.configuration, @@ -801,13 +802,13 @@ describe('deploy', () => { ioHelper, }), }); - + // WHEN await cdkToolkit.deploy({ selector: { patterns: ['Test-Stack-C'] }, hotswap: HotswapMode.FULL_DEPLOYMENT, }); - + // THEN expect(flatten(stderrMock.mock.calls)).toEqual( expect.arrayContaining([expect.stringMatching(/SSM parameter.*not found./)]), @@ -840,14 +841,14 @@ describe('deploy', () => { }, ); }); - + test('fallback to deploy role if forEnvironment throws', async () => { // GIVEN // throw error first for the 'prepareSdkWithLookupRoleFor' call and succeed for the rest mockForEnvironment = jest.spyOn(sdkProvider, 'forEnvironment').mockImplementationOnce(() => { throw new Error('TheErrorThatGetsThrown'); }); - + const cdkToolkit = new CdkToolkit({ cloudExecutable: mockCloudExecutable, configuration: mockCloudExecutable.configuration, @@ -857,13 +858,13 @@ describe('deploy', () => { ioHelper, }), }); - + // WHEN await cdkToolkit.deploy({ selector: { patterns: ['Test-Stack-C'] }, hotswap: HotswapMode.FULL_DEPLOYMENT, }); - + // THEN expect(mockSSMClient).not.toHaveReceivedAnyCommand(); expect(flatten(stderrMock.mock.calls)).toEqual( @@ -897,7 +898,7 @@ describe('deploy', () => { }, ); }); - + test('dont lookup bootstrap version parameter if default credentials are used', async () => { // GIVEN mockForEnvironment = jest.fn().mockImplementation(() => { @@ -913,13 +914,13 @@ describe('deploy', () => { ioHelper, }), }); - + // WHEN await cdkToolkit.deploy({ selector: { patterns: ['Test-Stack-C'] }, hotswap: HotswapMode.FULL_DEPLOYMENT, }); - + // THEN expect(flatten(stderrMock.mock.calls)).toEqual( expect.arrayContaining([ @@ -954,7 +955,7 @@ describe('deploy', () => { }, ); }); - + test('do not print warnings if lookup role not provided in stack artifact', async () => { // GIVEN const cdkToolkit = new CdkToolkit({ @@ -966,13 +967,13 @@ describe('deploy', () => { ioHelper, }), }); - + // WHEN await cdkToolkit.deploy({ selector: { patterns: ['Test-Stack-A'] }, hotswap: HotswapMode.FULL_DEPLOYMENT, }); - + // THEN expect(flatten(stderrMock.mock.calls)).not.toEqual( expect.arrayContaining([ @@ -1563,6 +1564,7 @@ describe('rollback', () => { const mockedRollback = jest.spyOn(Deployments.prototype, 'rollbackStack').mockResolvedValue({ success: true, + stackArn: 'arn', }); const toolkit = new CdkToolkit({ @@ -1602,7 +1604,7 @@ describe('rollback', () => { }); // Rollback might be called -- just don't do anything. - const mockRollbackStack = jest.spyOn(deployments, 'rollbackStack').mockResolvedValue({}); + const mockRollbackStack = jest.spyOn(deployments, 'rollbackStack').mockResolvedValue({ success: true, stackArn: 'arn' }); const mockedDeployStack = jest .spyOn(deployments, 'deployStack') @@ -1828,12 +1830,13 @@ class FakeCloudFormation extends Deployments { public rollbackStack(_options: RollbackStackOptions): Promise { return Promise.resolve({ success: true, - }); + stackArn: 'arn', + } satisfies RollbackStackResult); } - public destroyStack(options: DestroyStackOptions): Promise { + public destroyStack(options: DestroyStackOptions): Promise { expect(options.stack).toBeDefined(); - return Promise.resolve(); + return Promise.resolve({ stackArn: 'arn' }); } public readCurrentTemplate(stack: cxapi.CloudFormationStackArtifact): Promise