forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (107 loc) · 2.92 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
import * as assert from 'assert';
import * as path from 'path';
import * as fs from 'fs';
import {
showOutput,
loadConfig,
loadSvelte,
env,
setupHtmlEqual,
shouldUpdateExpected
} from '../helpers.js';
let compileOptions = null;
const sveltePath = process.cwd();
describe('hydration', () => {
before(() => {
const svelte = loadSvelte();
require.extensions['.svelte'] = function(module, filename) {
const options = Object.assign(
{
filename,
hydratable: true,
format: 'cjs',
sveltePath
},
compileOptions
);
const { js } = svelte.compile(fs.readFileSync(filename, 'utf-8'), options);
return module._compile(js.code, filename);
};
return setupHtmlEqual();
});
function runTest(dir) {
if (dir[0] === '.') return;
const config = loadConfig(`./hydration/samples/${dir}/_config.js`);
const solo = config.solo || /\.solo/.test(dir);
if (solo && process.env.CI) {
throw new Error('Forgot to remove `solo: true` from test');
}
(config.skip ? it.skip : solo ? it.only : it)(dir, () => {
const cwd = path.resolve(`${__dirname}/samples/${dir}`);
compileOptions = config.compileOptions || {};
const window = env();
try {
global.window = window;
let SvelteComponent;
try {
SvelteComponent = require(`${cwd}/main.svelte`).default;
} catch (err) {
throw err;
}
const target = window.document.body;
const head = window.document.head;
target.innerHTML = fs.readFileSync(`${cwd}/_before.html`, 'utf-8');
let before_head;
try {
before_head = fs.readFileSync(`${cwd}/_before_head.html`, 'utf-8');
head.innerHTML = before_head;
} catch (err) {}
const snapshot = config.snapshot ? config.snapshot(target) : {};
const component = new SvelteComponent({
target,
hydrate: true,
props: config.props
});
try {
assert.htmlEqual(target.innerHTML, fs.readFileSync(`${cwd}/_after.html`, 'utf-8'));
} catch (error) {
if (shouldUpdateExpected()) {
fs.writeFileSync(`${cwd}/_after.html`, target.innerHTML);
console.log(`Updated ${cwd}/_after.html.`);
} else {
throw error;
}
}
if (before_head) {
try {
assert.htmlEqual(head.innerHTML, fs.readFileSync(`${cwd}/_after_head.html`, 'utf-8'));
} catch (error) {
if (shouldUpdateExpected()) {
fs.writeFileSync(`${cwd}/_after_head.html`, head.innerHTML);
console.log(`Updated ${cwd}/_after_head.html.`);
} else {
throw error;
}
}
}
if (config.test) {
config.test(assert, target, snapshot, component, window);
} else {
component.$destroy();
assert.equal(target.innerHTML, '');
}
} catch (err) {
showOutput(cwd, {
hydratable: true
});
throw err;
}
if (config.show) showOutput(cwd, {
hydratable: true
});
});
}
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
runTest(dir, null);
});
});