diff --git a/src/detector.ids.test.ts b/src/detector.ids.test.ts index 24ad091..c2d3207 100644 --- a/src/detector.ids.test.ts +++ b/src/detector.ids.test.ts @@ -1,8 +1,9 @@ describe("check ids api", () => { - window.TS = { - token: "token", - }; - beforeAll(async () => { + beforeEach(async () => { + window.TS = { + token: "token", + }; + jest.resetModules(); await import("./detector"); }); @@ -22,4 +23,9 @@ describe("check ids api", () => { window.TS.setUserId?.("customId"); expect(window.TS.getUserId?.()).toEqual("customId"); }); + + test("reads id from cookie correctly", () => { + document.cookie = "foo=bar; tsuid=customId"; + expect(window.TS.getUserId?.()).toEqual("customId"); + }); }); diff --git a/src/detector.ts b/src/detector.ts index 49908ed..23b1692 100644 --- a/src/detector.ts +++ b/src/detector.ts @@ -55,7 +55,7 @@ window.TS.resetUserId = resetUserId; // Based on https://stackoverflow.com/a/25490531/1413687 function getUserIdCookie(): string | undefined { const cookieName = window.TS.cookieName || "tsuid"; - const regex = new RegExp("/(^|;)\\s*" + cookieName + "\\s*=\\s*([^;]+)/"); + const regex = new RegExp("(^|;)\\s*" + cookieName + "\\s*=\\s*([^;]+)"); return regex.exec(document.cookie)?.pop(); }