Files
better-auth/packages/cli/test/version-warning.test.ts
Alex Yang 73ca92ee8c fix(cli): warn when old @better-auth/cli is used with better-auth v1.5.x+
Closes #8622

When the old @better-auth/cli detects that better-auth >= 1.5.0 is
installed, it now prints a warning directing users to the new `auth` CLI
(`npx auth@latest`). This prevents unexpected behavior from the old CLI
generating incorrect schemas for v1.5.x projects.

Uses `createRequire` from the user's CWD to resolve better-auth's
package.json and `semver.gte` for version comparison. Uses `chalk` for
terminal colors (consistent with the rest of the CLI). Silently skips
the check if better-auth is not installed.
2026-03-16 13:22:20 -07:00

82 lines
2.2 KiB
TypeScript

import { exec } from "node:child_process";
import fs from "node:fs/promises";
import path from "node:path";
import { promisify } from "node:util";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { cliPath } from "./utils";
const execAsync = promisify(exec);
let tmpDir: string;
/**
* @see https://github.com/better-auth/better-auth/issues/8622
*/
describe("version warning for better-auth v1.5.x+", () => {
beforeEach(async () => {
const tmp = path.join(
process.cwd(),
"node_modules",
".cache",
"version_warning_test-",
);
await fs.mkdir(path.dirname(tmp), { recursive: true });
tmpDir = await fs.mkdtemp(tmp);
});
afterEach(async () => {
await fs.rm(tmpDir, { recursive: true });
});
async function setupBetterAuth(version: string) {
const pkgDir = path.join(tmpDir, "node_modules", "better-auth");
await fs.mkdir(pkgDir, { recursive: true });
await fs.writeFile(
path.join(pkgDir, "package.json"),
JSON.stringify({ name: "better-auth", version }),
);
// Create a minimal entry point so require.resolve works
await fs.writeFile(path.join(pkgDir, "index.js"), "");
}
it("should warn when better-auth >= 1.5.0 is installed", async () => {
await setupBetterAuth("1.5.0");
const { stderr } = await execAsync(`node ${cliPath} --help`, {
cwd: tmpDir,
});
expect(stderr).toContain("You are using @better-auth/cli");
expect(stderr).toContain("npx auth@latest");
});
it("should warn for better-auth 1.5.3", async () => {
await setupBetterAuth("1.5.3");
const { stderr } = await execAsync(`node ${cliPath} --help`, {
cwd: tmpDir,
});
expect(stderr).toContain("better-auth v1.5.3");
expect(stderr).toContain("npx auth@latest");
});
it("should not warn when better-auth < 1.5.0 is installed", async () => {
await setupBetterAuth("1.4.21");
const { stderr } = await execAsync(`node ${cliPath} --help`, {
cwd: tmpDir,
});
expect(stderr).not.toContain("npx auth@latest");
});
it("should not warn when better-auth is not installed", async () => {
const { stderr } = await execAsync(`node ${cliPath} --help`, {
cwd: tmpDir,
});
expect(stderr).not.toContain("npx auth@latest");
});
});