Skip to content

Commit

Permalink
test: Add simple tests for output files & local fetch patch
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Dec 9, 2024
1 parent c823a99 commit 0bf98c5
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/plugins/prerender-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
},
// Injects window checks into Vite's preload helper & modulepreload polyfill
transform(code, id) {
if (id.includes('vite/preload-helper')) {
if (id.includes(preloadHelperId)) {
// Injects a window check into Vite's preload helper, instantly resolving
// the module rather than attempting to add a <link> to the document.
const s = new MagicString(code);
Expand Down
1 change: 0 additions & 1 deletion tests/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as assert from 'uvu/assert';
import { setupTest, teardownTest, loadFixture, viteBuild } from './lib/lifecycle.js';
import { getOutputFile, outputFileExists, writeFixtureFile } from './lib/utils.js';


const writeConfig = async (dir, content) => writeFixtureFile(dir, 'vite.config.js', content);

let env;
Expand Down
24 changes: 24 additions & 0 deletions tests/features.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';

import { setupTest, teardownTest, loadFixture, viteBuild } from './lib/lifecycle.js';
import { getOutputFile } from './lib/utils.js';

let env;
test.before.each(async () => {
env = await setupTest();
});

test.after.each(async () => {
await teardownTest(env);
});

test('Should support isomorphic fetch', async () => {
await loadFixture('features/local-fetch', env);
await viteBuild(env.tmp.path);

const prerenderedHtml = await getOutputFile(env.tmp.path, 'index.html');
assert.match(prerenderedHtml, '<body>Local fetch works');
});

test.run();
9 changes: 9 additions & 0 deletions tests/fixtures/features/local-fetch/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<script prerender type="module" src="/src/index.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Local fetch works
4 changes: 4 additions & 0 deletions tests/fixtures/features/local-fetch/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function prerender() {
const res = await fetch('/local-fetch-test.txt')
return await res.text();
}
6 changes: 6 additions & 0 deletions tests/fixtures/features/local-fetch/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from 'vite';
import { vitePrerenderPlugin } from 'vite-prerender-plugin';

export default defineConfig({
plugins: [vitePrerenderPlugin()],
});
4 changes: 3 additions & 1 deletion tests/fixtures/simple/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export async function prerender() {}
export async function prerender() {
return `<h1>Simple Test Result</h1>`;
}
32 changes: 32 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import path from 'node:path';
import { promises as fs } from 'node:fs';

import { setupTest, teardownTest, loadFixture, viteBuild } from './lib/lifecycle.js';
import { getOutputFile } from './lib/utils.js';

let env;
test.before.each(async () => {
env = await setupTest();
});

test.after.each(async () => {
await teardownTest(env);
});

test('Should prerender and output correct file structure', async () => {
await loadFixture('simple', env);
await viteBuild(env.tmp.path);

const prerenderedHtml = await getOutputFile(env.tmp.path, 'index.html');
assert.match(prerenderedHtml, '<h1>Simple Test Result</h1>');

const outDir = path.join(env.tmp.path, 'dist');
const outDirAssets = path.join(outDir, 'assets');

assert.equal((await fs.readdir(outDir)).length, 2);
assert.equal((await fs.readdir(outDirAssets)).length, 1);
});

test.run();

0 comments on commit 0bf98c5

Please sign in to comment.