I'm trying to use PactJS to test some of my Angular Services. I'm using:
"@pact-foundation/karma-pact": "^2.1.1",
"pact": "^4.2.1",
"pact-web": "^4.2.1",
I can't get the test to run successfully. Without using async the subscribe callback never gets hit and if I do use async then Pact fails. This is my code:
import { TestBed, async, inject } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { ApiService } from '../index';
import { HttpParams } from '@angular/common/http';
import { Group } from '../../grouping/model';
let Pact = require('pact-web');
describe('ApiService', () => {
let provider;
beforeAll((done) => {
provider = Pact({
consumer: 'client',
provider: 'server',
web: true
});
// required for slower CI environments
setTimeout(done, 2000);
// Required if run with `singleRun: false`
provider.removeInteractions();
});
afterAll((done) => {
provider.finalize().then(done, e => done.fail(e));
});
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [
ApiService
]
});
}));
afterEach((done) => {
provider.verify().then(done, e => done.fail(e));
});
describe('Get all Groups', () => {
beforeAll((done) => {
provider.addInteraction({
given: 'groups exist',
uponReceiving: 'a request to get groups',
withRequest: {
method: 'GET',
path: '/api/groups'
},
willRespondWith: {
status: 200,
headers: {'Content-Type': 'application/json'},
body: [{
id: Pact.Matchers.somethingLike(1),
name: Pact.Matchers.somethingLike('a group name'),
disabled: Pact.Matchers.somethingLike(false),
}]
}
}).then(done, e => done.fail(e));
});
it('should return all groups from API', (done) => {
const apiService: ApiService = TestBed.get(ApiService);
const groups: Group[] = [{
id: 1,
name: false,
disabled: false
}];
apiService.getGroups().subscribe((response: Group[]) => {
expect(response).toEqual(groups);
done()
});
});
});
});
And the errors I get:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Failed: Actual interactions do not match expected interactions for mock MockService.
Missing requests:
GET /api/groups
See standard out/err for details.
I have been following these two projects
https://paucls.wordpress.com/2017/08/04/pact-consumer-driven-contract-implementation-in-angular/
https://github.com/koelec/angular-pact-example
Has anyone managed to successfully implement PactJS with Angular or what am I missing to get this working?