-2

I am using Pact.js first time for microservice testing. I tried to follow these following examples:

  1. https://github.com/lucasmajerowicz/pact-node-example

  2. https://github.com/pact-foundation/pact-js/tree/master/examples/e2e

  3. https://github.com/pact-foundation/pact-js/tree/master/examples/mocha

In example 3; I navigate into test folder and run mocha index.spec.js But it throws the following error:

[2017-02-27T21:37:52.426Z]  INFO: pact-node@4.8.0/38629 on Meliss-MacBook-Pro.local: Creating Pact with PID: 38630
    1) "before all" hook
    2) "after all" hook


  0 passing (2s)
  2 failing

  1) The Dog API "before all" hook:
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.


  2) The Dog API "after all" hook:
     Error: connect ECONNREFUSED 127.0.0.1:8989
      at Object.exports._errnoException (util.js:1022:11)
      at exports._exceptionWithHostPort (util.js:1045:20)
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)

It creates the log and pact folders as expected, but it neither creates a pact.json file in the pact folder nor logs files in the log folder.

What confuses me more is I get the exact same error in examples 1 and 2. This tells me that I have an issue on my side but I don't know what it is. Can someone help me to debug the issue?

melis
  • 1,145
  • 2
  • 13
  • 30
  • Can you please take a look at `./logs/mockserver-integration.log` and paste a gist of the contents? – Matthew Fellows Feb 28 '17 at 00:29
  • @MatthewFellows thanks for the response. Unfortunately this file is empty. – melis Feb 28 '17 at 01:30
  • No worries. Can you please try some of the tips discussed in https://github.com/pact-foundation/pact-node/issues/30? That issue is for the verification process, but a number of things including testing the Ruby process outside of node and checking ports are pertinent to this discussion. – Matthew Fellows Feb 28 '17 at 01:42
  • I tried to run it on a different port, killed all node processes and rerun, and made sure PID is not used by another processes. The result hasn't changed. Have I missed anything? – melis Feb 28 '17 at 02:00
  • Have you tried executing the Ruby process directly? e.g. `./node_modules/bin/pact-mock-service`? If that works, can you see if it opens a port for listening? – Matthew Fellows Feb 28 '17 at 02:08
  • Yes it does! `INFO WEBrick::HTTPServer#start: pid=40965 port=63782`. I changed the provider port to 63782 and ran mocha index.spec.js again. This time, before all hook passed but after all hook failed with `Pact::ConsumerContractWriterError`. – melis Feb 28 '17 at 13:28
  • I have checked the logs in the ruby process. I see this error: ` Error ocurred in mock service: Pact::ConsumerContractWriterError - Please indicate the directory to write the pact to by specifying the pact_dir field` and the error stack. I'm not sure why `pact_dir` field is not set or how/where to set it. – melis Feb 28 '17 at 14:05
  • It's because you wouldn't have passed it to that Ruby process when you started it by itself. But that's OK, we were just trying to establish that the Ruby process would run. It does, so now we look elsewhere for the source of the problem. – Matthew Fellows Feb 28 '17 at 20:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136900/discussion-between-melis-and-matthew-fellows). – melis Feb 28 '17 at 20:56
  • @melis Your edit falls short of the mark. Here is what the closure reason says: "must include the desired behavior, a *specific problem or error and the shortest code necessary* to reproduce **it in the question itself.**" The code must be **in the question**. Links to code are not sufficient. If you think you're dealing with too much code, then your question is too broad. – Louis Mar 02 '17 at 13:32
  • @Louis this is not a coding problem. I didn't write the code and I don't think the issue is about the code. I tried to emphasize this in my question; for all those different examples in the urls, tests failed with the same error; which made me think, the problem was a local problem maybe a configuration issue and not about the code base. Putting the source code and the test code looks irrelevant to me. And at the end, we found out that I needed to increase the timeout value for mocha which confirms my point about this question not being a 'coding' problem. – melis Mar 02 '17 at 14:29

1 Answers1

1

Can you please try the following?

  1. Increase the tests timeout (currently at 2s)
  2. set logLevel: 'DEBUG' in any pact({...}) declarations
  3. Re-run the tests

And provide us a gist of the output.

It seems the Ruby mock server is not starting or is taking longer than 2 seconds, and the node process is timing out waiting for it to come up.

If that doesn't work, please try manually starting the mock service ./node_modules/bin/pact-mock-service --port 1234 and when it comes up, run the command netstat -an | grep LISTEN | grep 1234 so that we can see what network it is binding to.

Matthew Fellows
  • 3,669
  • 1
  • 15
  • 18
  • 1
    I ran mocha with the timeout flag ` mocha index.spec.js --timeout 10000` and it works! I guess it just takes ruby process more than 2 seconds to start the mock server! Thank you so much @MatthewFellows. I'm sure I'll have more questions later on :) – melis Feb 28 '17 at 21:57
  • Awesome! I'll update the docs on pact-js to recommend increasing the timeout. – Matthew Fellows Feb 28 '17 at 22:02
  • @MatthewFellows is there no way to avoid having the delay of 2000ms or more, it slows down the time significantly - since each test needs to be encapsulated we restart the pact server on each test - which quickly slows down huge test packages? – Simon Dragsbæk Jan 05 '21 at 13:54
  • The new v3 (beta) branch should start up a lot faster, so you could give that a try – Matthew Fellows Jan 06 '21 at 22:48