Prateek Madapurmath
08/15/2022, 1:17 AMconst provider = new PactV3({ consumer, provider, dir, port: 1234 })
describe('test 1', () => {
test('should pass', () => {
provider
.given('valid user session')
.uponReceiving('request')
.withRequest({
method: 'POST',
path: '/my-url',
headers: { custom_header },
body: reqBody
})
.willRespondWith({
status: 200,
headers: reponseHeaders,
body: like(exampleBody)
})
await provider.executeTest(async mockService => {
myApi.configureUrl(mockService.url)
const apiResp = await <http://myApi.post|myApi.post>({
headers: { custom_header },
data: reqBody,
})
})
})
})
however, the above test fails with The following request was expected but not received and then it displays the request that I have defined under withRequest
i’m not sure what setup i got wrongTimothy Jones
08/15/2022, 1:43 AMTimothy Jones
08/15/2022, 1:44 AMawait but no async, so it's not returning a promiseTimothy Jones
08/15/2022, 1:44 AMawait provider.executeTest with return provider.executeTestTimothy Jones
08/15/2022, 1:45 AMexpect call. See the examples in the documentation.Timothy Jones
08/15/2022, 1:48 AMawait for return, then probably your <http://myApi.post|myApi.post> is not sending the request you think it is.Prateek Madapurmath
08/15/2022, 2:00 AMTimothy Jones
08/15/2022, 2:00 AMPrateek Madapurmath
08/15/2022, 2:01 AMPrateek Madapurmath
08/15/2022, 2:01 AM<http://myApi.post|myApi.post> is indeed making a request to the configured urlPrateek Madapurmath
08/15/2022, 2:01 AMTimothy Jones
08/15/2022, 2:02 AMTimothy Jones
08/15/2022, 2:03 AMthe response that is returned however is not something i recognizeWhat is the response? Is it a 500? Pact will return 500 if it gets a request it wasn't expecting.
Prateek Madapurmath
08/15/2022, 2:03 AMTimothy Jones
08/15/2022, 2:04 AMPrateek Madapurmath
08/15/2022, 2:04 AM{
status: [Function: status],
ok: true,
get: [Function: mockConstructor] {
_isMockFunction: true,
getMockImplementation: [Function],
mock: [Getter/Setter],
mockClear: [Function],
mockReset: [Function],
mockRestore: [Function],
mockReturnValueOnce: [Function],
mockResolvedValueOnce: [Function],
mockRejectedValueOnce: [Function],
mockReturnValue: [Function],
mockResolvedValue: [Function],
mockRejectedValue: [Function],
mockImplementationOnce: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
mockName: [Function],
getMockName: [Function]
},
toError: [Function: mockConstructor] {
_isMockFunction: true,
getMockImplementation: [Function],
mock: [Getter/Setter],
mockClear: [Function],
mockReset: [Function],
mockRestore: [Function],
mockReturnValueOnce: [Function],
mockResolvedValueOnce: [Function],
mockRejectedValueOnce: [Function],
mockReturnValue: [Function],
mockResolvedValue: [Function],
mockRejectedValue: [Function],
mockImplementationOnce: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
mockName: [Function],
getMockName: [Function]
}
}Prateek Madapurmath
08/15/2022, 2:04 AMsuperagent to make requestsTimothy Jones
08/15/2022, 2:05 AMTimothy Jones
08/15/2022, 2:05 AMsuperagent. You want to send requests with the code that is under test.Timothy Jones
08/15/2022, 2:05 AMPrateek Madapurmath
08/15/2022, 2:06 AMsuperagentTimothy Jones
08/15/2022, 2:06 AMTimothy Jones
08/15/2022, 2:07 AMsupertest. I guess superagent is a regular http framework.Prateek Madapurmath
08/15/2022, 2:07 AMTimothy Jones
08/15/2022, 2:07 AMPrateek Madapurmath
08/15/2022, 2:07 AMTimothy Jones
08/15/2022, 2:08 AMTimothy Jones
08/15/2022, 2:08 AMPrateek Madapurmath
08/15/2022, 2:08 AMTimothy Jones
08/15/2022, 2:08 AMPrateek Madapurmath
08/15/2022, 2:09 AMTimothy Jones
08/15/2022, 2:09 AMjest.mock('superagent')Timothy Jones
08/15/2022, 2:10 AMi have also confirmed that myApi.post is indeed making a request to the configured urlI would guess that you confirmed that your code is being called, but not that it actually is making the request
Prateek Madapurmath
08/15/2022, 2:21 AMsuperagent was being mocked by jest automatically because we have a ___mocks___ directory where a custom implementation of superagent was defined.
just added jest.unmock('superagent') to the pact test file and looks like the request is being made as expectedTimothy Jones
08/15/2022, 2:33 AMmyApi instead - then you can use pact to ensure that the mock objects that you're using in your unit tests can actually be received by your API.Timothy Jones
08/15/2022, 2:34 AMstripMatchers to remove the matchers from the pact expectation and use the exact same object in your mock responses 🙌Timothy Jones
08/15/2022, 2:37 AMassert that
myApi.createUser({ name: "foo" })
calls with the expected request
the expected response is unmarshalled into
a returned user object like { ...whatever the user object is }
Mocks:
when myApi.createUser({ name: "foo" }), then return { ...whatever the user object is }Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Timothy Jones
08/15/2022, 2:45 AMexpect(responseObject).toMatchPactExpectations() which just does a stripMatchers, but I think that might cause more confusionPrateek Madapurmath
08/15/2022, 2:55 AMstripMatchers method sounds like a useful one that i am definitely going to use. didn’t know that existed and i just manually stripped them lol. thanks!Prateek Madapurmath
08/15/2022, 2:58 AMTimothy Jones
08/15/2022, 2:59 AMPrateek Madapurmath
08/15/2022, 3:00 AMPrateek Madapurmath
08/15/2022, 3:02 AMextractPayload recursively strip matchers?Timothy Jones
08/15/2022, 3:02 AM