Closed lhokktyn closed 1 month ago
Related to https://github.com/pact-foundation/pact-js/issues/1068 which discusses the premature execution of afterEach
Thanks for the helpful repro @lhokktyn, I'd be open to a PR to fix this. The problem might be in https://github.com/pact-foundation/pact-js/blob/1a3815ef456dc7e608a02e3cc2b267cac8b1f8ca/src/dsl/verifier/proxy/hooks.ts, but would need further investigation.
I'm not a maintainer any more, but I'm pretty sure the problem is:
logger.trace("registered 'afterEach' hook");
next();
if (req.path !== stateSetupPath) {
logger.debug("executing 'afterEach' hook");
try {
await config.afterEach();
} catch (e) {
logger.error(`error executing 'afterEach' hook: ${e.message}`);
logger.debug(`Stack trace was: ${e.stack}`);
next(new Error(`error executing 'afterEach' hook: ${e.message}`));
}
should be:
logger.trace("registered 'afterEach' hook");
if (req.path !== stateSetupPath) {
logger.debug("executing 'afterEach' hook");
try {
await config.afterEach();
next();
} catch (e) {
logger.error(`error executing 'afterEach' hook: ${e.message}`);
logger.debug(`Stack trace was: ${e.stack}`);
next(new Error(`error executing 'afterEach' hook: ${e.message}`));
}
Also that "registered afterEach hook"
trace line is in the wrong place - should be outside the app.use
Software versions
Please provide at least OS and version of pact-js
Issue Checklist
Please confirm the following:
Expected behaviour
When defining an async function for
afterEach
, it should pause further execution of the verification tests until that Promise has resolved.We require async behaviour for clearing up state in
afterEach
as part of our verification tests.Actual behaviour
Test execution is not paused, and Promise is not handled cleanly.
Steps to reproduce
npm i -DE @pact-foundation/pact@latest
)provider.spec.js
to configure the provider verifier as so:./node_modules/.bin/jest provider.spec.js
)Given this
afterEach
hook, I would expect test execution to pause for ~2 seconds, but it doesn't and jest does not exit cleanly, giving the warning:Note the timestamps either side of the
executing 'afterEach' hook
log indicate execution on this thread is no paused ...Note that
beforeEach
does appear to support async operation. Here are the equivalent logs when replacingafterEach
withbeforeEach
in the above snippet, which show a ~2 second gap between logs: