one
Version:
One is a new React Framework that makes Vite serve both native and web.
72 lines (71 loc) • 3.4 kB
JavaScript
import { mkdtempSync } from "fs";
import { tmpdir } from "os";
import { join } from "path";
import FSExtra from "fs-extra";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { buildOutputPointerPath, resolveServeOutDir, writeBuildOutputPointer } from "./buildOutputPointer.native.js";
describe("build output pointer", function () {
var originalCwd = process.cwd();
var tempRoot = "";
beforeEach(function () {
tempRoot = mkdtempSync(join(tmpdir(), "one-build-pointer-"));
process.chdir(tempRoot);
});
afterEach(async function () {
process.chdir(originalCwd);
vi.restoreAllMocks();
await FSExtra.remove(tempRoot);
});
it("prefers an explicit serve outDir", async function () {
await FSExtra.ensureDir("build-out");
await FSExtra.writeJSON(join("build-out", "buildInfo.json"), {});
await writeBuildOutputPointer("build-out");
expect(await resolveServeOutDir("manual-out")).toBe("manual-out");
});
it("preserves serving from inside the output directory", async function () {
await FSExtra.writeJSON("buildInfo.json", {});
await FSExtra.ensureDir("build-out");
await FSExtra.writeJSON(join("build-out", "buildInfo.json"), {});
await writeBuildOutputPointer("build-out");
expect(await resolveServeOutDir()).toBe(".");
});
it("resolves the outDir written by build", async function () {
await FSExtra.ensureDir("build-out");
await FSExtra.writeJSON(join("build-out", "buildInfo.json"), {});
await writeBuildOutputPointer("build-out");
expect(await FSExtra.readJSON(buildOutputPointerPath)).toEqual({
outDir: "build-out"
});
expect(await resolveServeOutDir()).toBe("build-out");
});
it("falls back to dist when the pointer is stale", async function () {
var warn = vi.spyOn(console, "warn").mockImplementation(function () {});
await writeBuildOutputPointer("missing-out");
expect(await resolveServeOutDir()).toBe("dist");
expect(warn).toHaveBeenCalledTimes(2);
expect(warn.mock.calls[0][0]).toMatch(/build-pointer\.json points to/);
expect(warn.mock.calls[1][0]).toMatch(/no build-output pointer/);
});
it("falls back to dist when no pointer exists and warns about missing dist", async function () {
var warn = vi.spyOn(console, "warn").mockImplementation(function () {});
expect(await resolveServeOutDir()).toBe("dist");
expect(warn).toHaveBeenCalledOnce();
expect(warn.mock.calls[0][0]).toMatch(/no build-output pointer/);
});
it("does not warn when falling back to an existing dist/ build", async function () {
var warn = vi.spyOn(console, "warn").mockImplementation(function () {});
await FSExtra.ensureDir("dist");
await FSExtra.writeJSON(join("dist", "buildInfo.json"), {});
expect(await resolveServeOutDir()).toBe("dist");
expect(warn).not.toHaveBeenCalled();
});
it("warns when the pointer write fails", async function () {
var warn = vi.spyOn(console, "warn").mockImplementation(function () {});
var writeSpy = vi.spyOn(FSExtra, "writeJSON").mockRejectedValueOnce(new Error("permission denied"));
await writeBuildOutputPointer("build-out");
expect(writeSpy).toHaveBeenCalled();
expect(warn).toHaveBeenCalledOnce();
expect(warn.mock.calls[0][0]).toMatch(/could not write build-output pointer/);
});
});
//# sourceMappingURL=buildOutputPointer.test.native.js.map