Александр Чепрасов
09/07/2024, 6:10 PM1) GET /dogs
returns an HTTP 200 and a list of dogs:
TypeError: provider.executeTest is not a function
at Context.<anonymous> (file:///home/chepracio/Projects/tests/pact/test-from-doc/tests/server-1.test.js:52:21)
at process.processImmediate (node:internal/timers:478:21)
Code:
import {PactV4, MatchersV3, SpecificationVersion} from '@pact-foundation/pact';
import axios from "axios";
import path from "path";
// Create a 'pact' between the two applications in the integration we are testing
const provider = new PactV4({
log: path.resolve(process.cwd(), "logs", "pact.log"),
dir: "/home/chepracio/Projects/_utils/docker-composes/pact/pacts/",
consumer: 'MyConsumer',
provider: 'MyProvider',
spec: SpecificationVersion.SPECIFICATION_VERSION_V4, // Modify this as needed for your use case
});
// API Client that will fetch dogs from the Dog API
// This is the target of our Pact test
class DogService {
constructor(url) {
this.url = url
}
getMeDogs = (from) => {
return axios.request({
baseURL: this.url,
params: {from},
headers: {Accept: 'application/json'},
method: 'GET',
url: '/dogs',
});
};
}
const dogExample = {dog: 1};
const EXPECTED_BODY = MatchersV3.eachLike(dogExample);
let dogService;
describe('GET /dogs', () => {
it('returns an HTTP 200 and a list of dogs', () => {
provider
.addInteraction()
.given('I have a list of dogs')
.uponReceiving('a request for all dogs with the builder pattern')
.withRequest('GET', '/dogs', (builder) => {
builder.query({from: 'today'})
builder.headers({Accept: 'application/json'})
})
.willRespondWith(200, (builder) => {
builder.headers({'Content-Type': 'application/json'})
builder.jsonBody(EXPECTED_BODY)
});
return provider.executeTest(async (mockserver) => {
// Act: test our API client behaves correctly
//
// Note we configure the DogService API client dynamically to
// point to the mock service Pact created for us, instead of
// the real one
dogService = new DogService(mockserver.url);
const response = await dogService.getMeDogs('today')
// Assert: check the result
expect(response.data[0]).to.deep.eq(dogExample);
});
});
});