UNPKG

@embeddable.com/sdk-core

Version:

Core Embeddable SDK module responsible for web-components bundling and publishing.

125 lines (103 loc) 3.5 kB
import { describe, it, expect, vi, beforeEach } from "vitest"; import { spawn } from "child_process"; import fs from "fs"; import path from "path"; // Mock the dependencies vi.mock("child_process"); vi.mock("fs"); vi.mock("path"); vi.mock("../lib/index.esm.js", () => ({ build: vi.fn().mockResolvedValue(undefined), login: vi.fn().mockResolvedValue(undefined), push: vi.fn().mockResolvedValue(undefined), dev: vi.fn().mockResolvedValue(undefined), defineConfig: vi.fn().mockResolvedValue(undefined), buildPackage: vi.fn().mockResolvedValue(undefined), })); describe("entryPoint", () => { let originalWarn; let main; beforeEach(async () => { // Reset all mocks before each test vi.clearAllMocks(); originalWarn = console.warn; console.warn = vi.fn(); fs.existsSync.mockReturnValue(true); // Mock process.on to avoid actually setting up process listeners vi.spyOn(process, "on").mockImplementation(() => process); vi.spyOn(process, "exit").mockImplementation(() => { throw new Error("Process.exit called with code 1"); }); // Mock path.join to return predictable paths path.join.mockImplementation((...args) => args.join("/")); // Mock spawn spawn.mockReturnValue({ on: vi.fn().mockImplementation((event, cb) => { if (event === "exit") { cb(0); // Success by default } }), }); // Set up process.argv for the test process.argv = ["node", "script.js", "defineConfig"]; // Dynamically import main after mocking process.exit const entryPoint = await import("./entryPoint.js"); main = entryPoint.main; }); afterEach(() => { console.warn = originalWarn; }); it("should run typescript check before build command", async () => { process.argv = ["node", "script.js", "build"]; await main(); expect(spawn).toHaveBeenCalledWith( expect.stringContaining("tsc"), ["--noEmit", "--pretty"], expect.any(Object), ); }); it("should skip typescript check with --force flag", async () => { process.argv = ["node", "script.js", "build", "--force"]; await main(); expect(spawn).not.toHaveBeenCalled(); expect(console.warn).toHaveBeenCalled(); }); it("should handle missing tsconfig.json", async () => { process.argv = ["node", "script.js", "build"]; fs.existsSync.mockImplementation((path) => { return !path.includes("tsconfig.json"); }); await main(); expect(spawn).not.toHaveBeenCalled(); }); it("should exit if typescript check fails", async () => { process.argv = ["node", "script.js", "build"]; spawn.mockReturnValue({ on: vi.fn().mockImplementation((event, cb) => { if (event === "exit") { cb(1); // Simulate failure } }), }); try { await main(); } catch (error) { expect(error.message).toBe("Process.exit called with code 1"); expect(console.warn).toHaveBeenCalled(); } }); it("should handle missing typescript compiler", async () => { process.argv = ["node", "script.js", "build"]; // Mock both tsconfig.json existence and tsc missing fs.existsSync.mockImplementation((filePath) => { if (filePath.includes("tsconfig.json")) return true; if (filePath.includes("tsc")) return false; return true; }); try { await main(); } catch (error) { expect(error.message).toBe("Process.exit called with code 1"); } }); });