Jiayao Xu
04/19/2022, 8:52 AMMatt (pactflow.io / pact-js / pact-go)
Jiayao Xu
04/19/2022, 8:56 AMMatt (pactflow.io / pact-js / pact-go)
Jiayao Xu
04/19/2022, 9:03 AMexpect(response.status).to.equal(400)
That still passes đź‘€ even though if I log status that is 204
3. I am occasionally getting an error Error: Port 8080 is unavailable on address ...
why would that be?
ThanksMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Jiayao Xu
04/19/2022, 9:58 AMdescribe('sample test', () => {
before(() =>
provider.setup().then(() =>
provider.addInteraction({
state: 'GET success',
uponReceiving: 'GET',
withRequest: {
method: 'GET',
path: '/get',
headers: {
'Accept': 'application/json; v=1',
'Content-Type': 'application/json; v=1',
}
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json'
},
body: eachLike(expectedBody)
},
state: 'PATCH success',
uponReceiving: 'PATCH',
withRequest: {
method: 'PATCH',
path: '/patch',
headers: {
'Accept': 'application/json; v=1',
'Content-Type': 'application/json; v=1'
},
data: {
value: 'xyz'
}
},
willRespondWith: {
status: 204,
headers: {
'Content-Type': 'application/json'
}
}
})
)
);
it('should return the expected response body', () => {
axios
.request({
method: 'GET',
url: '<http://localhost:3000/get>',
headers: {
'Accept': 'application/json; v=1',
'Content-Type': 'application/json; v=1',
}
})
.then(response => {
expect(response.data).to.equal(expectedBody);
});
});
it('should return the expected response body', () => {
axios
.request({
method: 'PATCH',
url: '<http://localhost:3000/patch>',
headers: {
'Accept': 'application/json; v=1',
'Content-Type': 'application/json; v=1'
},
data: {
value: 'xyz'
}
})
.then(response => {
expect(response.status).to.deep.equal(204);
});
});
afterEach(() => provider.verify());
after(() => provider.finalize());
});
Jiayao Xu
04/19/2022, 9:58 AMMatt (pactflow.io / pact-js / pact-go)
it
blocks aren’t returning or awaiting promises - this will be the reason for the flakey testsMatt (pactflow.io / pact-js / pact-go)
expect(response.status).to.deep.equal(204)
is not causing your test to fail - the it
block has returned, so it can’t change the outcomeMatt (pactflow.io / pact-js / pact-go)
provider.addInteraction({
state: 'GET success',
uponReceiving: 'GET',
withRequest: {
method: 'GET',
path: '/get',
headers: {
'Accept': 'application/json; v=1',
'Content-Type': 'application/json; v=1',
}
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json'
},
body: eachLike(expectedBody)
},
state: 'PATCH success',
uponReceiving: 'PATCH',
withRequest: {
method: 'PATCH',
path: '/patch',
headers: {
'Accept': 'application/json; v=1',
'Content-Type': 'application/json; v=1'
},
data: {
value: 'xyz'
}
},
willRespondWith: {
status: 204,
headers: {
'Content-Type': 'application/json'
}
}
})
You’re passing a JSON object to addInteraction
. The duplicate keys are conflicting - only one of the withRequest
, state
etc. will actually win.Matt (pactflow.io / pact-js / pact-go)
addInteraction
per it
block, not line them all up in a single before
blockYousaf Nabi (pactflow.io)
path
but a different id
.
https://github.com/pactflow/example-consumer/blob/d4777a53d42f7688a7c5586c3296c08cdf1f08f4/src/api.pact.spec.js#L25
https://github.com/pactflow/example-consumer/blob/d4777a53d42f7688a7c5586c3296c08cdf1f08f4/src/api.pact.spec.js#L55
You can register multiple interactions per mock server, but each addInteraction
requires a single object, not the dual objects you have passed in.
If you use TypeScript you can get intelligent typing support.
Also assuming your example is just for illustration purposes, are the client under test, isn't your application code, but a client constructed during the test (to make the call to the mock provider directly)Jiayao Xu
04/19/2022, 12:07 PMit
block isn’t returning a promise. what do you mean? 👀Matt (pactflow.io / pact-js / pact-go)
it('should return the expected response body', () => {
// this next line is a promise, but you're not returning it or await-ing it. This means the it block immediately returns before the promise does it's work.
axios
.request({
method: 'GET',
url: '<http://localhost:3000/get>',
headers: {
'Accept': 'application/json; v=1',
'Content-Type': 'application/json; v=1',
}
})
.then(response => {
expect(response.data).to.equal(expectedBody);
});
});
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Jiayao Xu
04/19/2022, 12:31 PMJiayao Xu
04/19/2022, 12:35 PMMatt (pactflow.io / pact-js / pact-go)
the port unavailable error, I don’t think that is JS or promise relatedthis can happen if the tests aren’t shutting down properly or if
finalize()
isn’t called. You should be able to see if the server is hanging around (it’s a Ruby process so ps -ef | grep ruby
will usually show any hanging processes.Matt (pactflow.io / pact-js / pact-go)
provider.setup()
. Most of our examples use this formatMatt (pactflow.io / pact-js / pact-go)
I have the test working now, but the contract keeps overriding, is that expected?Each time the test suite is run, it should replace the contents of the contract, yes. What were you expecting?
Jiayao Xu
04/19/2022, 12:39 PMYousaf Nabi (pactflow.io)
Jiayao Xu
04/19/2022, 12:39 PMJiayao Xu
04/19/2022, 12:39 PMJiayao Xu
04/19/2022, 12:41 PMMatt (pactflow.io / pact-js / pact-go)
why is it recommended to use fixed port?I think you mean “not recommended”. The reason is, port conflicts with other tools on your (and your colleagues) machines. If you don’t specify a port, we’ll allocate a free one. If you always use a fixed port, it might conflict with another tool down the track
Matt (pactflow.io / pact-js / pact-go)
mock provider with multiple interactions, should it add both interactions rather than replacing with the latest one?It should add both
Matt (pactflow.io / pact-js / pact-go)
addInteraction
for each interaction you want to test (usually a 1:1 between addInteraction
and it
blocks)Jiayao Xu
04/19/2022, 12:44 PMYousaf Nabi (pactflow.io)
Jiayao Xu
04/19/2022, 2:12 PM