Expected
Run npm run pactTest
which will create a Pact file for my TotalPayout.test.pact.ts
file.
Results
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
Here is 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);
});
});
});
Here is the service file that contains the getTotalPayout
function:
This endpoint doesn't exist yet, but this Pact test should still work is my understanding.
// @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;
};
Also my axios
mock 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
};
Aucun commentaire:
Enregistrer un commentaire