0

We're using the karma-pact plugin to run our pact JS client tests, based on the example from https://github.com/pact-foundation/pact-js/blob/master/karma/mocha/client-spec.js .

In the example there's a timeout in the before(), I believe to ensure the mock service has started before running the tests (see comment "required for slower Travis CI builds").

I'm reluctant to set a fixed timeout in our tests as it'll either be too short or too long in different environments (e.g. CI vs local) and so I was looking for a way to check if the server has started.

I'd tried using the pact API https://github.com/pact-foundation/pact-node#check-if-a-mock-server-is-running , however this appears to start a new mock server which conflicts with the one started by the karma-pact plugin (an Error: kill ESRCH error is reported when trying to run pact.createServer().running from within a test).

Is there a way to determine if the mock server has started up e.g. by waiting for a URL to become available? Possibly there's a way to get a reference the mock server started by the karma-pact plugin in order to use the pact-node API?

1 Answers1

0

Actually the simplest way is to wait for the port to be in use.

Karma Pact by default will start the Mock on port 1234 (and you can specify your own). Once the port is up, the service is running and you can proceed.

For example, you could use something like wait-for-host to detect the running mock service:

var waitForPort = require('wait-for-port');

waitForPort('localhost', 1234, function(err) {
  if (err) throw new Error(err);
  // ... Mock Service is up - now we can run the tests
});
Matthew Fellows
  • 3,669
  • 1
  • 15
  • 18
  • Interesting idea, I'll give that a try. Am I right in thinking there's the (small) possibility that the [isPortAvailable function](https://github.com/pact-foundation/pact-js/blob/master/src/common/net.js) will be using the port while the mock server is trying to start up, and prevent the mock server from starting? – Alasdair Maclean May 03 '17 at 14:14
  • Actually that function is the exact opposite of what you want - quite right. It is actually used to determine if port is free before starting the server. Hopefully you get the idea though! I'll update the answer – Matthew Fellows May 03 '17 at 21:49
  • Right, it's the opposite we're looking to do. I do have a nagging feeling using this method might cause the mock server to fail to start (as the check opens up port 1234, albeit briefly). – Alasdair Maclean May 04 '17 at 08:51
  • Yes there is definitely a high chance of conflict, that's why you wouldn't use this method. I've updated my response to clarify the position. – Matthew Fellows May 04 '17 at 09:50
  • Unfortunately our tests run within PhantomJS which doesn't have the 'net' node package. Also tried the 'wait-for-url' npm package which also requires net. – Alasdair Maclean May 18 '17 at 13:49
  • We've written a small utility function to wait for the URL to become available (via ajax calls) and then to call provider.removeInteractions() - any other ideas would be welcome, of course – Alasdair Maclean May 18 '17 at 13:55
  • That sounds about right. Will be keen to see what you ended up with! – Matthew Fellows May 18 '17 at 21:25