UNPKG

playwright-fluent

Version:
52 lines (51 loc) 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const path_1 = tslib_1.__importDefault(require("path")); const playwright_1 = require("playwright"); const SUT = tslib_1.__importStar(require("../index")); const index_1 = require("../index"); describe('on request to respond from HAR', () => { let browser = undefined; // eslint-disable-next-line @typescript-eslint/no-empty-function beforeEach(() => { }); afterEach(async () => { if (browser) { await browser.close(); } }); test('should return an error when browser has not been launched', async () => { // Given const page = undefined; const harFile = path_1.default.join(__dirname, 'on-request-to-respond-with.har'); const harFiles = [harFile]; // When // Then const expectedError = new Error("Cannot intercept requests to '/foobar' because no browser has been launched"); await SUT.onRequestToRespondFromHar('/foobar', harFiles, page, index_1.defaultHarRequestResponseOptions).catch((error) => expect(error).toMatchObject(expectedError)); }); test('should return an error when no har file provided', async () => { // Given browser = await playwright_1.chromium.launch({ headless: true, }); const context = await browser.newContext({ viewport: null }); const page = await context.newPage(); // When // Then const expectedError = new Error("Cannot intercept requests to '/foobar' because no HAR file(s) has been provided. You must provide at least one HAR file."); await SUT.onRequestToRespondFromHar('/foobar', [], page, index_1.defaultHarRequestResponseOptions).catch((error) => expect(error).toMatchObject(expectedError)); }); test('should return an error when har file does not exist', async () => { // Given browser = await playwright_1.chromium.launch({ headless: true, }); const context = await browser.newContext({ viewport: null }); const page = await context.newPage(); // When // Then const expectedError = new Error("File 'foo.har' does not exist. Ensure you have called 'recordNetworkActivity({path: foo.har})' and that you have closed the browser. HAR data is only saved to disk when the browser is closed."); await SUT.onRequestToRespondFromHar('/foobar', ['foo.har'], page, index_1.defaultHarRequestResponseOptions).catch((error) => expect(error).toMatchObject(expectedError)); }); });