Skip to content

Commit

Permalink
feat: promise based matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
NoamGaash committed May 13, 2024
1 parent b7b0587 commit d57ff70
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { defaultMatcher } from "./utils/matchers/defaultMatcher";
export { Matcher, AdvancedRouteFromHAR } from "./utils/types";
export { defaultMatcher } from "./utils/matchers/defaultMatcher";
export { customMatcher } from "./utils/matchers/customMatcher";
export { parseContent } from "./utils/serveFromHar";
import * as path from "path";

export const test = base.extend<{
Expand Down
8 changes: 4 additions & 4 deletions src/utils/serveFromHar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export async function serveFromHar(
async () => {
await page.route(options.url ?? /.*/, async (route) => {
let entry = typeof options.matcher === "function" ?
findEntry(har, route.request(), options!.matcher) :
(options.matcher.findEntry ?? findEntry)(har, route.request(), options!.matcher.matchFunction);
await findEntry(har, route.request(), options!.matcher) :
await (options.matcher.findEntry ?? findEntry)(har, route.request(), options!.matcher.matchFunction);
if("postProcess" in options.matcher && options.matcher.postProcess) {
entry = options.matcher.postProcess(entry, route);
}
Expand All @@ -49,13 +49,13 @@ export async function serveFromHar(
);
}

export function findEntry(
export async function findEntry(
har: Har,
request: Request,
matcher: Matcher = defaultMatcher
) {
// score each entry
const entriesWithScore = har.log.entries.map((entry) => ({ entry, score: matcher(request, entry) }));
const entriesWithScore = await Promise.all(har.log.entries.map(async (entry) => ({ entry, score: await matcher(request, entry) })));

// filter out entries with negative scores
const goodEntries = entriesWithScore.filter(({ score }) => score >= 0);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { APIResponse, Request, Route, Cookie } from "@playwright/test";
import type { Entry } from "har-format";
import type { findEntry } from "./serveFromHar";

export type Matcher = (request: Request, entry: Entry) => number;
export type Matcher = (request: Request, entry: Entry) => number | Promise<number>;

export type AdvancedMatcher = {
findEntry?: typeof findEntry;
Expand Down
2 changes: 1 addition & 1 deletion tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test("record test with a joke and postprocess", async ({ page, advancedRouteFrom
});


test("record not embedded", async ({ page, advancedRouteFromHAR }) => {
test("record attached (not embedded)", async ({ page, advancedRouteFromHAR }) => {
await advancedRouteFromHAR("tests/har/temp/not-embedded.har", {
update: true,
});
Expand Down
14 changes: 8 additions & 6 deletions tests/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from "@playwright/test";
import { defaultMatcher, test } from "../lib/index";
import fs from "fs";
import { parseContent } from "../lib/index";

test("sanity", async ({ page, advancedRouteFromHAR }) => {
await advancedRouteFromHAR("tests/har/demo-todo-app.har");
Expand Down Expand Up @@ -104,7 +105,6 @@ test("ignore port number", async ({ page, advancedRouteFromHAR }) => {
) {
return 1;
}
console.log("no match", reqUrl.toString(), entryUrl.toString());
return -1;
},
});
Expand All @@ -126,7 +126,6 @@ test("ignore search params", async ({ page, advancedRouteFromHAR }) => {
) {
return 1;
}
console.log("no match", reqUrl.toString(), entryUrl.toString());
return -1;
},
});
Expand Down Expand Up @@ -173,8 +172,13 @@ async function waitForFile(path: string) {

test("attached content", async ({ page, advancedRouteFromHAR }) => {
await advancedRouteFromHAR("tests/har/temp/not-embedded.har", {
matcher: (request, entry) => {
expect(entry.response.content.text).toBeTruthy();
matcher: async (request, entry) => {
let content = await parseContent(entry.response.content, "tests/har/temp");
if(typeof content === "object")
content = content.toString();

expect(content).toBeTruthy();
expect(content).toContain("category");
return defaultMatcher(request, entry);
}
});
Expand Down Expand Up @@ -220,8 +224,6 @@ test("test a postprocess that change only part of the output", async ({ page, ad
matcher: {
postProcess(entry) {
const json = JSON.parse(entry.response.content.text ?? "{}");
console.log(json);
console.log(entry.response.content.text);
json.flags.custom = true;
entry.response.content.text = JSON.stringify(json);
return entry;
Expand Down

0 comments on commit d57ff70

Please sign in to comment.