-
-
Notifications
You must be signed in to change notification settings - Fork 343
/
index.ts
56 lines (51 loc) · 1.96 KB
/
index.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
54
55
56
import { ConsumerPact, makeConsumerPact } from '@pact-foundation/pact-core';
import { UnconfiguredInteraction } from './http';
import { PactV4Options, V4UnconfiguredInteraction } from './http/types';
import { V4ConsumerPact } from './types';
import { version as pactPackageVersion } from '../../package.json';
import { V4UnconfiguredSynchronousMessage } from './message/types';
import { UnconfiguredSynchronousMessage } from './message';
import { SpecificationVersion } from '../v3';
export class PactV4 implements V4ConsumerPact {
private pact: ConsumerPact;
constructor(private opts: PactV4Options) {
this.setup();
this.pact.addMetadata('pact-js', 'version', pactPackageVersion);
}
setup(): void {
this.pact = makeConsumerPact(
this.opts.consumer,
this.opts.provider,
this.opts.spec ?? SpecificationVersion.SPECIFICATION_VERSION_V4,
this.opts.logLevel ?? 'info'
);
}
addInteraction(): V4UnconfiguredInteraction {
return new UnconfiguredInteraction(
this.pact,
this.pact.newInteraction(''),
this.opts,
() => {
// This function needs to be called if the PactV4 object is to be re-used (commonly expected by users)
// Because of the type-state model used here, it's a bit awkward as we need to thread this through
// to children, ultimately to be called on the "executeTest" stage.
this.setup();
}
);
}
addSynchronousInteraction(
description: string
): V4UnconfiguredSynchronousMessage {
return new UnconfiguredSynchronousMessage(
this.pact,
this.pact.newSynchronousMessage(description),
this.opts,
() => {
// This function needs to be called if the PactV4 object is to be re-used (commonly expected by users)
// Because of the type-state model used here, it's a bit awkward as we need to thread this through
// to children, ultimately to be called on the "executeTest" stage.
this.setup();
}
);
}
}