Possible broken test in username plugin #2312

Closed
opened 2026-03-13 09:43:18 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @kno-raziel on GitHub (Nov 15, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. create a backend
  2. create a client with username plugin
  3. call authClient.isUsernameAvailable with username lenght less than 3 letters

Current vs. Expected behavior

current error

{
    "code": "USERNAME_IS_TOO_SHORT",
    "message": "Username is too short",
    "status": 422,
    "statusText": "Unprocessable Entity"
}

expected

{
    "code": "USERNAME_TOO_SHORT",
    "message": "Username is too short",
    "status": 422,
    "statusText": "Unprocessable Entity"
}

What version of Better Auth are you using?

1.3.32

System info

{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:26 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6041",
    "release": "25.0.0",
    "cpuCount": 14,
    "cpuModel": "Apple M4 Pro",
    "totalMemory": "48.00 GB",
    "freeMemory": "0.63 GB"
  },
  "node": {
    "version": "v22.18.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "10.9.3"
  },
  "frameworks": [
    {
      "name": "react",
      "version": "^19.2.0"
    }
  ],
  "databases": null,
  "betterAuth": {
    "version": "^1.3.32",
    "config": null
  }
}

Which area(s) are affected? (Select all that apply)

Types, Other

Auth config (if applicable)

export const createAuth = (
  ctx: GenericCtx<DataModel>,
  { optionsOnly } = { optionsOnly: false },
) =>
  betterAuth({
    baseURL: siteUrl,
    logger: {
      disabled: optionsOnly,
    },
    database: authComponent.adapter(ctx),
    account: {
      accountLinking: {
        enabled: true,
      },
    },
    emailVerification: {
      sendOnSignUp: true,
      sendVerificationEmail: async ({ user, url }) => {
        await sendEmailVerification(requireActionCtx(ctx), {
          to: user.email,
          url,
        })
      },
    },
    emailAndPassword: {
      enabled: true,
      requireEmailVerification: false,
      sendResetPassword: async ({ user, url }) => {
        await sendResetPassword(requireActionCtx(ctx), {
          to: user.email,
          url,
        })
      },
    },
    // socialProviders: {
    //   github: {
    //     clientId: process.env.GITHUB_CLIENT_ID as string,
    //     clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
    //   },
    //   google: {
    //     clientId: process.env.GOOGLE_CLIENT_ID as string,
    //     clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
    //   },
    // },
    user: {
      deleteUser: {
        enabled: true,
      },
      additionalFields: {
        firstName: {
          type: 'string',
          required: false,
          defaultValue: '',
        },
        lastName: {
          type: 'string',
          required: false,
          defaultValue: '',
        },
      },
    },
    plugins: [
      magicLink({
        sendMagicLink: async ({ email, url }) => {
          await sendMagicLink(requireActionCtx(ctx), {
            to: email,
            url,
          })
        },
      }),
      emailOTP({
        async sendVerificationOTP({ email, otp }) {
          await sendOTPVerification(requireActionCtx(ctx), {
            to: email,
            code: otp,
          })
        },
      }),
      twoFactor(),
      anonymous(),
      username(),
      convex(),
    ],
  })

Additional context

I was implementing i18l to error codes when I got the following error

{ "code": "USERNAME_IS_TOO_SHORT", "message": "Username is too short", "status": 422, "statusText": "Unprocessable Entity" }

and noticed that the code key was not matching any key in USERNAME_ERROR_CODES exported from username plugin

7aa296e6cb/packages/better-auth/src/plugins/username/error-codes.ts (L1-L12)

and the test is using a value not sure where is coming from 🤔

7aa296e6cb/packages/better-auth/src/plugins/username/username.test.ts (L205-L211)

Originally created by @kno-raziel on GitHub (Nov 15, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. create a backend 2. create a client with username plugin 3. call `authClient.isUsernameAvailable ` with username lenght less than 3 letters ### Current vs. Expected behavior current error ``` { "code": "USERNAME_IS_TOO_SHORT", "message": "Username is too short", "status": 422, "statusText": "Unprocessable Entity" } ``` expected ``` { "code": "USERNAME_TOO_SHORT", "message": "Username is too short", "status": 422, "statusText": "Unprocessable Entity" } ``` ### What version of Better Auth are you using? 1.3.32 ### System info ```bash { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:26 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6041", "release": "25.0.0", "cpuCount": 14, "cpuModel": "Apple M4 Pro", "totalMemory": "48.00 GB", "freeMemory": "0.63 GB" }, "node": { "version": "v22.18.0", "env": "development" }, "packageManager": { "name": "npm", "version": "10.9.3" }, "frameworks": [ { "name": "react", "version": "^19.2.0" } ], "databases": null, "betterAuth": { "version": "^1.3.32", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Types, Other ### Auth config (if applicable) ```typescript export const createAuth = ( ctx: GenericCtx<DataModel>, { optionsOnly } = { optionsOnly: false }, ) => betterAuth({ baseURL: siteUrl, logger: { disabled: optionsOnly, }, database: authComponent.adapter(ctx), account: { accountLinking: { enabled: true, }, }, emailVerification: { sendOnSignUp: true, sendVerificationEmail: async ({ user, url }) => { await sendEmailVerification(requireActionCtx(ctx), { to: user.email, url, }) }, }, emailAndPassword: { enabled: true, requireEmailVerification: false, sendResetPassword: async ({ user, url }) => { await sendResetPassword(requireActionCtx(ctx), { to: user.email, url, }) }, }, // socialProviders: { // github: { // clientId: process.env.GITHUB_CLIENT_ID as string, // clientSecret: process.env.GITHUB_CLIENT_SECRET as string, // }, // google: { // clientId: process.env.GOOGLE_CLIENT_ID as string, // clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, // }, // }, user: { deleteUser: { enabled: true, }, additionalFields: { firstName: { type: 'string', required: false, defaultValue: '', }, lastName: { type: 'string', required: false, defaultValue: '', }, }, }, plugins: [ magicLink({ sendMagicLink: async ({ email, url }) => { await sendMagicLink(requireActionCtx(ctx), { to: email, url, }) }, }), emailOTP({ async sendVerificationOTP({ email, otp }) { await sendOTPVerification(requireActionCtx(ctx), { to: email, code: otp, }) }, }), twoFactor(), anonymous(), username(), convex(), ], }) ``` ### Additional context I was implementing i18l to error codes when I got the following error `{ "code": "USERNAME_IS_TOO_SHORT", "message": "Username is too short", "status": 422, "statusText": "Unprocessable Entity" }` and noticed that the code key was not matching any key in `USERNAME_ERROR_CODES` exported from `username` plugin https://github.com/better-auth/better-auth/blob/7aa296e6cb0cb430de349ddeb4d5c22cfdd00e2a/packages/better-auth/src/plugins/username/error-codes.ts#L1-L12 and the test is using a value not sure where is coming from 🤔 https://github.com/better-auth/better-auth/blob/7aa296e6cb0cb430de349ddeb4d5c22cfdd00e2a/packages/better-auth/src/plugins/username/username.test.ts#L205-L211
GiteaMirror added the plugin label 2026-03-13 09:43:18 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Nov 15, 2025):

It looks like the test is comparing the returned error code to a value ("USERNAME_TOO_SHORT") that isn’t matching the actual output ("USERNAME_IS_TOO_SHORT"). The first step would be to verify the exported error codes in the username plugin and compare them with what the test expects. You can check the error-codes file to see which value is defined and then adjust either the test or the plugin’s error response accordingly. Have you already attempted a quick fix in your local copy to see if standardizing the code value resolves the issue? Tag @better-auth-agent if you would like an answer from the Agent.

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Nov 15, 2025): It looks like the test is comparing the returned error code to a value ("USERNAME_TOO_SHORT") that isn’t matching the actual output ("USERNAME_IS_TOO_SHORT"). The first step would be to verify the exported error codes in the username plugin and compare them with what the test expects. You can check the error-codes file to see which value is defined and then adjust either the test or the plugin’s error response accordingly. Have you already attempted a quick fix in your local copy to see if standardizing the code value resolves the issue? Tag @better-auth-agent if you would like an answer from the Agent. <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@dosubot[bot] commented on GitHub (Nov 15, 2025):

There’s a clear mismatch between the error code returned by the username plugin and what’s expected in your tests and i18n logic. The plugin defines and uses the error code USERNAME_TOO_SHORT for short usernames, but the tests (and your error response) expect USERNAME_IS_TOO_SHORT, which is not defined in the exported error codes or used in the plugin logic. This inconsistency is the root cause of your issue and will break i18n/error handling that relies on the exported constants.

To fix this, update your tests and any client-side logic to expect USERNAME_TOO_SHORT, matching the plugin’s implementation and the exported USERNAME_ERROR_CODES constant. For best results and future-proofing, always reference error codes directly from the exported constants in both your implementation and tests, rather than using string literals. This will keep your error handling and localization robust and consistent as the codebase evolves.

You can see the relevant error code definition and usage in the plugin here:
error-codes.ts
plugin implementation
test file

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Nov 15, 2025): <!-- Answer --> There’s a clear mismatch between the error code returned by the username plugin and what’s expected in your tests and i18n logic. The plugin defines and uses the error code `USERNAME_TOO_SHORT` for short usernames, but the tests (and your error response) expect `USERNAME_IS_TOO_SHORT`, which is not defined in the exported error codes or used in the plugin logic. This inconsistency is the root cause of your issue and will break i18n/error handling that relies on the exported constants. To fix this, update your tests and any client-side logic to expect `USERNAME_TOO_SHORT`, matching the plugin’s implementation and the exported `USERNAME_ERROR_CODES` constant. For best results and future-proofing, always reference error codes directly from the exported constants in both your implementation and tests, rather than using string literals. This will keep your error handling and localization robust and consistent as the codebase evolves. You can see the relevant error code definition and usage in the plugin here: [error-codes.ts](https://github.com/better-auth/better-auth/blob/7aa296e6cb0cb430de349ddeb4d5c22cfdd00e2a/packages/better-auth/src/plugins/username/error-codes.ts#L1-L12) [plugin implementation](https://github.com/better-auth/better-auth/blob/6b29a9ca414bd2f70c391075cfa66baaba739341/packages/better-auth/src/plugins/username/index.ts#L13-L574) [test file](https://github.com/better-auth/better-auth/blob/6b29a9ca414bd2f70c391075cfa66baaba739341/packages/better-auth/src/plugins/username/username.test.ts#L155-L211) <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/ffb76152-5bb3-4684-be7c-1f0110dadd4a?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/ffb76152-5bb3-4684-be7c-1f0110dadd4a?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/ffb76152-5bb3-4684-be7c-1f0110dadd4a?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/ffb76152-5bb3-4684-be7c-1f0110dadd4a?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/ffb76152-5bb3-4684-be7c-1f0110dadd4a?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/ffb76152-5bb3-4684-be7c-1f0110dadd4a?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/ffb76152-5bb3-4684-be7c-1f0110dadd4a?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/6002)
Author
Owner

@GautamBytes commented on GitHub (Nov 15, 2025):

will be fixing it soon!

@GautamBytes commented on GitHub (Nov 15, 2025): will be fixing it soon!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2312