This DSL is in very early stages of development, please bear with us as we give it some polish. Please raise any problems you have in the github issues. Check out the Development Roadmap to see where we are headed.
This codebase provides a Javascript DSL for creating pacts. If you are new to Pact, please read the Pact README first.
This DSL relies on the Ruby pact-mock_service gem to provide the mock service for the Javascript tests. If you do not want to use Ruby in your project, please read about using a standalone Pact mock service here.
- Please see the wiki for documentation (eg. using CORS, using the DSL on Windows).
- Google users group: https://groups.google.com/forum/#!forum/pact-support
- Twitter: @pact_up
- If on Windows, please refer to the Installing pact-mock-service on Windows
-
After these binaries are available in the console, you can install the mock service by creating a Gemfile as shown below (this is the Ruby equivalent of package.json), then running
gem install bundler && bundle install
(the equivalent of npm install).
- Note: Windows users must run the install command after following Wiki instructions
source 'https://rubygems.org'
gem 'pact-mock_service', '~> 0.4.1'
-
Install and configure Karma with Jasmine
-
Create a
package.json
if you don't have one already - usenpm init
if you don't -
Install Karma using their installation instructions
This basically consists of running,
```
npm install karma karma-jasmine karma-chrome-launcher --save-dev
npm install -g karma-cli
```
- Initialise and configure Karma
Run `karma init`. Answer **jasmine** for *testing framework* and **no** for *use require.js*.
-
Add
pact-consumer-js-dsl
to your project by runningbower install pact-consumer-js-dsl --save-dev
. -
Tell Karma about
pact-consumer-js-dsl.js
inkarma.conf.js
. In thefiles: []
section add a new entry forbower_components/pact-consumer-js-dsl/dist/web/pact-consumer-js-dsl.js
. -
Allow tests to load resources from
pact
mock server. One way to do this is in thekarma.conf.js
, changebrowsers: ['Chrome'],
orbrowsers: ['PhantomJS'],
to,````javascript browsers: ['Chrome_without_security'], customLaunchers: { Chrome_without_security: { base: 'Chrome', flags: ['--disable-web-security'] } } or: browsers: ['PhantomJS_without_security'], customLaunchers: { PhantomJS_without_security: { base: 'PhantomJS', flags: ['--web-security=false'] } } ````
Note that running your tests across multiple browsers with one pact mock server will probably conflict with each other. You will need to either run them sequentially or start multiple pact mock servers. To run them sequentially make multiple calls to karma from the command line with the different browsers passed with the --browser
option.
-
Write a Jasmine unit test similar to the following,
describe("Client", function() { var client, helloProvider; beforeEach(function() { client = new ProviderClient('http://localhost:1234'); helloProvider = MockService.create({ consumer: 'Hello Consumer', provider: 'Hello Provider', port: 1234, done: function (error) { expect(error).toBe(null); } }); }); it("should say hello", function(done) { helloProvider .given("default server state") .uponReceiving("a request for hello") .withRequest("get", "/sayHello") .willRespondWith(200, { "Content-Type": "application/json" }, { reply: "Hello" }); helloProvider.run(done, function(runComplete) { expect(client.sayHello()).toEqual("Hello"); runComplete(); }); }); });
See the spec in the example directory for more examples of asynchronous callbacks, how to expect error responses, and how to use query params.
Make sure the source and test files are included by Karma in the
karma.conf.js
in the files array. -
Let's run that bad boy!
- Start the pact mock server with
bundle exec pact-mock-service -p 1234 -l log/pact.logs --pact-dir tmp/pacts
- Run
karma start
(in another terminal window) - Inspect the pact file that has been written to "hello_consumer-hello_provider.json"
- Start the pact mock server with
Have a look at the example folder. Ensure you have Google Chrome installed.
$ cd example/web
$ bundle install
$ npm install
$ npm test
This is only an example on how to use the pact-consumer-js-dsl within Node. This is not best practice, but is a good starting point without creating a lengthy example using Grunt or Gulp with Protractor or Karma.
-
Start pact-mock-service with
bundle exec pact-mock-service start -p 1234 -l tmp/pact.log --pact-dir tmp/pacts
-
Run nodejs command to setup pact consumer
node example/nodejs/setup.js
-
Run your tests here with whatever you want, like Protractor for e2e testing
-
Run nodejs command to verify interactions and write pact files
node example/nodejs/teardown.js
-
Stop the mock service with
bundle exec pact-mock-service stop -p 1234
Please read CONTRIBUTING.md