Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli): prevent test interference #32270

Merged
merged 10 commits into from
Nov 25, 2024
Prev Previous commit
Next Next commit
Run afterAll in reverse order
  • Loading branch information
rix0rrr committed Nov 25, 2024
commit 58b9c2f5fece1a5842db0fac0c8b6c7a154792b8
33 changes: 28 additions & 5 deletions packages/aws-cdk/test/jest-setup-after-env.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { isPromise } from 'util/types';

/**
* Global test setup for Jest tests
Expand Down Expand Up @@ -38,6 +39,8 @@ beforeAll(() => {
tmpDir = process.cwd(); // This will have resolved symlinks
});

const reverseAfterAll: Array<jest.ProvidesHookCallback> = [];

/**
* We need a cleanup here
*
Expand All @@ -51,12 +54,32 @@ beforeAll(() => {
* Jest will then try to write the `coverage` directory to the readonly directory,
* and fail. Chdir back to the original dir.
*
* Only if we are still in the tempdir, because if not then some other temporary
* directory cleanup block has already done the same and we shouldn't interfere
* with that.
* If the test file has an `afterAll()` hook it installed as well, we need to run
* it before our cleanup, otherwise the wrong thing will happen (by default,
* all `afterAll()`s run in call order, but they should be run in reverse).
*/
afterAll(() => {
afterAll(async () => {
for (const aft of reverseAfterAll.reverse()) {
await new Promise<void>((resolve, reject) => {
const response = aft(resolve as any);
if (isPromise(response)) {
response.then(() => { return resolve(); }, reject);
} else {
resolve();
}
});
}

// eslint-disable-next-line no-console
process.stderr.write(`${process.cwd()}, ${tmpDir}\n`);
if (process.cwd() === tmpDir) {
// eslint-disable-next-line no-console
process.stderr.write('chmod\n');
process.chdir(oldDir);
}
});
});

// Patch afterAll to make later-provided afterAll's run before us (in reverse order even).
afterAll = (after: jest.ProvidesHookCallback) => {
reverseAfterAll.push(after);
};
Loading