GitHub
10/04/2024, 4:25 PMafterEach, 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
• Install the sample project as a basis for this test
• Upgrade project to latest pact-js version (npm i -DE @pact-foundation/pact@latest)
• Modify provider.spec.js to configure the provider verifier as so:
const pact = new pact_1.Verifier({
pactUrls: ["./pacts/myconsumer-myprovider.json"],
providerBaseUrl: "http://127.0.0.1:8080",
afterEach: async () => {
return new Promise((resolve) => setTimeout(resolve, 2000))
},
});
• Run provider test (./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:
Jest did not exit one second after the test run has completed.
'This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
Note the timestamps either side of the executing 'afterEach' hook log indicate execution on this thread is no paused ...
2024-10-04T16:15:22.434564Z DEBUG ThreadId(02) verify_interaction{interaction="a request for dynamic keys"}: hyper_util::client::legacy::pool: reuse idle connection for ("http", 127.0.0.1:60185)
[17:15:22.434] DEBUG (53817): pact@13.1.3: incoming request: {"body":{},"headers":{"accept":"*/*","accept-encoding":"gzip, deflate","host":"127.0.0.1:60185"},"method":"GET","path":"/"}
[17:15:22.434] DEBUG (53817): pact@13.1.3: Proxying GET: /
[17:15:22.436] DEBUG (53817): pact@13.1.3: executing 'afterEach' hook
[17:15:22.438] DEBUG (53817): pact@13.1.3: outgoing response: {"body":"{\"foo\":\"1\"}","headers":{"x-powered-by":"Express","content-type":"application/json; charset=utf-8","content-length":"11","etag":"W/\"b-Pvi0Q5/yHZyhiaQpID3i4+RNmB4\"","date":"Fri, 04 Oct 2024 16:15:22 GMT","connection":"close"},"status":200}
Note that beforeEach does appear to support async operation. Here are the equivalent logs when replacing afterEach with beforeEach in the above snippet, which show a ~2 second gap between logs:
2024-10-04T16:23:18.834894Z DEBUG ThreadId(02) verify_interaction{interaction="a request for dynamic keys"}: hyper_util::client::legacy::connect::http: connected to 127.0.0.1:60460
[17:23:18.845] DEBUG (58216): pact@13.1.3: executing 'beforeEach' hook
[17:23:20.849] DEBUG (58216): pact@13.1.3: incoming request: {"body":{"action":"setup","params":{},"state":""},"headers":{"content-type":"application/json","accept":"*/*","accept-encoding":"gzip, deflate","host":"127.0.0.1:60460","content-length":"41"},"method":"POST","path":"/_pactSetup"}
pact-foundation/pact-jsGitHub
10/08/2024, 2:45 PMGitHub
10/08/2024, 2:45 PM