Skip to content
Closed
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
26 changes: 26 additions & 0 deletions packages/cli/src/utils/motionSpec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ describe("parseMotionSpec", () => {
if (!result.ok) expect(result.errors[0]).toContain("unknown assertion kind");
});

it.each(["constructor", "__proto__", "hasOwnProperty", "toString", "valueOf"])(
"rejects inherited validator name %s as an unknown assertion kind",
(kind) => {
expect(parseMotionSpec({ assertions: [{ kind }] })).toEqual({
ok: false,
errors: [`assertions[0]: unknown assertion kind ${JSON.stringify(kind)}`],
});
},
);

it("reports per-field errors for missing required fields", () => {
const result = parseMotionSpec({
assertions: [
Expand Down Expand Up @@ -160,6 +170,22 @@ describe("readMotionSpec", () => {
if (!result.ok) expect(result.errors[0]).toContain("no assertions");
});

it("reports an inherited kind alongside other validation errors from a sidecar", () => {
const dir = tempDir("motion-inherited-");
const path = join(dir, "index.motion.json");
writeFileSync(
path,
JSON.stringify({ assertions: [{ kind: "__proto__" }, { kind: "before", a: "#a" }] }),
);
expect(readMotionSpec(path)).toEqual({
ok: false,
errors: [
'assertions[0]: unknown assertion kind "__proto__"',
'assertions[1] (before): "b" must be a non-empty string',
],
});
});

it("parses a valid sidecar file", () => {
const dir = tempDir("motion-valid-");
const path = join(dir, "main.motion.json");
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/utils/motionSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ const VALIDATORS: Record<string, Validator> = {
function validateAssertion(raw: unknown, index: number): MotionAssertion | string {
const at = `assertions[${index}]`;
if (!isObject(raw)) return `${at}: must be an object`;
const validator = typeof raw.kind === "string" ? VALIDATORS[raw.kind] : undefined;
const validator =
typeof raw.kind === "string" && Object.hasOwn(VALIDATORS, raw.kind)
? VALIDATORS[raw.kind]
: undefined;
if (!validator) return `${at}: unknown assertion kind ${JSON.stringify(raw.kind)}`;
return validator(raw, at);
}
Expand Down