UNPKG

@o3r/testing

Version:

The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.

79 lines 4.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateDatesInMocks = updateDatesInMocks; const temporal_polyfill_1 = require("temporal-polyfill"); /** * Update mock checksum and response body to keep using the same mocks every day with updated dates * @param mock the mock instance provided by the hook method of Kassette * @param inputOptions default options will extract ISO strings and use 'day-offset' mode */ async function updateDatesInMocks(mock, inputOptions = {}) { const plainDateLength = 'YYYY-MM-DDThh:mm:ss'.length; const options = { mode: 'day-offset', extractor: /\b(\d{4}-\d{2}-\d{2})[^"]*/g, converter: { fromDate: (date) => date.toString().substring(0, plainDateLength), toDate: (input) => { input = input.replace(/\.\d+/, '').replace(/Z/, '+00:00'); return input.length > plainDateLength ? temporal_polyfill_1.Temporal.ZonedDateTime.from(`${input.replace(/(\.\d+)/, '')}[${input.replace(/^.*(\+\d{2}:\d{2}).*$/, '$1') || '+00:00'}]`) : temporal_polyfill_1.Temporal.PlainDate.from(input); } }, ...inputOptions }; const todayTime = temporal_polyfill_1.Temporal.Now.plainDateISO(); // Update request const replaceDatesInInput = (input) => { switch (options.mode) { case 'any': { return input.replace(options.extractor, '<any>'); } case 'day-offset': { return input.replace(options.extractor, (match) => `<t+${temporal_polyfill_1.Temporal.PlainDate.from(todayTime).until(options.converter.toDate(match)).toString()}>`); } case 'same-day-of-week': { return input.replace(options.extractor, (match) => `<day ${options.converter.toDate(match).dayOfWeek} next week>`); } } return input; }; const checksum = await mock.checksum({ headers: false, body: { filter: (body) => replaceDatesInInput(body.toString()) }, query: { filter: (params) => Object.fromEntries(Object.entries(params).map(([key, value]) => [key, replaceDatesInInput(value)])) }, customData: { updateDatesMode: options.mode } }); mock.setLocalPath([mock.localPath, checksum]); // Update response if (/get|post/.test(mock.request.method) && (await mock.hasLocalMock())) { const localPayload = (await mock.readLocalPayload())?.payload; if (localPayload && localPayload.data.creationDateTime) { const referenceTime = temporal_polyfill_1.Temporal.PlainDate.from(localPayload.data.creationDateTime.toISOString().substring(0, 10)); const timeOffset = temporal_polyfill_1.Temporal.PlainDate.from(referenceTime).until(todayTime, { smallestUnit: 'days', largestUnit: 'days' }); if (timeOffset.days !== 0) { mock.setMode('manual'); const replaceDatesInOutput = (output) => { switch (options.mode) { case 'any': case 'day-offset': { return output.replace(options.extractor, (match) => options.converter.fromDate(options.converter.toDate(match).add(timeOffset))); } case 'same-day-of-week': { return output.replace(options.extractor, (match) => options.converter.fromDate(options.converter.toDate(match).add(`P${Math.ceil(timeOffset.days / 7)}W`))); } } return output; }; const wrappedPayload = mock.createPayload({ data: localPayload.data, body: replaceDatesInOutput(localPayload.body?.toString() || '') }); mock.fillResponseFromPayload(wrappedPayload); await mock.sendResponse(); } } } } //# sourceMappingURL=update-dates-in-mocks.js.map