Skip to content

Commit

Permalink
Add genezio supercommand tests
Browse files Browse the repository at this point in the history
  • Loading branch information
costinsin committed Dec 15, 2023
1 parent aec116e commit 705f265
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions tests/commands/superGenezio/superGenezio.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { vi, describe, beforeEach, test, expect } from "vitest";
import { vol } from "memfs";
import fs from "fs/promises";
import inquirer from "inquirer";
import { genezioCommand } from "../../../src/commands/superGenezio/superGenezio";
import { IFs } from "memfs";
import { deployCommand } from "../../../src/commands/deploy";
import { startLocalEnvironment } from "../../../src/commands/local";
import colors from "colors";
import { loginCommand } from "../../../src/commands/login";

vi.mock("fs", async () => {
const memfs = await vi.importActual<{ fs: IFs }>("memfs");
return { default: memfs.fs };
});
vi.mock("fs/promises", async () => {
const memfs = await vi.importActual<{ fs: IFs }>("memfs");
return { default: memfs.fs.promises };
});
vi.mock("../../../src/commands/deploy", async () => {
return { deployCommand: vi.fn(() => Promise.resolve()) };
});
vi.mock("../../../src/commands/local", async () => {
return { startLocalEnvironment: vi.fn(() => Promise.resolve()) };
});
vi.mock("../../../src/commands/login", async (original) => {
return { loginCommand: vi.fn(() => Promise.resolve()) };
});

describe("superGenezio", () => {
beforeEach(() => {
// Clean up the mocked file system before each test
vol.reset();
});

test("detects genezio.yaml and deploys project", async () => {
// Create environment
await fs.mkdir(process.cwd(), { recursive: true });
await fs.writeFile("genezio.yaml", "test");

const promptSpy = vi.spyOn(inquirer, "prompt");

// Mock user selecting Deploy
promptSpy.mockResolvedValueOnce({ command: "deploy" });

// Run the super command
await genezioCommand();

// Check if inquirer.prompt was called with the correct arguments
expect(promptSpy).toHaveBeenCalledWith([
{
type: "list",
name: "command",
message: colors.magenta("Genezio project detected. What would you like to do?"),
choices: [
{
name: "Deploy your project (genezio deploy)",
value: "deploy",
},
{
name: "Start a Local server (genezio local)",
value: "local",
},
],
},
]);

// Login should be called because the memfs doesn't have the auth token stored in .geneziorc
expect(loginCommand).toHaveBeenCalledOnce();
// Check if the deploy command was called
expect(deployCommand).toHaveBeenCalledOnce();
});

test("detects genezio.yaml and starts local", async () => {
// Create environment
await fs.mkdir(process.cwd(), { recursive: true });
await fs.writeFile("genezio.yaml", "test");

const promptSpy = vi.spyOn(inquirer, "prompt");

// Mock user selecting Local
promptSpy.mockResolvedValueOnce({ command: "local" });

// Run the super command
await genezioCommand();

// Check if inquirer.prompt was called with the correct arguments
expect(promptSpy).toHaveBeenCalledWith([
{
type: "list",
name: "command",
message: colors.magenta("Genezio project detected. What would you like to do?"),
choices: [
{
name: "Deploy your project (genezio deploy)",
value: "deploy",
},
{
name: "Start a Local server (genezio local)",
value: "local",
},
],
},
]);

// Check if the local environment was started
expect(startLocalEnvironment).toHaveBeenCalled();
});
});

0 comments on commit 705f265

Please sign in to comment.