pact-foundation / pact-js

JS version of Pact. Pact is a contract testing framework for HTTP APIs and non-HTTP asynchronous messaging systems.
https://pact.io
Other
1.64k stars 349 forks source link

Jest has detected the following 1 open handle potentially keeping Jest from exiting: #450

Open danhlee329 opened 4 years ago

danhlee329 commented 4 years ago

Software versions

I've setup both consumer, provider, and pact broker locally and successfully got the consumer contract verified via the provider test (all tests pass). However, in the provider test, I am running into this Jest warning:

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  TCPSERVERWRAP

      93 |     }
      94 | 
    > 95 |     return new Verifier(opts).verifyProvider().then(output => {
         |                               ^
      96 |       console.log("Pact Verification Complete!")
      97 |       console.log(output)
      98 |     }).catch(e => console.log(e))

      at Verifier.startProxy (node_modules/@pact-foundation/src/dsl/verifier.ts:119:35)
      at Verifier.verifyProvider (node_modules/@pact-foundation/src/dsl/verifier.ts:74:25)
      at Object.<anonymous> (index.pact-test.js:95:31)

This chunk of code (except the .catch(e => console.log(e)) was taking from https://github.com/pact-foundation/pact-js/blob/3d2e7ddc2a06d92d2d8f5a246d014b92cd7331a2/examples/e2e/test/provider.spec.js#L96

How do I resolve this warning and is there documentation on this? Possible bug? Couldn't find anything on this warning in the Pact docs. Thanks!

mefellows commented 4 years ago

So, are you saying you've removed the catch statement from your block of code?

That could explain the issue.

On that line in the verifier, we start a proxy service to perform administration and other stuff to do the verification process. We handle the success and fail case by closing the server, and returning a Promise. If you're not handling the fail case, it's possible there is an open file handle.

But doing a cursory Google, there are tonnes of issues that suggest Jest can be overly eager and reports "potential" file handles as well as actual.

Can you confirm the file handle is indeed open?

danhlee329 commented 4 years ago

I should clarify that I've added the catch to the block in my test, which is not present in the e2e source above. My test runs the same with the warning with or without the catch after the verification. Is there another step required on my end or possible the issue is in the verification logic?

mefellows commented 4 years ago

I did a bit of Googling, i'd be interested to know if the common strategy of "added a wait" essentially solves it. That indicates that it's just Jest being overly aggressive. Are you able to provide a reproduceable example people could use to fix/address it?

danhlee329 commented 4 years ago

I will provide a working example later today, thanks!

danhlee329 commented 4 years ago

Here is the example I'm using.

NOTE: this was setup in a PoC, so it may be a bit rough:

const { Verifier } = require("@pact-foundation/pact")
const path = require("path")

// Verify that the provider meets all consumer expectations
describe("Pact Verification", () => {
  it("validates the expectations of the Apollo Federation Service", () => {
    // let token = "INVALID TOKEN"

    let opts = {
      provider: "testprovider",
      logLevel: "DEBUG",
      providerBaseUrl: "http://localhost:3999",

      requestFilter: (req, res, next) => {
        next()
      },

      stateHandlers: { },

      // Fetch pacts from broker
      pactBrokerUrl: "http://localhost:9292/",

      // Fetch from broker with given tags
      consumerVersionTag: ["master"],

      // Tag provider with given tags
      providerVersionTag: ["master"],

      // Enables "pending pacts" feature
      enablePending: true,

      publishVerificationResult: true,
      providerVersion: "1.0.0",
    }

    return new Verifier(opts).verifyProvider().then(output => {
      console.log("Pact Verification Complete!")
      console.log(output)
    }).catch(e => console.log(e))
  })
})
doktor500 commented 4 years ago

I am experiencing the same issue, I managed to work around it by running jest --force-exit ...

wellington-aquino-dev commented 4 years ago

you could resolve the promise by adding a reference to the function and calling it when is done,

describe("Pact Verification", () => { it("validates the expectations of the Apollo Federation Service", (done) => { return new Verifier(opts).verifyProvider().then(output => { console.log("Pact Verification Complete!") console.log(output) done(); }).catch(e => {console.log(e); done();}) }) })

nelsonthedev commented 3 years ago

I'm having the same problem, pact hangs the execution on verifying the pacts and jest does not exit

gustawdaniel commented 3 years ago

--force-exit or --forceExit?

TimothyJones commented 3 years ago

I don't think catching the promise result from the verifier is the safest approach - it will return a success promise to jest even in a failure.

If this is still happening, what happens if you run jest with --detectOpenHandles?