Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 5 additions & 48 deletions packages/aws-cdk-lib/cx-api/lib/cloud-assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<https://github.com/jestjs/jest/issues/10927>).
*
* ## 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.
*/
Expand Down Expand Up @@ -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 });
}
});
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
15 changes: 0 additions & 15 deletions packages/aws-cdk-lib/testhelpers/jest-autoclean.ts

This file was deleted.

12 changes: 0 additions & 12 deletions tools/@aws-cdk/cdk-build-tools/config/jest.config.js
Original file line number Diff line number Diff line change
@@ -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: [
Expand Down Expand Up @@ -41,6 +31,4 @@ module.exports = {
],
coveragePathIgnorePatterns: ['\\.generated\\.[jt]s$', '<rootDir>/test/', '.warnings.jsii.js$', '/node_modules/'],
reporters: ['default', ['jest-junit', { suiteName: 'jest tests', outputDirectory: 'coverage' }]],

setupFilesAfterEnv,
};
Loading