Skip to content

test: add regression tests for #8156 (secondaryStorage with auto-deserialized JSON)#8430

Open
himself65 wants to merge 1 commit intomainfrom
fix/close-8156-regression-test
Open

test: add regression tests for #8156 (secondaryStorage with auto-deserialized JSON)#8430
himself65 wants to merge 1 commit intomainfrom
fix/close-8156-regression-test

Conversation

@himself65
Copy link
Copy Markdown
Contributor

Summary

Closes #8156

The root cause of this issue was already fixed in #8248 (reviveDates in safeJSONParse), which correctly converts ISO 8601 date strings back to Date instances when secondary storage (e.g. @vercel/kv) returns already-parsed objects instead of strings.

This PR adds regression tests to prevent the issue from recurring:

  • Adapter-level test (internal-adapter.test.ts): Simulates @vercel/kv behavior where get() returns a parsed object. Verifies that updateUser and updateUserByEmail correctly refresh sessions without crashing on expiresAt.getTime().
  • Route-level test (update-user.test.ts): End-to-end test via the auth client confirming that updateUser propagates changes and sessions remain valid when secondary storage auto-deserializes JSON.

Both tests verify the precondition (typeof expiresAt === "string" in the store) before exercising the code path that previously crashed.

Test plan

  • npx vitest packages/better-auth/src/db/internal-adapter.test.ts --run — 34 tests pass
  • npx vitest packages/better-auth/src/api/routes/update-user.test.ts --run — 22 tests pass
  • biome check passes on changed files

…rialized JSON)

Add tests covering the scenario where secondaryStorage (e.g. @vercel/kv)
returns already-parsed objects instead of strings, causing expiresAt to be
a string rather than a Date. The root cause was fixed in #8248 via
reviveDates in safeJSONParse. These tests prevent regression.
@himself65 himself65 requested a review from Bekacru as a code owner March 5, 2026 21:33
Copilot AI review requested due to automatic review settings March 5, 2026 21:33
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
better-auth-demo Ignored Ignored Mar 5, 2026 9:33pm
better-auth Skipped Skipped Mar 5, 2026 9:33pm

Request Review

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 5, 2026

Open in StackBlitz

@better-auth/api-key

npm i https://pkg.pr.new/@better-auth/api-key@8430

better-auth

npm i https://pkg.pr.new/better-auth@8430

auth

npm i https://pkg.pr.new/auth@8430

@better-auth/core

npm i https://pkg.pr.new/@better-auth/core@8430

@better-auth/drizzle-adapter

npm i https://pkg.pr.new/@better-auth/drizzle-adapter@8430

@better-auth/electron

npm i https://pkg.pr.new/@better-auth/electron@8430

@better-auth/expo

npm i https://pkg.pr.new/@better-auth/expo@8430

@better-auth/i18n

npm i https://pkg.pr.new/@better-auth/i18n@8430

@better-auth/kysely-adapter

npm i https://pkg.pr.new/@better-auth/kysely-adapter@8430

@better-auth/memory-adapter

npm i https://pkg.pr.new/@better-auth/memory-adapter@8430

@better-auth/mongo-adapter

npm i https://pkg.pr.new/@better-auth/mongo-adapter@8430

@better-auth/oauth-provider

npm i https://pkg.pr.new/@better-auth/oauth-provider@8430

@better-auth/passkey

npm i https://pkg.pr.new/@better-auth/passkey@8430

@better-auth/prisma-adapter

npm i https://pkg.pr.new/@better-auth/prisma-adapter@8430

@better-auth/redis-storage

npm i https://pkg.pr.new/@better-auth/redis-storage@8430

@better-auth/scim

npm i https://pkg.pr.new/@better-auth/scim@8430

@better-auth/sso

npm i https://pkg.pr.new/@better-auth/sso@8430

@better-auth/stripe

npm i https://pkg.pr.new/@better-auth/stripe@8430

@better-auth/telemetry

npm i https://pkg.pr.new/@better-auth/telemetry@8430

@better-auth/test-utils

npm i https://pkg.pr.new/@better-auth/test-utils@8430

commit: 49957cc

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds regression coverage for issue #8156 where secondaryStorage implementations that return already-parsed JSON objects (e.g. @vercel/kv) previously caused crashes during session refresh due to expiresAt being a string instead of a Date.

Changes:

  • Add an adapter-level regression test ensuring updateUser / updateUserByEmail refresh sessions correctly when secondary storage returns parsed objects.
  • Add a route-level regression test verifying authClient.updateUser() propagates changes and sessions remain valid under the same secondary storage behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/better-auth/src/db/internal-adapter.test.ts Adds adapter-level regression test simulating KV-style auto-deserialization.
packages/better-auth/src/api/routes/update-user.test.ts Adds end-to-end regression test validating update propagation + session validity with KV-style storage behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

},
} satisfies BetterAuthOptions;

(await getMigrations(testOpts)).runMigrations();
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runMigrations() is async (returns a Promise) but isn't awaited here. That can lead to a race where init(testOpts) runs before tables are created, causing intermittent failures. Await the runMigrations() call (or await (await getMigrations(testOpts)).runMigrations()).

Suggested change
(await getMigrations(testOpts)).runMigrations();
await (await getMigrations(testOpts)).runMigrations();

Copilot uses AI. Check for mistakes.
@gustavovalverde gustavovalverde added core Core infra, API routes, session, cookies, client SDK database Database layer, all adapters, schema, migrations labels Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core infra, API routes, session, cookies, client SDK database Database layer, all adapters, schema, migrations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using secondaryStorage gives TypeError: expiresAt.getTime is not a function when calling auth.api.updateUser()

5 participants