pact-js icon indicating copy to clipboard operation
pact-js copied to clipboard

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

Open danhlee329 opened this issue 5 years ago • 15 comments

Software versions

  • OS: e.g. Mac OSX 10.15.4
  • Consumer Pact library: pact-go v1.2.0
  • Provider Pact library: @pact-foundation/pact v9.8.2
  • Node Version: v12.14.1

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!

danhlee329 avatar May 03 '20 21:05 danhlee329

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?

mefellows avatar May 03 '20 23:05 mefellows

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?

danhlee329 avatar May 04 '20 00:05 danhlee329

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?

mefellows avatar May 15 '20 06:05 mefellows

I will provide a working example later today, thanks!

danhlee329 avatar May 18 '20 23:05 danhlee329

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))
  })
})

danhlee329 avatar May 22 '20 05:05 danhlee329

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

doktor500 avatar May 23 '20 16:05 doktor500

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();}) }) })

wellington-aquino-dev avatar Nov 10 '20 03:11 wellington-aquino-dev

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

leonardonelson91 avatar Apr 30 '21 13:04 leonardonelson91

--force-exit or --forceExit?

gustawdaniel avatar Jun 24 '21 01:06 gustawdaniel

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?

TimothyJones avatar Jun 24 '21 01:06 TimothyJones