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?

4

1 回答 1

0

实际上最简单的方法是等待端口被使用。

默认情况下, Karma Pact将在端口上启动 Mock 1234(您可以指定自己的)。端口启动后,服务正在运行,您可以继续。

例如,您可以使用诸如wait-for-host之类的东西来检测正在运行的模拟服务:

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
});
于 2017-05-02T23:12:16.993 回答