Abdurahman Hijazi
06/22/2022, 4:53 PMexport class User {
constructor({id, name, age}) {
this.id = id;
this.name = name;
this.age = age;
}
}
API.js
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
export class API {
constructor(url) {
if (url === undefined || url === "") {
url = '<http://localhost:3000>';
}
if (url.endsWith("/")) {
url = url.substr(0, url.length - 1)
}
this.url = url
}
fetchUser = async url => {
fetch(url)
.then(res => res.json())
.then(res => console.log(res))
.catch(e => console.log("Error from response: ", e))
}
generateAuthToken() {
return "Bearer " + new Date().toISOString()
}
}
export default new API('<http://localhost:3000>');
test.pact.spec.js
import { Pact } from '@pact-foundation/pact';
import { API } from './API';
import { Matchers } from '@pact-foundation/pact';
import { User } from './User';
const { eachLike, like, regex } = Matchers;
const mockProvider = new Pact({
consumer: 'User-frontend',
provider: 'Nodejs-backend',
port: 1234
});
describe('API Pact test', () => {
beforeAll(() => mockProvider.setup());
afterEach(() => mockProvider.verify());
afterAll(() => mockProvider.finalize());
describe('retrieving a product', () => {
test('ID 10 exists', async () => {
// Arrange
const expectedProduct = { id: '12', name: 'Sally', age: '32'}
await mockProvider.addInteraction({
state: 'a user with ID 12 exists',
uponReceiving: 'a request to get a user',
withRequest: {
method: 'GET',
path: '/user/12',
headers: {
Authorization: like('Bearer 2019-01-14T11:34:18.045Z'),
},
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': regex({generate: 'application/json; charset=utf-8', matcher: 'application/json;?.*'}),
},
body: like(expectedProduct),
},
});
// Act
const api = new API(mockProvider.mockService.baseUrl);
const product = await api.fetchUser('10');
// Assert - did we get the expected response
expect(product).toStrictEqual(new Product(expectedProduct));
});
});
});
How would I run the test locally in my cmd? Any help is appreciatedTimothy Jones
06/22/2022, 5:05 PMTimothy Jones
06/22/2022, 5:05 PMAbdurahman Hijazi
06/22/2022, 5:06 PMTimothy Jones
06/22/2022, 5:06 PMtoStrictEqual
is Jest, so I reckon your test framework is jestTimothy Jones
06/22/2022, 5:06 PMconst fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
Abdurahman Hijazi
06/22/2022, 5:07 PMTimothy Jones
06/22/2022, 5:07 PMimport fetch from 'node-fetch';
Abdurahman Hijazi
06/22/2022, 5:08 PMAbdurahman Hijazi
06/22/2022, 5:08 PMTimothy Jones
06/22/2022, 5:10 PMTimothy Jones
06/22/2022, 5:13 PMTimothy Jones
06/22/2022, 5:15 PMAbdurahman Hijazi
06/22/2022, 5:16 PMAbdurahman Hijazi
06/22/2022, 5:16 PMTimothy Jones
06/22/2022, 5:17 PMjest-pact
which you don't need to use, but it substantially reduces the boilerplate setup in a pact testTimothy Jones
06/22/2022, 5:17 PMmocha-pact
which does the same thingAbdurahman Hijazi
06/22/2022, 5:18 PMAbdurahman Hijazi
06/22/2022, 5:19 PMTimothy Jones
06/22/2022, 5:19 PMscript
with a test
inside it. That will tell you what test runner you're usingTimothy Jones
06/22/2022, 5:19 PMTimothy Jones
06/22/2022, 5:19 PMTimothy Jones
06/22/2022, 5:20 PMscripts
not script
Abdurahman Hijazi
06/22/2022, 5:21 PMTimothy Jones
06/22/2022, 5:21 PMAbdurahman Hijazi
06/22/2022, 5:24 PMMatt (pactflow.io / pact-js / pact-go)
Abdurahman Hijazi
06/22/2022, 9:57 PM