forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
239 lines (197 loc) · 6.28 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import assert from "assert";
import chalk from 'chalk';
import * as path from "path";
import * as fs from "fs";
import * as acorn from "acorn";
import { transitionManager } from "../../shared.js";
import {
showOutput,
loadConfig,
loadSvelte,
env,
setupHtmlEqual,
spaces
} from "../helpers.js";
let svelte$;
let svelte;
let compileOptions = null;
let compile = null;
function getName(filename) {
const base = path.basename(filename).replace(".html", "");
return base[0].toUpperCase() + base.slice(1);
}
describe("runtime", () => {
before(() => {
svelte = loadSvelte(false);
svelte$ = loadSvelte(true);
require.extensions[".html"] = function(module, filename) {
const options = Object.assign(
{ filename, name: getName(filename), format: 'cjs' },
compileOptions
);
const { js } = compile(fs.readFileSync(filename, "utf-8"), options);
return module._compile(js.code, filename);
};
return setupHtmlEqual();
});
const failed = new Set();
function runTest(dir, shared, hydrate) {
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");
}
(config.skip ? it.skip : config.solo ? it.only : it)(`${dir} (${shared ? 'shared' : 'inline'} helpers${hydrate ? ', hydration' : ''})`, () => {
if (failed.has(dir)) {
// this makes debugging easier, by only printing compiled output once
throw new Error('skipping test, already failed');
}
compile = (config.preserveIdentifiers ? svelte : svelte$).compile;
const cwd = path.resolve(`test/runtime/samples/${dir}`);
global.document.title = '';
compileOptions = config.compileOptions || {};
compileOptions.shared = shared;
compileOptions.hydratable = hydrate;
compileOptions.store = !!config.store;
compileOptions.immutable = config.immutable;
compileOptions.skipIntroByDefault = config.skipIntroByDefault;
compileOptions.nestedTransitions = config.nestedTransitions;
Object.keys(require.cache)
.filter(x => x.endsWith(".html"))
.forEach(file => {
delete require.cache[file];
});
let SvelteComponent;
let unintendedError = null;
const window = env();
return Promise.resolve()
.then(() => {
// set of hacks to support transition tests
transitionManager.running = false;
transitionManager.transitions = [];
const raf = {
time: 0,
callback: null,
tick: now => {
raf.time = now;
if (raf.callback) raf.callback();
}
};
window.performance.now = () => raf.time;
global.requestAnimationFrame = cb => {
let called = false;
raf.callback = () => {
if (!called) {
called = true;
cb();
}
};
};
try {
SvelteComponent = require(`./samples/${dir}/main.html`);
} catch (err) {
showOutput(cwd, { shared, format: 'cjs', hydratable: hydrate, store: !!compileOptions.store, skipIntroByDefault: compileOptions.skipIntroByDefault, nestedTransitions: compileOptions.nestedTransitions }, compile); // eslint-disable-line no-console
throw err;
}
global.window = window;
// Put the constructor on window for testing
window.SvelteComponent = SvelteComponent;
const target = window.document.querySelector("main");
const warnings = [];
const warn = console.warn;
console.warn = warning => {
warnings.push(warning);
};
const options = Object.assign({}, {
target,
hydrate,
data: config.data,
store: (config.store !== true && config.store),
intro: config.intro
}, config.options || {});
const component = new SvelteComponent(options);
console.warn = warn;
if (config.error) {
unintendedError = true;
throw new Error("Expected a runtime error");
}
if (config.warnings) {
assert.deepEqual(warnings, config.warnings);
} else if (warnings.length) {
unintendedError = true;
throw new Error("Received unexpected warnings");
}
if (config.html) {
assert.htmlEqual(target.innerHTML, config.html);
}
if (config.test) {
return Promise.resolve(config.test(assert, component, target, window, raf)).then(() => {
component.destroy();
});
} else {
component.destroy();
assert.htmlEqual(target.innerHTML, "");
}
})
.catch(err => {
if (config.error && !unintendedError) {
if (typeof config.error === 'function') {
config.error(assert, err);
} else {
assert.equal(config.error, err.message);
}
} else {
failed.add(dir);
showOutput(cwd, {
shared,
format: 'cjs',
hydratable: hydrate,
store: !!compileOptions.store,
skipIntroByDefault: compileOptions.skipIntroByDefault,
nestedTransitions: compileOptions.nestedTransitions,
dev: compileOptions.dev
}, compile); // eslint-disable-line no-console
throw err;
}
})
.then(() => {
if (config.show) showOutput(cwd, { shared, format: 'cjs', hydratable: hydrate, store: !!compileOptions.store, skipIntroByDefault: compileOptions.skipIntroByDefault, nestedTransitions: compileOptions.nestedTransitions }, compile);
});
});
}
const shared = path.resolve("shared.js");
fs.readdirSync("test/runtime/samples").forEach(dir => {
runTest(dir, shared, false);
runTest(dir, shared, true);
runTest(dir, null, false);
});
it("fails if options.target is missing in dev mode", () => {
const { js } = svelte$.compile(`<div></div>`, {
format: "iife",
name: "SvelteComponent",
dev: true
});
const SvelteComponent = eval(
`(function () { ${js.code}; return SvelteComponent; }())`
);
assert.throws(() => {
new SvelteComponent();
}, /'target' is a required option/);
});
it("fails if options.hydrate is true but the component is non-hydratable", () => {
const { js } = svelte$.compile(`<div></div>`, {
format: "iife",
name: "SvelteComponent",
dev: true
});
const SvelteComponent = eval(
`(function () { ${js.code}; return SvelteComponent; }())`
);
assert.throws(() => {
new SvelteComponent({
target: {},
hydrate: true
});
}, /options.hydrate only works if the component was compiled with the `hydratable: true` option/);
});
});