ftmocks-utils 1.3.2 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/compare-utils.js +151 -0
- package/src/file-utils.js +39 -0
- package/src/index.js +22 -971
- package/src/log-utils.js +15 -2
- package/src/match-utils.js +94 -0
- package/src/mock-utils.js +101 -0
- package/src/playwright-utils.js +340 -0
- package/src/rank-compare-utils.js +62 -0
- package/src/react-utils.js +189 -0
- package/src/recorder.js +289 -271
- package/src/snap-utils.js +45 -0
- package/src/test-utils.js +49 -0
package/package.json
CHANGED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
const { clearNulls, processURL } = require("./common-utils");
|
|
2
|
+
const { FtJSON } = require("./json-utils");
|
|
3
|
+
|
|
4
|
+
const isUrlAndMethodSame = (req1, req2) => {
|
|
5
|
+
const url1 = new URL(`http://domain.com${req1.url}`);
|
|
6
|
+
const url2 = new URL(`http://domain.com${req2.url}`);
|
|
7
|
+
return (
|
|
8
|
+
url1.pathname === url2.pathname &&
|
|
9
|
+
url1.method?.toLowerCase() === url2.method?.toLowerCase()
|
|
10
|
+
);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const isSameRequest = (req1, req2) => {
|
|
14
|
+
clearNulls(req1.postData);
|
|
15
|
+
clearNulls(req2.postData);
|
|
16
|
+
let matched = true;
|
|
17
|
+
if (req1.url !== req2.url) {
|
|
18
|
+
matched = false;
|
|
19
|
+
} else if (req1.method?.toLowerCase() !== req2.method?.toLowerCase()) {
|
|
20
|
+
matched = false;
|
|
21
|
+
} else if (
|
|
22
|
+
(!req1.postData && req2.postData) ||
|
|
23
|
+
(req1.postData && !req2.postData)
|
|
24
|
+
) {
|
|
25
|
+
matched = FtJSON.areJsonEqual(req1.postData || {}, req2.postData || {});
|
|
26
|
+
} else if (
|
|
27
|
+
req1.postData &&
|
|
28
|
+
req2.postData &&
|
|
29
|
+
!FtJSON.areJsonEqual(req1.postData, req2.postData)
|
|
30
|
+
) {
|
|
31
|
+
matched = false;
|
|
32
|
+
}
|
|
33
|
+
return matched;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const isSameResponse = (req1, req2) => {
|
|
37
|
+
try {
|
|
38
|
+
let matched = true;
|
|
39
|
+
if (req1.response.status !== req2.response.status) {
|
|
40
|
+
matched = false;
|
|
41
|
+
// console.log('not matched at url', req1.method, req2.method);
|
|
42
|
+
} else if (
|
|
43
|
+
(!req1.response.content && req2.response.content) ||
|
|
44
|
+
(req1.response.content && !req2.response.content)
|
|
45
|
+
) {
|
|
46
|
+
matched = FtJSON.areJsonEqual(
|
|
47
|
+
FtJSON.parse(req1.response.content) || {},
|
|
48
|
+
FtJSON.parse(req2.response.content) || {}
|
|
49
|
+
);
|
|
50
|
+
// console.log('not matched at post Data 0', req1.postData, req2.postData);
|
|
51
|
+
} else if (
|
|
52
|
+
req1.response.content &&
|
|
53
|
+
req2.response.content &&
|
|
54
|
+
!FtJSON.areJsonEqual(
|
|
55
|
+
FtJSON.parse(req1.response.content) || {},
|
|
56
|
+
FtJSON.parse(req2.response.content) || {}
|
|
57
|
+
)
|
|
58
|
+
) {
|
|
59
|
+
matched = false;
|
|
60
|
+
}
|
|
61
|
+
// if (matched) {
|
|
62
|
+
// console.log('matched responses', req1, req2);
|
|
63
|
+
// }
|
|
64
|
+
return matched;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error(error);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
function compareMockToRequest(mock, req) {
|
|
72
|
+
const mockURL = processURL(
|
|
73
|
+
mock.fileContent.url,
|
|
74
|
+
mock.fileContent.ignoreParams
|
|
75
|
+
);
|
|
76
|
+
const reqURL = processURL(req.originalUrl, mock.fileContent.ignoreParams);
|
|
77
|
+
const isSameUrlAndMethod = isUrlAndMethodSame(
|
|
78
|
+
{ url: mockURL, method: mock.fileContent.method },
|
|
79
|
+
{ url: reqURL, method: req.method }
|
|
80
|
+
);
|
|
81
|
+
if (!isSameUrlAndMethod) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
const postData = mock.fileContent.request?.postData?.text
|
|
85
|
+
? FtJSON.parse(mock.fileContent.request?.postData?.text)
|
|
86
|
+
: mock.fileContent.request?.postData;
|
|
87
|
+
return isSameRequest(
|
|
88
|
+
{ url: mockURL, method: mock.fileContent.method, postData },
|
|
89
|
+
{
|
|
90
|
+
method: req.method,
|
|
91
|
+
postData: req.body,
|
|
92
|
+
url: reqURL,
|
|
93
|
+
}
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function compareMockToFetchRequest(mock, fetchReq) {
|
|
98
|
+
try {
|
|
99
|
+
const mockURL = processURL(
|
|
100
|
+
mock.fileContent.url,
|
|
101
|
+
mock.fileContent.ignoreParams
|
|
102
|
+
);
|
|
103
|
+
const reqURL = processURL(fetchReq.url, mock.fileContent.ignoreParams);
|
|
104
|
+
const isSameUrlAndMethod = isUrlAndMethodSame(
|
|
105
|
+
{ url: mockURL, method: mock.fileContent.method },
|
|
106
|
+
{ url: reqURL, method: fetchReq.options.method || "GET" }
|
|
107
|
+
);
|
|
108
|
+
if (!isSameUrlAndMethod) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
const postData = mock.fileContent.request?.postData?.text
|
|
112
|
+
? FtJSON.parse(mock.fileContent.request?.postData?.text)
|
|
113
|
+
: mock.fileContent.request?.postData;
|
|
114
|
+
return isSameRequest(
|
|
115
|
+
{ url: mockURL, method: mock.fileContent.method, postData },
|
|
116
|
+
{
|
|
117
|
+
method: fetchReq.options.method || "GET",
|
|
118
|
+
postData: fetchReq.options.body?.length
|
|
119
|
+
? FtJSON.parse(fetchReq.options.body)
|
|
120
|
+
: fetchReq.options.body,
|
|
121
|
+
url: reqURL,
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
} catch (e) {
|
|
125
|
+
console.error("error at compareMockToFetchRequest", mock, fetchReq);
|
|
126
|
+
console.error(e);
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const compareMockToMock = (mock1, mock2, matchResponse) => {
|
|
132
|
+
try {
|
|
133
|
+
if (matchResponse) {
|
|
134
|
+
return isSameRequest(mock1, mock2) && isSameResponse(mock1, mock2);
|
|
135
|
+
} else {
|
|
136
|
+
return isSameRequest(mock1, mock2);
|
|
137
|
+
}
|
|
138
|
+
} catch (error) {
|
|
139
|
+
console.error(error);
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
module.exports = {
|
|
145
|
+
isUrlAndMethodSame,
|
|
146
|
+
isSameRequest,
|
|
147
|
+
isSameResponse,
|
|
148
|
+
compareMockToRequest,
|
|
149
|
+
compareMockToFetchRequest,
|
|
150
|
+
compareMockToMock,
|
|
151
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const path = require("path");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const { getMockDir, nameToFolder } = require("./common-utils");
|
|
4
|
+
|
|
5
|
+
const saveIfItIsFile = async (route, testName, ftmocksConifg) => {
|
|
6
|
+
const urlObj = new URL(route.request().url());
|
|
7
|
+
|
|
8
|
+
// Check if URL contains file extension like .js, .png, .css etc
|
|
9
|
+
const fileExtMatch = urlObj.pathname.match(/\.[a-zA-Z0-9]+$/);
|
|
10
|
+
if (fileExtMatch) {
|
|
11
|
+
const fileExt = fileExtMatch[0];
|
|
12
|
+
// Create directory path matching URL structure
|
|
13
|
+
const dirPath = path.join(
|
|
14
|
+
getMockDir(ftmocksConifg),
|
|
15
|
+
`test_${nameToFolder(testName)}`,
|
|
16
|
+
"_files",
|
|
17
|
+
path.dirname(urlObj.pathname)
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
// Create directories if they don't exist
|
|
21
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
22
|
+
|
|
23
|
+
// Save file with original name
|
|
24
|
+
const fileName = path.basename(urlObj.pathname);
|
|
25
|
+
const filePath = path.join(dirPath, fileName);
|
|
26
|
+
|
|
27
|
+
const response = await route.fetch();
|
|
28
|
+
const buffer = await response.body();
|
|
29
|
+
fs.writeFileSync(filePath, buffer);
|
|
30
|
+
|
|
31
|
+
await route.continue();
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
module.exports = {
|
|
38
|
+
saveIfItIsFile,
|
|
39
|
+
};
|