Skip to content

Commit

Permalink
Added basic test with vite
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Jun 6, 2024
1 parent 3366c08 commit b8bfaa9
Show file tree
Hide file tree
Showing 5 changed files with 576 additions and 11 deletions.
5 changes: 5 additions & 0 deletions __tests__/fixtures/dummy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!doctype html>
<html>
<head></head>
<body></body>
</html>
15 changes: 14 additions & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { compileRollup } from "./utils";
import { compileRollup, compileVite } from "./utils";

test("Basic rollup", async () => {
const htaccess = await compileRollup({
Expand All @@ -12,3 +12,16 @@ test("Basic rollup", async () => {
});
expect(htaccess).toBe("Header set X-Frame-Options DENY\n");
});

test.only("Basic vite", async () => {
const htaccess = await compileVite({
headers: [
{
header: "X-Frame-Options",
action: "set",
value: "deny",
},
],
});
expect(htaccess).toBe("Header set X-Frame-Options DENY\n");
});
35 changes: 27 additions & 8 deletions __tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
import { type OutputAsset, rollup } from "rollup";
import { type OutputAsset, rollup, type RollupOutput } from "rollup";
import { build } from "vite";

import htaccess, { type Options } from "../src";

function extractFileContents(output: RollupOutput): string {
const htaccessFiles = output.output.filter(
(file): file is OutputAsset =>
file.type === "asset" && file.fileName === ".htaccess",
);
expect(htaccessFiles).toHaveLength(1);
return htaccessFiles[0].source.toString();
}

export async function compileRollup(
options?: Partial<Options>,
): Promise<string> {
const bundle = await rollup({
input: "__tests__/fixtures/dummy.js",
plugins: [htaccess(options)],
});
const { output } = await bundle.generate({});
const htaccessFiles = output.filter(
(file): file is OutputAsset =>
file.type === "asset" && file.fileName === ".htaccess",
);
expect(htaccessFiles).toHaveLength(1);
return htaccessFiles[0].source.toString();
const output = await bundle.generate({});
return extractFileContents(output);
}

export async function compileVite(options?: Partial<Options>): Promise<string> {
const output = (await build({
build: {
rollupOptions: {
input: {
app: "__tests__/fixtures/dummy.html",
},
},
},
plugins: [htaccess(options)],
})) as Array<RollupOutput> | RollupOutput;
return extractFileContents(Array.isArray(output) ? output[0] : output);
}
Loading

0 comments on commit b8bfaa9

Please sign in to comment.