@dhlab/e2e-autogen
Version:
Google 스프레드시트 기반 시나리오를 Playwright 테스트 스텁 코드로 자동 생성하고, 테스트 실행 결과를 다시 시트에 업데이트하는 CLI 도구
53 lines (51 loc) • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mswScenarioTest = void 0;
const tslib_1 = require("tslib");
const config_1 = require("../../../config");
const playwright_1 = require("@msw/playwright");
const test_1 = require("@playwright/test");
const path_1 = tslib_1.__importDefault(require("path"));
const url_1 = require("url");
const mswScenarioTest = test_1.test.extend({
network: async ({}, use) => {
const { handlers } = await loadHandlersFromUserConfig();
const fixture = (0, playwright_1.createNetworkFixture)({ initialHandlers: handlers });
await use(fixture);
},
page: async ({ page }, use, testInfo) => {
const scenarioId = extractScenarioId(testInfo.title);
if (scenarioId) {
await page.addInitScript((sid) => {
const globalWindow = globalThis;
const originalFetch = globalWindow.fetch.bind(globalWindow);
globalWindow.fetch = (input, init) => {
const newInit = {
...(init ?? {}),
headers: {
...(init?.headers ?? {}),
"x-scenario": sid,
},
};
return originalFetch(input, newInit);
};
}, scenarioId);
}
await use(page);
},
});
exports.mswScenarioTest = mswScenarioTest;
async function loadHandlersFromUserConfig() {
const config = await (0, config_1.loadUserConfig)();
const handlerModule = await import((0, url_1.pathToFileURL)(path_1.default.resolve(config.mswHandlersFile)).href);
const handlers = handlerModule.handlers;
if (!Array.isArray(handlers)) {
throw new Error(`[e2e-autogen] ${config.mswHandlersFile}에서 handlers 배열을 찾을 수 없습니다.`);
}
return { handlers };
}
function extractScenarioId(title) {
// 제목 포맷: "[TC-1.1] ..." 와 같이 대괄호 안에 TC-... 가 포함됨
const match = title.match(/\[(TC-[\d.]+)]/);
return match?.[1];
}