create-esmx
Version:
A scaffold tool for creating Esmx projects
276 lines (275 loc) • 11.2 kB
JavaScript
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { getCommand } from "./package-manager.mjs";
describe("package-manager utilities", () => {
let originalUserAgent;
beforeEach(() => {
originalUserAgent = process.env.npm_config_user_agent;
});
afterEach(() => {
if (originalUserAgent !== void 0) {
process.env.npm_config_user_agent = originalUserAgent;
} else {
process.env.npm_config_user_agent = void 0;
}
});
describe("getCommand", () => {
describe("when using npm", () => {
beforeEach(() => {
process.env.npm_config_user_agent = "npm/8.19.2 node/v18.12.0 darwin x64 workspaces/false";
});
it("should return npm install command", () => {
const commandType = "install";
const result = getCommand(commandType);
expect(result).toBe("npm install");
});
it("should return npm dev command", () => {
const commandType = "dev";
const result = getCommand(commandType);
expect(result).toBe("npm run dev");
});
it("should return npm build command", () => {
const commandType = "build";
const result = getCommand(commandType);
expect(result).toBe("npm run build");
});
it("should return npm start command", () => {
const commandType = "start";
const result = getCommand(commandType);
expect(result).toBe("npm start");
});
it("should return npm create command", () => {
const commandType = "create";
const result = getCommand(commandType);
expect(result).toBe("npm create esmx@latest");
});
it("should return npm build:type command", () => {
const commandType = "build:type";
const result = getCommand(commandType);
expect(result).toBe("npm run build:type");
});
it("should return npm lint:type command", () => {
const commandType = "lint:type";
const result = getCommand(commandType);
expect(result).toBe("npm run lint:type");
});
});
describe("when using yarn", () => {
beforeEach(() => {
process.env.npm_config_user_agent = "yarn/1.22.19 npm/? node/v18.12.0 darwin x64";
});
it("should return yarn install command", () => {
const commandType = "install";
const result = getCommand(commandType);
expect(result).toBe("yarn install");
});
it("should return yarn dev command", () => {
const commandType = "dev";
const result = getCommand(commandType);
expect(result).toBe("yarn dev");
});
it("should return yarn build command", () => {
const commandType = "build";
const result = getCommand(commandType);
expect(result).toBe("yarn build");
});
it("should return yarn start command", () => {
const commandType = "start";
const result = getCommand(commandType);
expect(result).toBe("yarn start");
});
it("should return yarn create command", () => {
const commandType = "create";
const result = getCommand(commandType);
expect(result).toBe("yarn create esmx");
});
it("should return yarn build:type command", () => {
const commandType = "build:type";
const result = getCommand(commandType);
expect(result).toBe("yarn build:type");
});
it("should return yarn lint:type command", () => {
const commandType = "lint:type";
const result = getCommand(commandType);
expect(result).toBe("yarn lint:type");
});
});
describe("when using pnpm", () => {
beforeEach(() => {
process.env.npm_config_user_agent = "pnpm/7.14.0 npm/? node/v18.12.0 darwin x64";
});
it("should return pnpm install command", () => {
const commandType = "install";
const result = getCommand(commandType);
expect(result).toBe("pnpm install");
});
it("should return pnpm dev command", () => {
const commandType = "dev";
const result = getCommand(commandType);
expect(result).toBe("pnpm dev");
});
it("should return pnpm build command", () => {
const commandType = "build";
const result = getCommand(commandType);
expect(result).toBe("pnpm build");
});
it("should return pnpm start command", () => {
const commandType = "start";
const result = getCommand(commandType);
expect(result).toBe("pnpm start");
});
it("should return pnpm create command", () => {
const commandType = "create";
const result = getCommand(commandType);
expect(result).toBe("pnpm create esmx");
});
it("should return pnpm build:type command", () => {
const commandType = "build:type";
const result = getCommand(commandType);
expect(result).toBe("pnpm build:type");
});
it("should return pnpm lint:type command", () => {
const commandType = "lint:type";
const result = getCommand(commandType);
expect(result).toBe("pnpm lint:type");
});
});
describe("when using bun", () => {
beforeEach(() => {
process.env.npm_config_user_agent = "bun/0.6.0 bun/0.6.0 darwin x64";
});
it("should return bun install command", () => {
const commandType = "install";
const result = getCommand(commandType);
expect(result).toBe("bun install");
});
it("should return bun dev command", () => {
const commandType = "dev";
const result = getCommand(commandType);
expect(result).toBe("bun dev");
});
it("should return bun build command", () => {
const commandType = "build";
const result = getCommand(commandType);
expect(result).toBe("bun run build");
});
it("should return bun start command", () => {
const commandType = "start";
const result = getCommand(commandType);
expect(result).toBe("bun start");
});
it("should return bun create command", () => {
const commandType = "create";
const result = getCommand(commandType);
expect(result).toBe("bun create esmx");
});
it("should return bun build:type command", () => {
const commandType = "build:type";
const result = getCommand(commandType);
expect(result).toBe("bun run build:type");
});
it("should return bun lint:type command", () => {
const commandType = "lint:type";
const result = getCommand(commandType);
expect(result).toBe("bun run lint:type");
});
});
describe("when user agent is not set", () => {
beforeEach(() => {
process.env.npm_config_user_agent = void 0;
});
it("should default to npm commands", () => {
const commandType = "install";
const result = getCommand(commandType);
expect(result).toBe("npm install");
});
});
describe("when user agent is empty string", () => {
beforeEach(() => {
process.env.npm_config_user_agent = "";
});
it("should default to npm commands", () => {
const commandType = "dev";
const result = getCommand(commandType);
expect(result).toBe("npm run dev");
});
});
describe("when user agent contains unknown package manager", () => {
beforeEach(() => {
process.env.npm_config_user_agent = "unknown-pm/1.0.0 node/v18.12.0";
});
it("should default to npm commands", () => {
const commandType = "build";
const result = getCommand(commandType);
expect(result).toBe("npm run build");
});
});
});
describe("getCommand with userAgent parameter", () => {
beforeEach(() => {
process.env.npm_config_user_agent = void 0;
});
it("should use provided userAgent over environment variable", () => {
process.env.npm_config_user_agent = "npm/8.19.2 node/v18.12.0";
const customUserAgent = "pnpm/7.14.0 npm/? node/v18.12.0 darwin x64";
const result = getCommand("install", customUserAgent);
expect(result).toBe("pnpm install");
});
it("should detect pnpm from custom userAgent", () => {
const userAgent = "pnpm/7.14.0 npm/? node/v18.12.0 darwin x64";
const installResult = getCommand("install", userAgent);
const devResult = getCommand("dev", userAgent);
const buildResult = getCommand("build", userAgent);
expect(installResult).toBe("pnpm install");
expect(devResult).toBe("pnpm dev");
expect(buildResult).toBe("pnpm build");
});
it("should detect yarn from custom userAgent", () => {
const userAgent = "yarn/1.22.19 npm/? node/v18.12.0 darwin x64";
const installResult = getCommand("install", userAgent);
const devResult = getCommand("dev", userAgent);
const buildResult = getCommand("build", userAgent);
expect(installResult).toBe("yarn install");
expect(devResult).toBe("yarn dev");
expect(buildResult).toBe("yarn build");
});
it("should detect bun from custom userAgent", () => {
const userAgent = "bun/0.6.0 bun/0.6.0 darwin x64";
const installResult = getCommand("install", userAgent);
const buildResult = getCommand("build", userAgent);
const buildTypeResult = getCommand("build:type", userAgent);
expect(installResult).toBe("bun install");
expect(buildResult).toBe("bun run build");
expect(buildTypeResult).toBe("bun run build:type");
});
it("should default to npm when userAgent does not match known patterns", () => {
const userAgent = "unknown-package-manager/1.0.0";
const result = getCommand("install", userAgent);
expect(result).toBe("npm install");
});
it("should handle empty userAgent string", () => {
const userAgent = "";
const result = getCommand("install", userAgent);
expect(result).toBe("npm install");
});
it("should fallback to environment variable when userAgent is undefined", () => {
process.env.npm_config_user_agent = "yarn/1.22.19 npm/? node/v18.12.0";
const result = getCommand("install", void 0);
expect(result).toBe("yarn install");
});
it("should handle case-sensitive package manager detection", () => {
const userAgent = "PNPM/7.14.0 npm/? node/v18.12.0";
const result = getCommand("install", userAgent);
expect(result).toBe("npm install");
});
it("should test all command types with custom userAgent", () => {
const userAgent = "pnpm/7.14.0 npm/? node/v18.12.0";
expect(getCommand("install", userAgent)).toBe("pnpm install");
expect(getCommand("dev", userAgent)).toBe("pnpm dev");
expect(getCommand("build", userAgent)).toBe("pnpm build");
expect(getCommand("start", userAgent)).toBe("pnpm start");
expect(getCommand("create", userAgent)).toBe("pnpm create esmx");
expect(getCommand("build:type", userAgent)).toBe("pnpm build:type");
expect(getCommand("lint:type", userAgent)).toBe("pnpm lint:type");
});
});
});