Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 11, 2024
1 parent 53ccc33 commit 7df105c
Showing 1 changed file with 51 additions and 55 deletions.
106 changes: 51 additions & 55 deletions test/unstorage.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readFile } from "node:fs/promises";
import { resolve } from "pathe";
import { describe, it, expect, beforeAll } from "vitest";
import { createStorage } from "unstorage";
Expand All @@ -6,23 +7,15 @@ import githubDriver from "unstorage/drivers/github";
import httpDriver from "unstorage/drivers/http";
import { IPX, createIPX, unstorageToIPXStorage } from "../src";

function getFile() {
// eslint-disable-next-line unicorn/prefer-module
const driver = fsLiteDriver({ base: resolve(__dirname, "assets") });
const storage = createStorage({ driver });
return storage.getItemRaw("bliss.jpg");
}

type TestVariant = {
name: string;
setup: () => IPX | Promise<IPX>;
condition?: () => boolean;
};
const sampleImage = await readFile(
new URL("assets/bliss.jpg", import.meta.url),
);

const variants = [
const tests = [
{
name: "node fs lite",
setup: () => {
name: "node-fs",
skip: false,
setup() {
// eslint-disable-next-line unicorn/prefer-module
const driver = fsLiteDriver({ base: resolve(__dirname, "assets") });
const storage = createStorage({ driver });
Expand All @@ -31,79 +24,82 @@ const variants = [
},
{
name: "memory",
setup: async () => {
skip: false,
async setup() {
const storage = createStorage();
await storage.setItemRaw("bliss.jpg", await getFile());
await storage.setItemRaw("nested/bliss.jpg", await getFile());
await storage.setItemRaw("bliss.jpg", sampleImage);
await storage.setItemRaw("nested/bliss.jpg", sampleImage);
return createIPX({ storage: unstorageToIPXStorage(storage) });
},
},
{
name: "memory (prefix)",
setup: async () => {
name: "memory (prefixed)",
skip: false,
async setup() {
const storage = createStorage();
await storage.setItemRaw("images/bliss.jpg", await getFile());
await storage.setItemRaw("images/nested/bliss.jpg", await getFile());
await storage.setItemRaw("images/bliss.jpg", sampleImage);
await storage.setItemRaw("images/nested/bliss.jpg", sampleImage);
return createIPX({
storage: unstorageToIPXStorage(storage, { prefix: "images" }),
});
},
},
{
name: "github",
skip: !process.env.TEST_UNSTORAGE_GITHUB,
setup: () => {
const driver = githubDriver({ repo: "unjs/ipx", dir: "test/assets" });
const storage = createStorage({ driver });
return createIPX({ storage: unstorageToIPXStorage(storage) });
},
condition: () => process.env.TEST_UNSTORAGE_GITHUB === "true",
},
{
name: "http",
skip: !process.env.TEST_UNSTORAGE_HTTP,
setup: () => {
const driver = httpDriver({
base: "https://raw.githubusercontent.com/unjs/ipx/main/test/assets",
});
const storage = createStorage({ driver });
return createIPX({ storage: unstorageToIPXStorage(storage) });
},
condition: () => process.env.TEST_UNSTORAGE_HTTP === "true",
},
] satisfies TestVariant[];
] as const;

describe.each(variants)(`unstorage: $name`, ({ setup, condition }) => {
const shouldRun = condition ? condition() : true;
let ipx: IPX;
for (const test of tests) {
describe.skipIf(test.skip)(`unstorage:ipx:${test.name}`, () => {
let ipx: IPX;

beforeAll(async () => {
ipx = await setup();
});
beforeAll(async () => {
ipx = await test.setup();
});

it.runIf(shouldRun)("file found", async () => {
const source = await ipx("bliss.jpg");
const { data, format } = await source.process();
expect(data).toBeInstanceOf(Buffer);
expect(format).toBe("jpeg");
});
it("file found", async () => {
const source = await ipx("bliss.jpg");
const { data, format } = await source.process();
expect(data).toBeInstanceOf(Buffer);
expect(format).toBe("jpeg");
});

it.runIf(shouldRun)("file found nested", async () => {
const source = await ipx("nested/bliss.jpg");
const { data, format } = await source.process();
expect(data).toBeInstanceOf(Buffer);
expect(format).toBe("jpeg");
});
it("file found nested", async () => {
const source = await ipx("nested/bliss.jpg");
const { data, format } = await source.process();
expect(data).toBeInstanceOf(Buffer);
expect(format).toBe("jpeg");
});

it.runIf(shouldRun)("file not found", async () => {
const source = await ipx("unknown.jpg");
await expect(() => source.process()).rejects.toThrowError(
"Resource not found: /unknown.jpg",
);
});
it("file not found", async () => {
const source = await ipx("unknown.jpg");
await expect(() => source.process()).rejects.toThrowError(
"Resource not found: /unknown.jpg",
);
});

it.runIf(shouldRun)("invalid path", async () => {
const source = await ipx("*.jpg");
await expect(() => source.process()).rejects.toThrowError(
"Resource not found: /*.jpg",
);
it("invalid path", async () => {
const source = await ipx("*.jpg");
await expect(() => source.process()).rejects.toThrowError(
"Resource not found: /*.jpg",
);
});
});
});
}

0 comments on commit 7df105c

Please sign in to comment.