I have this function:
saveApplication(details: Application): Observable<any> {
return this.http.post(`http://localhost:5000/Apply`, details);
}
And I am writing a Pact Test for it (API contract testing) and below is the test:
describe('ApplicationService contract testing', () => {
let provider: PactWeb;
let applicationService: ApplicationService;
beforeAll(function (done) {
provider = new PactWeb({
cors: true, host: '127.0.0.1', port: 9776, logLevel: 'error'
});
setTimeout(done, 2000);
provider.removeInteractions();
});
afterAll(function (done) {
provider.finalize().then(function () {
done();
}, function (err) { done.fail(err); });
});
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ApplicationService],
imports: [HttpClientTestingModule]
});
applicationService = getTestBed().get(ApplicationService);
});
afterEach((done) => {
provider.verify().then(done, e => done.fail(e));
});
describe('Save', () => {
const _details: Application = {
title: 'Mr', firstName: 'Application', lastName: 'applicant',
dob: new Date(1977, 10, 10).toString(), gender: 'M'
};
beforeAll((done) => {
provider.addInteraction({
state: ' a task to save the dashboard application to mongo',
uponReceiving: ' a request to post',
withRequest: {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
path: '/Apply', body: _details
}, willRespondWith:
{
status: 201,
headers: {
'Content-Type': 'application/json'
}
}
}).then(done, e => done.fail(e));
it('should call the API to Post the application Content', (done) => {
const _response: any = {};
applicationService.saveApplication(_details).subscribe(res => {
expect(res).toEqual(_response);
done();
}, error => { done.fail(error); }
);
});
});
});
});
The test isn't running when id npm run test or ng test, ut the pacts directory is being created. I don't know what is missing from my configs
This is what I have in Karma:
frameworks: ['jasmine', '@angular-devkit/build-angular','pact'],
plugins: [....,
require('@pact-foundation/karma-pact')],
pact: [{
cors: true,
port: 9776,
consumer: "ui",
provider: "Apply",
dir: "pacts/",
spec: 2
}],
proxies: {
'/api/applications/v1/': 'http://localhost:9776/Apply/'
},
Not sure what I am missing. any help?