2017-06-03 04:04:36 +02:00
|
|
|
import assert from "assert";
|
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
2017-06-25 20:15:06 +02:00
|
|
|
import glob from 'glob';
|
2017-06-03 04:04:36 +02:00
|
|
|
|
|
|
|
import {
|
2017-06-11 21:22:36 +02:00
|
|
|
showOutput,
|
2017-06-03 04:04:36 +02:00
|
|
|
loadConfig,
|
|
|
|
setupHtmlEqual,
|
|
|
|
tryToLoadJson
|
|
|
|
} from "../helpers.js";
|
|
|
|
|
|
|
|
function tryToReadFile(file) {
|
2016-12-08 11:48:54 +01:00
|
|
|
try {
|
2017-06-03 04:04:36 +02:00
|
|
|
return fs.readFileSync(file, "utf-8");
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code !== "ENOENT") throw err;
|
2016-12-08 11:48:54 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
describe("ssr", () => {
|
|
|
|
before(() => {
|
2017-11-23 17:28:28 +01:00
|
|
|
require("../../ssr/register")({
|
2017-11-24 20:49:16 +01:00
|
|
|
extensions: ['.svelte', '.html'],
|
|
|
|
store: true
|
2017-11-23 17:28:28 +01:00
|
|
|
});
|
2016-12-16 22:17:31 +01:00
|
|
|
|
|
|
|
return setupHtmlEqual();
|
2016-12-08 11:48:54 +01:00
|
|
|
});
|
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
fs.readdirSync("test/server-side-rendering/samples").forEach(dir => {
|
|
|
|
if (dir[0] === ".") return;
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-04-04 16:21:19 +02:00
|
|
|
// add .solo to a sample directory name to only run that test, or
|
|
|
|
// .show to always show the output. or both
|
2017-06-03 04:04:36 +02:00
|
|
|
const solo = /\.solo/.test(dir);
|
2017-06-26 02:03:17 +02:00
|
|
|
const show = /\.show/.test(dir);
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
if (solo && process.env.CI) {
|
|
|
|
throw new Error("Forgot to remove `solo: true` from test");
|
2017-01-04 20:07:05 +01:00
|
|
|
}
|
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
(solo ? it.only : it)(dir, () => {
|
|
|
|
dir = path.resolve("test/server-side-rendering/samples", dir);
|
2017-06-26 02:03:17 +02:00
|
|
|
try {
|
2017-11-23 17:28:28 +01:00
|
|
|
let component;
|
|
|
|
|
|
|
|
const mainHtmlFile = `${dir}/main.html`;
|
|
|
|
const mainSvelteFile = `${dir}/main.svelte`;
|
|
|
|
if (fs.existsSync(mainHtmlFile)) {
|
|
|
|
component = require(mainHtmlFile);
|
|
|
|
} else if (fs.existsSync(mainSvelteFile)) {
|
|
|
|
component = require(mainSvelteFile);
|
|
|
|
}
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-06-26 02:03:17 +02:00
|
|
|
const expectedHtml = tryToReadFile(`${dir}/_expected.html`);
|
|
|
|
const expectedCss = tryToReadFile(`${dir}/_expected.css`) || "";
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-06-26 02:03:17 +02:00
|
|
|
const data = tryToLoadJson(`${dir}/data.json`);
|
2017-04-04 16:21:19 +02:00
|
|
|
|
2017-12-14 01:40:32 +01:00
|
|
|
const { html, css, head } = component.render(data);
|
2017-04-04 16:21:19 +02:00
|
|
|
|
2017-06-26 02:03:17 +02:00
|
|
|
fs.writeFileSync(`${dir}/_actual.html`, html);
|
2017-12-13 15:28:29 +01:00
|
|
|
if (css.code) fs.writeFileSync(`${dir}/_actual.css`, css.code);
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-06-25 17:24:24 +02:00
|
|
|
assert.htmlEqual(html, expectedHtml);
|
|
|
|
assert.equal(
|
2017-12-13 15:28:29 +01:00
|
|
|
css.code.replace(/^\s+/gm, ""),
|
2017-06-25 17:24:24 +02:00
|
|
|
expectedCss.replace(/^\s+/gm, "")
|
|
|
|
);
|
2017-06-26 02:03:17 +02:00
|
|
|
|
2017-12-14 01:40:32 +01:00
|
|
|
if (fs.existsSync(`${dir}/_expected-head.html`)) {
|
|
|
|
fs.writeFileSync(`${dir}/_actual-head.html`, head);
|
|
|
|
assert.htmlEqual(
|
|
|
|
head,
|
|
|
|
fs.readFileSync(`${dir}/_expected-head.html`, 'utf-8')
|
2017-12-13 15:52:04 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-06-26 02:03:17 +02:00
|
|
|
if (show) showOutput(dir, { generate: 'ssr' });
|
2017-06-25 17:24:24 +02:00
|
|
|
} catch (err) {
|
|
|
|
showOutput(dir, { generate: 'ssr' });
|
|
|
|
throw err;
|
|
|
|
}
|
2016-12-08 11:48:54 +01:00
|
|
|
});
|
|
|
|
});
|
2017-01-17 19:46:37 +01:00
|
|
|
|
|
|
|
// duplicate client-side tests, as far as possible
|
2017-06-03 04:04:36 +02:00
|
|
|
fs.readdirSync("test/runtime/samples").forEach(dir => {
|
|
|
|
if (dir[0] === ".") return;
|
2017-01-17 19:46:37 +01:00
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
const config = loadConfig(`./runtime/samples/${dir}/_config.js`);
|
2017-01-17 19:46:37 +01:00
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
if (config.solo && process.env.CI) {
|
|
|
|
throw new Error("Forgot to remove `solo: true` from test");
|
2017-01-17 19:46:37 +01:00
|
|
|
}
|
2017-01-31 23:35:03 +01:00
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
if (config["skip-ssr"]) return;
|
2017-01-17 19:46:37 +01:00
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
(config.skip ? it.skip : config.solo ? it.only : it)(dir, () => {
|
2017-06-11 21:22:36 +02:00
|
|
|
const cwd = path.resolve("test/runtime/samples", dir);
|
2017-01-17 19:46:37 +01:00
|
|
|
|
2017-06-25 20:15:06 +02:00
|
|
|
glob.sync('**/*.html', { cwd: `test/runtime/samples/${dir}` }).forEach(file => {
|
2017-06-03 04:04:36 +02:00
|
|
|
const resolved = require.resolve(`../runtime/samples/${dir}/${file}`);
|
|
|
|
delete require.cache[resolved];
|
2017-05-28 17:27:51 +02:00
|
|
|
});
|
2017-04-19 03:38:30 +02:00
|
|
|
|
2017-11-24 20:49:16 +01:00
|
|
|
require("../../ssr/register")({
|
|
|
|
store: !!config.store
|
|
|
|
});
|
|
|
|
|
2017-01-31 23:35:03 +01:00
|
|
|
try {
|
2017-08-29 14:02:47 +02:00
|
|
|
const component = require(`../runtime/samples/${dir}/main.html`);
|
2017-12-13 15:28:29 +01:00
|
|
|
const { html } = component.render(config.data, {
|
2017-11-24 20:49:16 +01:00
|
|
|
store: config.store
|
|
|
|
});
|
2017-01-17 19:46:37 +01:00
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
if (config.html) {
|
|
|
|
assert.htmlEqual(html, config.html);
|
2017-01-31 23:35:03 +01:00
|
|
|
}
|
2017-06-03 04:04:36 +02:00
|
|
|
} catch (err) {
|
2017-06-11 21:22:36 +02:00
|
|
|
showOutput(cwd, { generate: "ssr" });
|
2017-01-31 23:35:03 +01:00
|
|
|
throw err;
|
2017-01-17 19:46:37 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-12-08 11:48:54 +01:00
|
|
|
});
|