Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ Context (packages/probitas-expect/mixin/object_value_mixin_test.ts:236:5)
236│ await assertSnapshotWithoutColors(
237│ t,
509│ t,
510│ catchError(() => applied.toHaveUserMatching({ name: "Bob" })).message,
558│ t,
559│ catchError(() => applied.toHaveUserMatching({ name: "Bob" })).message,
│ ^
511│ );'
560│ );'
`;

snapshot[`createObjectValueMixin - toHaveUserMatching with source context 2`] = `
Expand All @@ -132,8 +132,8 @@ snapshot[`createObjectValueMixin - toHaveUserMatching with source context 2`] =
251│ const applied = mixin({ dummy: true });
252│ await assertSnapshotWithoutColors(
 ┆
526│ t,
527│ catchError(() => applied.toHaveUserMatching({ name: "Bob" })).message,
575│ t,
576│ catchError(() => applied.toHaveUserMatching({ name: "Bob" })).message,
 │ ^
528│ );'
577│ );'
`;
22 changes: 1 addition & 21 deletions packages/probitas-expect/mixin/object_value_mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Any,
buildMatchingExpected,
buildPropertyExpected,
ensureNonNullish,
formatValue,
toPascalCase,
tryOk,
Expand Down Expand Up @@ -318,18 +317,9 @@ export function createObjectValueMixin<
const obj = getter.call(this);

let matcherError: Error | undefined;
let propertyExists = false;

try {
// @std/expect mutates array keyPath, so we need to copy it
const keyPathCopy = Array.isArray(keyPath) ? [...keyPath] : keyPath;
stdExpect(obj).toHaveProperty(keyPathCopy);
propertyExists = true;
const keyPathStr = Array.isArray(keyPath) ? keyPath.join(".") : keyPath;
const value = ensureNonNullish(
getPropertyValue(obj, keyPath),
keyPathStr,
);
const value = getPropertyValue(obj, keyPath);
matcher(value);
} catch (error) {
if (error instanceof Error) {
Expand All @@ -344,16 +334,6 @@ export function createObjectValueMixin<
if (isNegated ? passes : !passes) {
const keyPathStr = Array.isArray(keyPath) ? keyPath.join(".") : keyPath;

if (!propertyExists && !isNegated) {
throw createExpectationError({
message:
`Expected ${valueName} property "${keyPathStr}" to exist and satisfy the matcher, but it does not exist`,
expectOrigin: config.expectOrigin,
theme: config.theme,
subject: config.subject,
});
}

throw createExpectationError({
message: isNegated
? `Expected ${valueName} property "${keyPathStr}" to not satisfy the matcher, but it did`
Expand Down
49 changes: 49 additions & 0 deletions packages/probitas-expect/mixin/object_value_mixin_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,55 @@ Deno.test("createObjectValueMixin - toHaveUserPropertySatisfying", async (t) =>
).message,
);
});

await t.step("success - null value", () => {
const mixin = createObjectValueMixin(
() => ({ value: null }),
() => false,
{ valueName: "data" },
);
const applied = mixin({ dummy: true });
assertEquals(
applied.toHaveDataPropertySatisfying("value", (v) => {
if (v !== null) throw new Error("Must be null");
}),
applied,
);
});

await t.step("success - nested path with null intermediate", () => {
const mixin = createObjectValueMixin(
() => ({ user: null }),
() => false,
{ valueName: "data" },
);
const applied = mixin({ dummy: true });
assertEquals(
applied.toHaveDataPropertySatisfying("user.profile.age", (v) => {
if (v !== undefined) {
throw new Error("Must be undefined when intermediate is null");
}
}),
applied,
);
});

await t.step("success - nested path with undefined intermediate", () => {
const mixin = createObjectValueMixin(
() => ({ user: { profile: undefined } }),
() => false,
{ valueName: "data" },
);
const applied = mixin({ dummy: true });
assertEquals(
applied.toHaveDataPropertySatisfying("user.profile.age", (v) => {
if (v !== undefined) {
throw new Error("Must be undefined when intermediate is undefined");
}
}),
applied,
);
});
});

Deno.test("createObjectValueMixin - toHaveUserMatching with source context", async (t) => {
Expand Down