ditch async/await in tests, so that they run in node 6

pull/952/head
Rich Harris 7 years ago
parent 8a0813e96b
commit ccef13a2d5

@ -51,7 +51,7 @@ describe("runtime", () => {
throw new Error("Forgot to remove `solo: true` from test"); throw new Error("Forgot to remove `solo: true` from test");
} }
(config.skip ? it.skip : config.solo ? it.only : it)(`${dir} (${shared ? 'shared' : 'inline'} helpers)`, async () => { (config.skip ? it.skip : config.solo ? it.only : it)(`${dir} (${shared ? 'shared' : 'inline'} helpers)`, () => {
if (failed.has(dir)) { if (failed.has(dir)) {
// this makes debugging easier, by only printing compiled output once // this makes debugging easier, by only printing compiled output once
throw new Error('skipping test, already failed'); throw new Error('skipping test, already failed');
@ -105,95 +105,98 @@ describe("runtime", () => {
const window = env(); const window = env();
try { return Promise.resolve()
// set of hacks to support transition tests .then(() => {
transitionManager.running = false; // set of hacks to support transition tests
transitionManager.transitions = []; transitionManager.running = false;
transitionManager.transitions = [];
const raf = {
time: 0, const raf = {
callback: null, time: 0,
tick: now => { callback: null,
raf.time = now; tick: now => {
if (raf.callback) raf.callback(); 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();
} }
}; };
}; window.performance = { now: () => raf.time };
global.requestAnimationFrame = cb => {
global.window = window; let called = false;
raf.callback = () => {
if (!called) {
called = true;
cb();
}
};
};
try { global.window = window;
SvelteComponent = require(`./samples/${dir}/main.html`);
} catch (err) {
showOutput(cwd, { shared, format: 'cjs', hydratable: hydrate }, svelte); // eslint-disable-line no-console
throw err;
}
global.window = window; try {
SvelteComponent = require(`./samples/${dir}/main.html`);
} catch (err) {
showOutput(cwd, { shared, format: 'cjs', hydratable: hydrate }, svelte); // eslint-disable-line no-console
throw err;
}
// Put the constructor on window for testing global.window = window;
window.SvelteComponent = SvelteComponent;
const target = window.document.querySelector("main"); // Put the constructor on window for testing
window.SvelteComponent = SvelteComponent;
const warnings = []; const target = window.document.querySelector("main");
const warn = console.warn;
console.warn = warning => {
warnings.push(warning);
};
const options = Object.assign({}, { const warnings = [];
target, const warn = console.warn;
hydrate, console.warn = warning => {
data: config.data warnings.push(warning);
}, config.options || {}); };
const component = new SvelteComponent(options); const options = Object.assign({}, {
target,
hydrate,
data: config.data
}, config.options || {});
console.warn = warn; const component = new SvelteComponent(options);
if (config.error) { console.warn = warn;
unintendedError = true;
throw new Error("Expected a runtime error");
}
if (config.warnings) { if (config.error) {
assert.deepEqual(warnings, config.warnings); unintendedError = true;
} else if (warnings.length) { throw new Error("Expected a runtime error");
unintendedError = true; }
throw new Error("Received unexpected warnings");
}
if (config.html) { if (config.warnings) {
assert.htmlEqual(target.innerHTML, config.html); assert.deepEqual(warnings, config.warnings);
} } else if (warnings.length) {
unintendedError = true;
throw new Error("Received unexpected warnings");
}
if (config.test) { if (config.html) {
await config.test(assert, component, target, window, raf); assert.htmlEqual(target.innerHTML, config.html);
} else { }
component.destroy();
assert.equal(target.innerHTML, "");
}
} catch (err) {
if (config.error && !unintendedError) {
config.error(assert, err);
} else {
failed.add(dir);
showOutput(cwd, { shared, format: 'cjs', hydratable: hydrate }, svelte); // eslint-disable-line no-console
throw err;
}
}
if (config.show) showOutput(cwd, { shared, format: 'cjs', hydratable: hydrate }, svelte); if (config.test) {
return config.test(assert, component, target, window, raf);
} else {
component.destroy();
assert.equal(target.innerHTML, "");
}
})
.catch(err => {
if (config.error && !unintendedError) {
config.error(assert, err);
} else {
failed.add(dir);
showOutput(cwd, { shared, format: 'cjs', hydratable: hydrate }, svelte); // eslint-disable-line no-console
throw err;
}
})
.then(() => {
if (config.show) showOutput(cwd, { shared, format: 'cjs', hydratable: hydrate }, svelte);
});
}); });
} }

@ -13,38 +13,37 @@ export default {
<p>loading...</p> <p>loading...</p>
`, `,
async test(assert, component, target) { test(assert, component, target) {
fulfil(42); fulfil(42);
await thePromise;
assert.htmlEqual(target.innerHTML, ` return thePromise
<p>the value is 42</p> .then(() => {
`); assert.htmlEqual(target.innerHTML, `
<p>the value is 42</p>
`);
let reject; let reject;
thePromise = new Promise((f, r) => { thePromise = new Promise((f, r) => {
reject = r; reject = r;
}); });
component.set({ component.set({
thePromise thePromise
}); });
assert.htmlEqual(target.innerHTML, ` assert.htmlEqual(target.innerHTML, `
<p>loading...</p> <p>loading...</p>
`); `);
reject(new Error('something broke')); reject(new Error('something broke'));
try { return thePromise.catch(() => {});
await thePromise; })
} catch (err) { .then(() => {
// do nothing assert.htmlEqual(target.innerHTML, `
} <p>oh no! something broke</p>
`);
assert.htmlEqual(target.innerHTML, ` });
<p>oh no! something broke</p>
`);
} }
}; };
Loading…
Cancel
Save