ftmocks-utils 1.0.8 → 1.1.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +35 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ftmocks-utils",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "description": "Util functions for FtMocks",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -198,6 +198,39 @@ async function resetAllMockStats({testMockData, testConfig, testName}) {
198
198
  }
199
199
  }
200
200
 
201
+ async function initiatePlaywrightRoutes (page, ftmocksConifg, testName) {
202
+ const testMockData = testName ? loadMockDataFromConfig(ftmocksConifg, testName) : [];
203
+ resetAllMockStats({testMockData, testConfig: ftmocksConifg, testName});
204
+ const defaultMockData = getDefaultMockDataFromConfig(ftmocksConifg);
205
+ console.debug('calling initiatePlaywrightRoutes fetch');
206
+ await page.route('**/*', async (route, request) => {
207
+ const url = request.url();
208
+ const options = {
209
+ options: {
210
+ url,
211
+ method: request.method(),
212
+ body: request.postData(),
213
+ }
214
+ }
215
+ console.debug('got fetch request', request.method(), request.url(), request.postData());
216
+ let mockData = getMatchingMockData({testMockData, defaultMockData, url, options, testConfig: ftmocksConifg, testName});
217
+ if (mockData) {
218
+ console.debug('mocked', url, options);
219
+ const { content, headers, status } = mockData.response;
220
+ const json = {
221
+ status,
222
+ headers,
223
+ body: content,
224
+ };
225
+
226
+ await route.fulfill(json);
227
+ } else {
228
+ console.debug('missing mock data', url, options);
229
+ await route.fallback();
230
+ }
231
+ });
232
+ }
233
+
201
234
  async function initiateJestFetch (jest, ftmocksConifg, testName) {
202
235
  const testMockData = testName ? loadMockDataFromConfig(ftmocksConifg, testName) : [];
203
236
  resetAllMockStats({testMockData, testConfig: ftmocksConifg, testName});
@@ -403,5 +436,6 @@ module.exports = {
403
436
  saveSnap,
404
437
  deleteAllSnaps,
405
438
  deleteAllLogs,
406
- initiateConsoleLogs
439
+ initiateConsoleLogs,
440
+ initiatePlaywrightRoutes
407
441
  };