Check out the Test Repo I created: https://github.com/leongaban/pact-io-js-test
https://i.sstatic.net/JESkK.jpg
Anticipated Outcome
To generate a Pact file for my TotalPayout.test.pact.ts
script, run npm run pactTest
.
Findings
D, [#38238] DEBUG -- : {
"description": "a GET request with a user id",
"request": {
"method": "GET",
"path": "/frontoffice/api/liquidity-pool/get-total-payout",
"headers": {
"Accept": "application/json"
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
}
}
}
W, [#38238] WARN -- : Verifying - actual interactions do not match expected interactions.
Missing requests:
GET /frontoffice/api/liquidity-pool/get-total-payout
W, [#38238] WARN -- : Missing requests:
GET /frontoffice/api/liquidity-pool/get-total-payout
Behold, My Pact File
// @ts-ignore
import path from 'path';
// @ts-ignore
import { Pact } from '@pact-foundation/pact';
import { getTotalPayout } from './LiquidityPool';
// const port = 12345;
const endpoint = '/frontoffice/api/liquidity-pool/get-total-payout';
const EXPECTED_BODY = {
total_payout: 100.21,
};
const userId = 'foo';
describe('The API', () => {
// Copy this block once per interaction under test
describe('getUsersTotalPayout', () => {
beforeEach(() => {
const interaction = {
uponReceiving: 'a GET request with a user id',
withRequest: {
method: 'GET',
path: endpoint,
headers: {
Accept: 'application/json',
},
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json'
},
data: EXPECTED_BODY
},
};
// @ts-ignore
return provider.addInteraction(interaction);
});
// add expectations
it('Should call getUsersTotalPayout and return an object with the total_payout', done => {
getTotalPayout(userId)
.then((response: any) => {
console.log('response', response);
console.log('EXPECTED_BODY', EXPECTED_BODY);
expect(response).toEqual(EXPECTED_BODY);
})
.then(done);
});
});
});
Contained in this service file is the getTotalPayout
function:
This specific endpoint has not been created yet, but this Pact test is expected to function correctly.
// @TODO Note, this is the placeholder for LiquidityPool API endpoints
// @ts-ignore
import axios, * as others from 'axios';
const endpoint = '/frontoffice/api/liquidity-pool/';
export const getTotalPayout = async (userId: string) => {
const response = await axios.get(`${endpoint}get-total-payout`, { params: userId });
return response.data;
};
Additionally, here's my axios
mock located in src/__mocks__/axios.ts
// tslint:disable-next-line:no-empty
const mockNoop = () => new Promise(() => {});
export default {
get: jest.fn(() => Promise.resolve({ data: { total_payout: 100.21 }})),
default: mockNoop,
post: mockNoop,
put: mockNoop,
delete: mockNoop,
patch: mockNoop
};