diff --git a/packages/aws-cdk-lib/cx-api/lib/cloud-assembly.ts b/packages/aws-cdk-lib/cx-api/lib/cloud-assembly.ts index df073a89365cb..91ba63df564bc 100644 --- a/packages/aws-cdk-lib/cx-api/lib/cloud-assembly.ts +++ b/packages/aws-cdk-lib/cx-api/lib/cloud-assembly.ts @@ -33,53 +33,6 @@ export class CloudAssembly implements ICloudAssembly { return x !== null && typeof(x) === 'object' && CLOUD_ASSEMBLY_SYMBOL in x; } - /** - * Cleans up any temporary assembly directories that got created in this process - * - * If a Cloud Assembly is emitted to a temporary directory, its directory gets - * added to a list. This function iterates over that list and deletes each - * directory in it, to free up disk space. - * - * This function will normally be called automatically during Node process - * exit and so you don't need to call this. However, some test environments do - * not properly trigger Node's `exit` event. Notably: Jest does not trigger - * the `exit` event (). - * - * ## Cleaning up temporary directories in jest - * - * For Jest, you have to make sure this function is called at the end of the - * test suite instead: - * - * ```js - * import { CloudAssembly } from 'aws-cdk-lib/cx-api'; - * - * afterAll(CloudAssembly.cleanupTemporaryDirectories); - * ``` - * - * Alternatively, you can use the `setupFilesAfterEnv` feature and use a - * provided helper script to automatically inject the above into every - * test file, so you don't have to do it by hand. - * - * ``` - * $ npx jest --setupFilesAfterEnv aws-cdk-lib/testhelpers/jest-autoclean - * ``` - * - * Or put the following into `jest.config.js`: - * - * ```js - * module.exports = { - * // ... - * setupFilesAfterEnv: ['aws-cdk-lib/testhelpers/jest-cleanup'], - * }; - * ``` - */ - public static cleanupTemporaryDirectories() { - for (const dir of TEMPORARY_ASSEMBLY_DIRS) { - fs.rmSync(dir, { recursive: true, force: true }); - } - TEMPORARY_ASSEMBLY_DIRS.splice(0, TEMPORARY_ASSEMBLY_DIRS.length); - } - /** * The root directory of the cloud assembly. */ @@ -545,4 +498,8 @@ function ensureDirSync(dir: string) { // On process exit, delete all temporary assembly directories const TEMPORARY_ASSEMBLY_DIRS: string[] = []; -process.on('exit', () => CloudAssembly.cleanupTemporaryDirectories()); +process.on('exit', () => { + for (const dir of TEMPORARY_ASSEMBLY_DIRS) { + fs.rmSync(dir, { recursive: true, force: true }); + } +}); diff --git a/packages/aws-cdk-lib/package.json b/packages/aws-cdk-lib/package.json index 6f50420c7a4e8..6daa92094076e 100644 --- a/packages/aws-cdk-lib/package.json +++ b/packages/aws-cdk-lib/package.json @@ -530,7 +530,6 @@ "./pipelines/package.json": "./pipelines/package.json", "./recommended-feature-flags.json": "./recommended-feature-flags.json", "./region-info": "./region-info/index.js", - "./testhelpers/jest-autoclean": "./testhelpers/jest-autoclean.js", "./triggers": "./triggers/index.js" }, "publishConfig": { diff --git a/packages/aws-cdk-lib/testhelpers/jest-autoclean.ts b/packages/aws-cdk-lib/testhelpers/jest-autoclean.ts deleted file mode 100644 index 7e3d71108eed6..0000000000000 --- a/packages/aws-cdk-lib/testhelpers/jest-autoclean.ts +++ /dev/null @@ -1,15 +0,0 @@ -/** - * A file designed to be used with Jest's `--setupFilesAfterEnv` flag, to automatically clean temporary directories. - * - * This is an effective alternative to adding an `afterAll()` to every individual test file. - * - * Can be used as follows: - * - * ``` - * $ jest --setupFilesAfterEnv aws-cdk-lib/testhelpers/jest-autoclean - * ``` - */ -import { CloudAssembly } from '../cx-api'; - -// eslint-disable-next-line @typescript-eslint/unbound-method -afterAll(CloudAssembly.cleanupTemporaryDirectories); diff --git a/tools/@aws-cdk/cdk-build-tools/config/jest.config.js b/tools/@aws-cdk/cdk-build-tools/config/jest.config.js index 0c47026702afe..030aa99371bad 100644 --- a/tools/@aws-cdk/cdk-build-tools/config/jest.config.js +++ b/tools/@aws-cdk/cdk-build-tools/config/jest.config.js @@ -1,13 +1,3 @@ -const thisPackagesPackageJson = require(`${process.cwd()}/package.json`); -const setupFilesAfterEnv = []; -if ('aws-cdk-lib' in thisPackagesPackageJson.devDependencies ?? {}) { - // If we depend on aws-cdk-lib, use the provided autoclean hook - setupFilesAfterEnv.push('aws-cdk-lib/testhelpers/jest-autoclean'); -} else if (thisPackagesPackageJson.name === 'aws-cdk-lib') { - // If we *ARE* aws-cdk-lib, use the hook in a slightly different way - setupFilesAfterEnv.push('./testhelpers/jest-autoclean.ts'); -} - module.exports = { // The preset deals with preferring TS over JS moduleFileExtensions: [ @@ -41,6 +31,4 @@ module.exports = { ], coveragePathIgnorePatterns: ['\\.generated\\.[jt]s$', '/test/', '.warnings.jsii.js$', '/node_modules/'], reporters: ['default', ['jest-junit', { suiteName: 'jest tests', outputDirectory: 'coverage' }]], - - setupFilesAfterEnv, };