Pact-JS + Karma: Registering interaction with empty-body request
What I learned today — 9 April 2018
1 min readApr 9, 2018
beforeAll((done) => {
provider.addInteraction({
state: 'a user with username joe.soap is logged in',
uponReceiving: 'a request to log joe.soap out',
withRequest: {
method: 'POST',
path: '/auth/logout'
},
willRespondWith: {
status: 200,
body: {}
}
}).then(done, e => done.fail(e));
});
To register an expected interaction with the Pact mock server of a request with an empty body, don’t use a matcher, rather leave out the body attribute completely, as in the working code snippet above here.
The following two request definitions will both result in the interaction not being added to the mock server and a vague error message: “Script error.”
// Example 1
withRequest: {
method: 'POST',
path: '/auth/logout',
body: Pact.Matchers.somethingLike(null)
},// Example 2
withRequest: {
method: 'POST',
path: '/auth/logout',
body: Pact.Matchers.somethingLike({})
},