-
Notifications
You must be signed in to change notification settings - Fork 15
/
pactFromMswServer.msw.spec.ts
166 lines (149 loc) · 4.62 KB
/
pactFromMswServer.msw.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import API from "../examples/react/src/api";
import { HttpResponse, http } from "msw";
import { setupServer } from "msw/node";
import { PactFile, setupPactMswAdapter } from "./pactMswAdapter";
const pjson = require("../package.json");
const server = setupServer();
const pactMswAdapter = setupPactMswAdapter({
server,
options: {
consumer: "testConsumer",
providers: {
["testProvider"]: ["products"],
["testProvider2"]: ["/product/10"],
},
debug: true,
includeUrl: ["products", "/product"],
excludeUrl: ["/product/11"],
excludeHeaders: ["x-powered-by", "cookie", "accept-encoding", "host"],
},
});
describe("API - With MSW mock generating a pact", () => {
beforeAll(async () => {
server.listen();
});
beforeEach(async () => {
pactMswAdapter.newTest();
});
afterEach(async () => {
pactMswAdapter.verifyTest();
server.resetHandlers();
});
afterAll(async () => {
await pactMswAdapter.writeToFile(); // writes the pacts to a file
pactMswAdapter.clear();
server.close();
});
test("get all products", async () => {
const products = [
{
id: "09",
type: "CREDIT_CARD",
name: "Gem Visa",
},
];
server.use(
http.get(API.url + "/products", () => {
return HttpResponse.json(products)
})
);
const respProducts = await API.getAllProducts();
expect(respProducts).toEqual(products);
});
test("post product ID 10", async () => {
const productData = {
type: "CREDIT_CARD",
name: "28 Degrees",
};
server.use(
http.post(API.url + "/product/10", () => {
return HttpResponse.json(productData)
})
);
const respProduct = await API.postProduct("10", productData);
expect(respProduct).toEqual(productData);
});
test("get product ID 10", async () => {
const product = {
id: "10",
type: "CREDIT_CARD",
name: "28 Degrees",
};
server.use(
http.get(API.url + "/product/10", () => {
return HttpResponse.json(product)
})
);
const respProduct = await API.getProduct("10");
expect(respProduct).toEqual(product);
});
test("get product ID 10 with visibility hidden", async () => {
const product = {
id: "10",
type: "CREDIT_CARD",
name: "28 Degrees",
};
const hiddenVisibilityProduct = {
...product,
visibility: "hidden",
};
server.use(
http.get(API.url + "/product/10", ({ request }) => {
const visibility = new URL(request.url).searchParams.get("visibility");
const response =
visibility === "hidden" ? hiddenVisibilityProduct : product;
return HttpResponse.json(response)
})
);
const respProduct = await API.getProduct("10", { visibility: "hidden" });
expect(respProduct).toEqual(hiddenVisibilityProduct);
});
test("unhandled route", async () => {
await expect(API.getProduct("11")).rejects.toThrow(
/^Error: connect ECONNREFUSED (127.0.0.1|::1):8081.*$/
);
});
test("creates pact files", async () => {
let pactResults: PactFile[] = [];
await pactMswAdapter.writeToFile((path, data) => {
pactResults.push(data as PactFile);
}); // writes the pacts to a file
expect(pactResults.length).toEqual(2);
expect(pactResults[0].consumer.name).toEqual("testConsumer");
expect(pactResults[0].provider.name).toEqual("testProvider");
expect(pactResults[1].consumer.name).toEqual("testConsumer");
expect(pactResults[1].provider.name).toEqual("testProvider2");
expect(pactResults[0].interactions[0].request.method).toEqual("GET");
expect(pactResults[0].interactions[0].request.path).toEqual("/products");
expect(pactResults[0].interactions[0].request.headers).toEqual({
accept: "application/json, text/plain, */*",
authorization: expect.any(String),
"user-agent": expect.any(String),
});
expect(pactResults[0].interactions[0].response.status).toEqual(200);
expect(pactResults[0].interactions[0].response.headers).toEqual({
"content-type": "application/json",
});
expect(pactResults[0].interactions[0].response.body).toEqual([
{
id: "09",
type: "CREDIT_CARD",
name: "Gem Visa",
},
]);
expect(pactResults[1].interactions[0].request.body).toEqual({
type: "CREDIT_CARD",
name: "28 Degrees",
});
expect(pactResults[1].interactions[1].request.body).toBeUndefined();
expect(pactResults[0].metadata).toEqual({
pactSpecification: {
version: "2.0.0",
},
client: {
name: "pact-msw-adapter",
version: pjson.version,
},
});
});
});