Hi. I am getting the error 'Cannot log after tests...
# pact-js
t
Hi. I am getting the error 'Cannot log after tests are done. Did you forget to wait for something async in your test?' for the following provider test Cannot log after tests are done. Did you forget to wait for something async in your test?
import { Verifier } from "@pact-foundation/pact";
import { server } from "../providers/product.provider";
import { describe, beforeAll, beforeEach, test } from "@jest/globals";
describe("Pact Verification", () => {
// (1) Starting the Provider API
beforeAll( () => server.listen(8081));
afterAll( () => {
if (server) {
server.close();
}
});
it("validates the expectations of ProductService", async () => {
// (2) Telling Pact to use the contracts stored in PactFlow and where the Product API will be running
const opts: any = {
logLevel: "INFO",
providerBaseUrl: "<http://localhost:8081>",
providerVersion: "1.0.0-someprovidersha",
provider: "products-provider",
providerBranch: "main",
consumerVersionSelectors: [{ branch: "main" }],
pactBrokerUrl: process.env.PACT_BROKER_BASE_URL,
// pactUrls: [
`//
${process.env.PWD}/pacts/katacoda-consumer-katacoda-provider.json
,`
// ],
publishVerificationResult: true,
enablePending: true,
};
// (3) Running the Provider verification task
return new Verifier(opts).verifyProvider().then((output: any) => {
console.log("Pact Verification Complete!");
});
});
});
With the option --detectOpenHandles added in my package.json jest command I get ... TCPSERVERWRAP 9 | 10 | //beforeAllasync((done: any) => server.listen(8081, done)); > 11 | beforeAll( () => server.listen(8081)); | ^ 12 | 13 | afterAll( () => { 14 | if (server) { at Function.listen (node_modules/express/lib/application.js63524) at Object.listen (test/provider_pact_specs/product.provider.pact.spec.ts1127) ● TCPSERVERWRAP 37 | 38 | // (3) Running the Provider verification task > 39 | return new Verifier(opts).verifyProvider().then((output: any) => { | ^ 40 | console.log("Pact Verification Complete!"); 41 | }); 42 | }); But Im not really sure how to fix these. From stackoverflow I found that I can add the --forceExit option to jest cl command, but is there a better way of writing this code so that I don't need this? Thanks.
t
You are not using the lifecycle hooks correctly. Check the examples.
Copy code
afterAll( () => {
    if (server) {
      server.close();
    }
  });
^ Where did you get this from?
Oh, wait, apologies - I thought this was a consumer test
I think your server is staying alive after the end of the tests
Copy code
server.close();
^ Is this async?
Also, this isn't your problem, but you can remove the
async
from your
it
function, because you're returning a promise.