Skip to content

Commit

Permalink
feat: Make "serializeForErrorMessage" optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed May 30, 2024
1 parent eabc34b commit b2b9528
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Date: 2024-04-21T22:14:24.556Z

- Breaking: Remove unused option "retryDelayMillis"
- Feature: Make "serializeForErrorMessage" optional. Default is `JSON.stringify` with 2 indent.

# v0.3.0

Expand Down
14 changes: 12 additions & 2 deletions src/QueryBin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export class QueryBin {
constructor(queries = {}, { timeoutMillis = 1000 } = {}) {
this.values = [];
this.defaultSerializer = (item) => JSON.stringify(item, null, 2);
this.timeoutMillis = timeoutMillis;

this.latch = new Latch();
Expand Down Expand Up @@ -88,7 +89,11 @@ export class QueryBin {
return new Error(
queryDefinition.multipleFoundMessage +
"\nFound: \n" +
results.map(queryDefinition.serializeForErrorMessage).join("\n"),
results
.map(
queryDefinition.serializeForErrorMessage ?? this.defaultSerializer,
)
.join("\n"),
);
}

Expand All @@ -97,7 +102,12 @@ export class QueryBin {
this.values.length === 0
? "List is completely empty!"
: "All values: \n" +
this.values.map(queryDefinition.serializeForErrorMessage).join("\n");
this.values
.map(
queryDefinition.serializeForErrorMessage ??
this.defaultSerializer,
)
.join("\n");
return new Error(`${queryDefinition.noneFoundMessage}\n${values}`);
}

Expand Down
41 changes: 41 additions & 0 deletions src/QueryBin.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { QueryBin, QueryDefinition } from "./QueryBin";
import { createThreadsRpcOptions } from "vitest/workers";

function dividableBy(modulo: number): QueryDefinition<number> {
return {
Expand Down Expand Up @@ -238,4 +239,44 @@ describe("QueryBin", () => {
"No number is dividable by 3.\nList is completely empty!",
);
});

describe("custom serializer", () => {
function createBinWithIds() {
return new QueryBin({
byIdPrefix(expected: string): QueryDefinition<{ id: string }> {
return {
test: (item) => item.id.startsWith(expected),
multipleFoundMessage: "Multiple found.",
noneFoundMessage: "None found.",
};
},
});
}

it("uses default serialize in 'none found'", () => {
const bin = createBinWithIds();
bin.addAll([{ id: "a" }, { id: "b" }]);

expect(() => {
bin.get.byIdPrefix("c");
}).toThrow(
`None found.\nAll values: \n${json({ id: "a" })}\n${json({ id: "b" })}`,
);
});

it("uses serializer in 'multiple found'", () => {
const bin = createBinWithIds();
bin.addAll([{ id: "ab" }, { id: "ac" }]);

expect(() => {
bin.get.byIdPrefix("a");
}).toThrow(
`Multiple found.\nFound: \n${json({ id: "ab" })}\n${json({ id: "ac" })}`,
);
});
});
});

function json(obj: unknown) {
return JSON.stringify(obj, null, 2);
}
3 changes: 2 additions & 1 deletion src/example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ interface Request {
function byMethodAndUrl(method: Method, url: string): QueryDefinition<Request> {
return {
test: (item) => item.method === method && item.url.includes(url),
serializeForErrorMessage: (item) => JSON.stringify(item, null, 2),
noneFoundMessage: `Could not find requests with method ${method} and URL containing ${url}.`,
multipleFoundMessage: `Multiple requests found method ${method} and URL containing ${url}.`,
// optional
serializeForErrorMessage: (item) => JSON.stringify(item, null, 2),
};
}

Expand Down

0 comments on commit b2b9528

Please sign in to comment.