You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/packages/svelte/tests/runtime-browser/test-ssr.ts

56 lines
1.7 KiB

// @vitest-environment jsdom
// Yes it's an SSR test, but we need the env to compare html
// This is in its own file because if we had the jsdom environment for the other playwright browser tests,
// esbuild would choke on it
import * as fs from 'node:fs';
import * as path from 'node:path';
import { setImmediate } from 'node:timers/promises';
import { render } from 'svelte/server';
import { compile_directory } from '../helpers';
import { assert_html_equal_with_options } from '../html_equal';
import { suite } from '../suite';
import { describe } from 'vitest';
export async function run_ssr_test(
config: ReturnType<typeof import('./assert').test>,
test_dir: string
) {
try {
compile_directory(test_dir, 'server', {
...config.compileOptions,
runes: test_dir.includes('runtime-runes')
});
const Component = (await import(`${test_dir}/_output/server/main.svelte.js`)).default;
const { html } = render(Component, { props: config.props || {} });
fs.writeFileSync(`${test_dir}/_output/rendered.html`, html);
if (config.ssrHtml) {
assert_html_equal_with_options(html, config.ssrHtml, {
preserveComments: config.compileOptions?.preserveComments
});
} else if (config.html) {
assert_html_equal_with_options(html, config.html, {
preserveComments: config.compileOptions?.preserveComments
});
}
} catch (err: any) {
err.stack += `\n\ncmd-click: ${path.relative(process.cwd(), test_dir)}/main.svelte`;
throw err;
}
// wait for vitest to report progress
await setImmediate();
}
const { run } = suite<ReturnType<typeof import('./assert').test>>(async (config, test_dir) => {
if (config.skip_if_ssr) return;
await run_ssr_test(config, test_dir);
});
describe('runtime-browser (ssr)', async () => {
await run(__dirname);
});