/
user.spec.ts
53 lines (46 loc) · 1.46 KB
/
user.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as path from 'path';
import * as sinonChai from 'sinon-chai';
import { PactV3, MatchersV3, LogLevel } from '@pact-foundation/pact';
import { UserService } from '../index';
const { like } = MatchersV3;
const LOG_LEVEL = process.env.LOG_LEVEL || 'TRACE';
const expect = chai.expect;
chai.use(sinonChai);
chai.use(chaiAsPromised);
describe('The Users API', () => {
let userService: UserService;
// Setup the 'pact' between two applications
const provider = new PactV3({
consumer: 'User Web',
provider: 'User API',
logLevel: LOG_LEVEL as LogLevel,
});
const userExample = { id: 1, name: 'Homer Simpson' };
const EXPECTED_BODY = like(userExample);
describe('get /users/:id', () => {
it('returns the requested user', () => {
// Arrange
provider
.given('a user with ID 1 exists')
.uponReceiving('a request to get a user')
.withRequest({
method: 'GET',
path: '/users/1',
})
.willRespondWith({
status: 200,
headers: { 'content-type': 'application/json' },
body: EXPECTED_BODY,
});
return provider.executeTest(async (mockserver) => {
// Act
userService = new UserService(mockserver.url);
const response = await userService.getUser(1);
// Assert
expect(response.data).to.deep.eq(userExample);
});
});
});
});