Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lilioid committed Sep 13, 2022
1 parent a89286e commit 9dbcdda
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package-lock.json
node_modules
dist
vite_dist
coverage

# Editor directories and files
.vscode/*
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"build:bundle_patch_script": "esbuild --bundle --platform=node --outfile=dist/assets/patch_runtime_config.js src/script_entrypoint.ts",
"build:bundle_patch_bin:gnu_linux": "pkg --public --targets node16-linux-x64 -o dist/assets/patch_runtime_config.gnu_linux.bin dist/assets/patch_runtime_config.js",
"build:bundle_patch_bin:alpine": "pkg --public --targets node16-alpine-x64 -o dist/assets/patch_runtime_config.alpine.bin dist/assets/patch_runtime_config.js",
"clean": "rm -rf dist"
"clean": "rm -rf dist",
"test": "vitest run"
},
"files": [
"dist",
Expand All @@ -38,6 +39,7 @@
"@types/node": "^18.7.14",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"@vitest/coverage-c8": "^0.23.2",
"argparse": "^2.0.1",
"dotenv": "^16.0.2",
"dotenv-expand": "^9.0.0",
Expand All @@ -49,6 +51,7 @@
"prettier": "^2.7.1",
"typescript": "^4.8.2",
"vite": "^3.1.0",
"vite-plugin-inspect": "^0.6.1"
"vite-plugin-inspect": "^0.6.1",
"vitest": "^0.23.2"
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ function pluginRuntimeConfig(options?: PluginOptions): Plugin {
};
}

export { PluginOptions };
export { PluginOptions as Options, pluginRuntimeConfig as runtimeConfig };
export default pluginRuntimeConfig;
35 changes: 35 additions & 0 deletions test/__snapshots__/patch_html.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Vitest Snapshot v1

exports[`replaceCompleteConfig 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
<meta charset=\\"UTF-8\\" />
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
<meta name=\\"test\\" content=\\"{{ VITE_TEST_IN_HEAD }}\\" />
<title>Vite Test html</title>
</head>
<body>
<h1>Hello {{ VITE_TEST_PERSON_NAME }}</h1>
<script type=\\"application/ecmascript\\">window.config = Object.freeze({\\"VITE_TEST_IN_HEAD\\":\\"test-in-head-value\\",\\"VITE_TEST_PERSON_NAME\\":\\"max mustermann\\"});</script>
</body>
</html>
"
`;
exports[`replaceIndividualKeys 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
<meta charset=\\"UTF-8\\" />
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
<meta name=\\"test\\" content=\\"test-in-head-value\\" />
<title>Vite Test html</title>
</head>
<body>
<h1>Hello max mustermann</h1>
<script type=\\"application/ecmascript\\">window.config = Object.freeze({% VITE_RT_CONFIG %});</script>
</body>
</html>
"
`;
52 changes: 52 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { EmittedFile } from "rollup";
import { test, expect, vi, beforeAll } from "vitest";
import { runtimeConfig } from "@/index";

const mockPluginContext = {
emitFile: (_emittedFile: EmittedFile) => {},
};

type MockHook = (this: typeof mockPluginContext, ...args: any) => Promise<void>;

beforeAll(() => {
vi.spyOn(mockPluginContext, "emitFile");
vi.mock("../src/scripts", () => {
return {
readPatchRuntimeConfigScript() {
return Promise.resolve("test_patch_script");
},
readPatchRuntimeConfigBinary(target: string) {
return Promise.resolve(`test_patch_${target}_binary`);
},
};
});
});

test.concurrent("generateBundle hook", async () => {
const plugin = runtimeConfig({
emitPatchScript: true,
emitGnuLinuxPatchBinary: true,
emitAlpinePatchBinary: true,
});
await (plugin.generateBundle as MockHook).call(mockPluginContext);
expect(mockPluginContext.emitFile).toHaveBeenCalledWith(<EmittedFile>{
type: "asset",
fileName: "patch_runtime_config.js",
source: "test_patch_script",
});
expect(mockPluginContext.emitFile).toHaveBeenCalledWith(<EmittedFile>{
type: "asset",
fileName: "patch_runtime_config.gnu_linux.bin",
source: "test_patch_gnu_linux_binary",
});
expect(mockPluginContext.emitFile).toHaveBeenCalledWith(<EmittedFile>{
type: "asset",
fileName: "patch_runtime_config.alpine.bin",
source: "test_patch_alpine_binary",
});
});

test.concurrent("configResolved hook", () => {
const plugin = runtimeConfig();
(plugin.configResolved as MockHook).call(mockPluginContext);
});
27 changes: 27 additions & 0 deletions test/patch_html.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { test, expect, beforeAll } from "vitest";
import { readFile } from "node:fs/promises";
import * as Path from "path";
import * as patch_html from "@/patch_html";

let html: string;
let mock_cfg: patch_html.ConfigLike = {
envPrefix: "VITE_",
env: {
VITE_TEST_IN_HEAD: "test-in-head-value",
VITE_TEST_PERSON_NAME: "max mustermann",
},
};

beforeAll(async () => {
html = await readFile(Path.join(__dirname, "test.html"), {
encoding: "utf8",
});
});

test.concurrent("replaceIndividualKeys", () => {
expect(patch_html.replaceIndividualKeys(html, mock_cfg)).toMatchSnapshot();
});

test.concurrent("replaceCompleteConfig", () => {
expect(patch_html.replaceCompleteConfig(html, mock_cfg)).toMatchSnapshot();
});
15 changes: 15 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="test" content="{{ VITE_TEST_IN_HEAD }}" />
<title>Vite Test html</title>
</head>
<body>
<h1>Hello {{ VITE_TEST_PERSON_NAME }}</h1>
<script type="application/ecmascript">
window.config = Object.freeze({% VITE_RT_CONFIG %});
</script>
</body>
</html>
8 changes: 5 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"moduleResolution": "Node",
"skipLibCheck": true,
"strict": true,
"allowSyntheticDefaultImports": true
},
"references": []
"allowSyntheticDefaultImports": true,
"paths": {
"@/*": ["./src/*"]
}
}
}
17 changes: 17 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
resolve: {
alias: {
"@": "src/",
},
},
test: {
coverage: {
enabled: true,
provider: "istanbul",
all: true,
include: ["src/**/*.ts"],
},
},
});

0 comments on commit 9dbcdda

Please sign in to comment.