-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathindex.js
144 lines (115 loc) · 3.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import * as assert from "assert";
import * as fs from "fs";
import * as path from "path";
import * as glob from 'tiny-glob/sync.js';
import {
showOutput,
loadConfig,
setupHtmlEqual,
tryToLoadJson
} from "../helpers.js";
function tryToReadFile(file) {
try {
return fs.readFileSync(file, "utf-8");
} catch (err) {
if (err.code !== "ENOENT") throw err;
return null;
}
}
const sveltePath = process.cwd().split('\\').join('/');
describe("ssr", () => {
before(() => {
require("../../register")({
extensions: ['.svelte', '.html'],
sveltePath
});
return setupHtmlEqual();
});
fs.readdirSync("test/server-side-rendering/samples").forEach(dir => {
if (dir[0] === ".") return;
// add .solo to a sample directory name to only run that test, or
// .show to always show the output. or both
const solo = /\.solo/.test(dir);
const show = /\.show/.test(dir);
if (solo && process.env.CI) {
throw new Error("Forgot to remove `solo: true` from test");
}
(solo ? it.only : it)(dir, () => {
dir = path.resolve("test/server-side-rendering/samples", dir);
try {
const Component = require(`${dir}/main.svelte`).default;
const expectedHtml = tryToReadFile(`${dir}/_expected.html`);
const expectedCss = tryToReadFile(`${dir}/_expected.css`) || "";
const props = tryToLoadJson(`${dir}/data.json`) || undefined;
const rendered = Component.render(props);
const { html, css, head } = rendered;
fs.writeFileSync(`${dir}/_actual.html`, html);
if (css.code) fs.writeFileSync(`${dir}/_actual.css`, css.code);
assert.htmlEqual(html, expectedHtml);
assert.equal(
css.code.replace(/^\s+/gm, ""),
expectedCss.replace(/^\s+/gm, "")
);
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')
);
}
if (show) showOutput(dir, { generate: 'ssr' });
} catch (err) {
showOutput(dir, { generate: 'ssr' });
throw err;
}
});
});
// duplicate client-side tests, as far as possible
fs.readdirSync("test/runtime/samples").forEach(dir => {
if (dir[0] === ".") return;
const config = loadConfig(`./runtime/samples/${dir}/_config.js`);
if (config.solo && process.env.CI) {
throw new Error("Forgot to remove `solo: true` from test");
}
if (config.skip_if_ssr) return;
(config.skip ? it.skip : config.solo ? it.only : it)(dir, () => {
const cwd = path.resolve("test/runtime/samples", dir);
Object.keys(require.cache)
.filter(x => x.endsWith('.svelte'))
.forEach(file => {
delete require.cache[file];
});
const compileOptions = Object.assign({ sveltePath }, config.compileOptions, {
generate: 'ssr'
});
require("../../register")(compileOptions);
try {
if (config.before_test) config.before_test();
const Component = require(`../runtime/samples/${dir}/main.svelte`).default;
const { html } = Component.render(config.props, {
store: (config.store !== true) && config.store
});
if (config.ssrHtml) {
assert.htmlEqual(html, config.ssrHtml);
} else if (config.html) {
assert.htmlEqual(html, config.html);
}
if (config.after_test) config.after_test();
if (config.show) {
showOutput(cwd, compileOptions);
}
} catch (err) {
if (config.error) {
if (typeof config.error === 'function') {
config.error(assert, err);
} else {
assert.equal(config.error, err.message);
}
} else {
showOutput(cwd, compileOptions);
throw err;
}
}
});
});
});