mirror of
https://github.com/logos-messaging/logos-delivery-js.git
synced 2026-03-10 03:33:41 +00:00
* use graceful timeout mechanism * set max timeout for all hooks * small fix * small fix * use MOCHA_HOOK_MAX_TIMEOUT as default timeoutDuration * force retry even when the hook fails * use custom hooks * fix global timeout problem * fix unwanted change * fix enr issue * force retry on before error as well
69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
import { Logger } from "@waku/utils";
|
|
import { Suite } from "mocha";
|
|
|
|
import { MOCHA_HOOK_MAX_TIMEOUT } from "../constants";
|
|
const log = new Logger("test:mocha-hook");
|
|
|
|
function withGracefulTimeout(
|
|
asyncOperation: () => Promise<void>,
|
|
doneCallback: (error?: unknown) => void,
|
|
timeoutDuration: number = MOCHA_HOOK_MAX_TIMEOUT
|
|
): void {
|
|
let operationCompleted = false;
|
|
|
|
const wrapperOperation: () => Promise<void> = async () => {
|
|
try {
|
|
await asyncOperation();
|
|
if (!operationCompleted) {
|
|
operationCompleted = true;
|
|
log.info("Mocha hook completed successfully.");
|
|
doneCallback();
|
|
}
|
|
} catch (error) {
|
|
if (!operationCompleted) {
|
|
operationCompleted = true;
|
|
log.error(
|
|
"Mocha hook failed. Proceeding to the test so it can retry.",
|
|
error
|
|
);
|
|
doneCallback();
|
|
}
|
|
}
|
|
};
|
|
|
|
void wrapperOperation();
|
|
setTimeout(() => {
|
|
if (!operationCompleted) {
|
|
log.info(
|
|
"Custom timeout reached. Proceeding to the test so it can retry."
|
|
);
|
|
operationCompleted = true;
|
|
doneCallback();
|
|
}
|
|
}, timeoutDuration);
|
|
}
|
|
|
|
export const beforeEachCustom = function (
|
|
suite: Suite,
|
|
cb: () => Promise<void>
|
|
): void {
|
|
const timeoutBefore = suite.timeout();
|
|
suite.timeout(MOCHA_HOOK_MAX_TIMEOUT);
|
|
suite.beforeEach((done) => {
|
|
withGracefulTimeout(cb, done);
|
|
});
|
|
suite.timeout(timeoutBefore); // restore timeout to the original value
|
|
};
|
|
|
|
export const afterEachCustom = function (
|
|
suite: Suite,
|
|
cb: () => Promise<void>
|
|
): void {
|
|
const timeoutBefore = suite.timeout();
|
|
suite.timeout(MOCHA_HOOK_MAX_TIMEOUT);
|
|
suite.afterEach((done) => {
|
|
withGracefulTimeout(cb, done);
|
|
});
|
|
suite.timeout(timeoutBefore); // restore timeout to the original value
|
|
};
|