Skip to content

Commit 56ab8c0

Browse files
authored
fix: allow an undefined user id to be passed (#776)
1 parent 7da00f8 commit 56ab8c0

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

.changeset/swift-papayas-design.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@knocklabs/client": patch
3+
---
4+
5+
fix: ensure that user.id can be undefined

packages/client/src/knock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class Knock {
239239
return userIdOrUserWithProperties.id;
240240
}
241241

242-
throw new Error("`user` object must contain an `id` property");
242+
return undefined;
243243
}
244244
}
245245

packages/client/test/knock.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,14 @@ describe("Knock Client", () => {
186186
expect(identify).toHaveBeenCalledWith({ name: "John Doe" });
187187
});
188188

189-
test("throws error when user object does not contain an `id` property", () => {
189+
test("does not identify user when user object does not contain an `id` property", () => {
190190
const knock = new Knock("pk_test_12345");
191+
const identify = vi.spyOn(knock.user, "identify");
192+
191193
// @ts-expect-error - we want to test the error case
192-
expect(() => knock.authenticate({ name: "John Doe" })).toThrowError(
193-
"`user` object must contain an `id` property",
194-
);
194+
knock.authenticate({ name: "John Doe" });
195+
196+
expect(identify).not.toHaveBeenCalled();
195197
});
196198
});
197199

0 commit comments

Comments
 (0)