@@ -4,19 +4,8 @@ import { ThingAPI } from './thing'
4
4
// Extract matchers here to improve readability when used in the test
5
5
const { like } = MatchersV3;
6
6
7
- // Create a 3 level test hierarchy
8
- //
9
- // 1. Top level describe block containing the name of the API being tested
10
- // 2. Describe block for the specific API endpoint
11
- // 3. Test block for the specific test case
12
- // 4. Execute the test case
13
- // 5. Call the API under test
14
- // 6. Assert the response
15
- // 8. Use Pact matchers to constrain and test the Provider response
16
- // 7. Use Jest matchers to assert the API client behaviour
17
-
18
7
// Top level - name of the API
19
- describe("Thing API", () => {
8
+ describe("🧱 Thing API", () => {
20
9
// Use the PactV4 class, and serialise the Pact as V4 Pact Specification
21
10
const pact = new PactV4({
22
11
consumer: "ThingConsumer",
@@ -25,20 +14,18 @@ describe("Thing API", () => {
25
14
});
26
15
27
16
// Level 2 - Describe block for the specific API endpoint
28
- describe("GET /thing/:id", () => {
17
+ describe("🔌 GET /thing/:id", () => {
29
18
30
19
// Level 3 - Test block for the specific test case
31
- test("given a valid thing, returns 200", async () => {
20
+ test("🧪 given a valid thing, returns 200", async () => {
32
21
await pact
33
22
.addInteraction()
34
23
.given("a thing with id 1 exists")
35
24
.uponReceiving("a request for a valid thing")
36
- // Avoid matchers on the request unless necessary
37
25
.withRequest("GET", "/thing/1", (builder) => {
38
26
builder.headers({ Accept: "application/json" });
39
27
})
40
28
.willRespondWith(200, (builder) => {
41
- // Use loose matchers where possible, to avoid unnecessary constraints on the provider
42
29
builder.jsonBody(
43
30
like({
44
31
id: 1,
@@ -48,13 +35,10 @@ describe("Thing API", () => {
48
35
);
49
36
})
50
37
.executeTest(async (mockserver) => {
51
- // Instantiate the ThingAPI client
52
38
const ThingAPI = new ThingAPI(mockserver.url);
53
39
54
- // Call the API under test
55
40
const Thing = await ThingAPI.getThingById(1);
56
41
57
- // Use Jest matchers to assert the response
58
42
expect(Thing).toEqual({
59
43
id: 1,
60
44
name: "Some 1",
0 commit comments