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");
}
(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)) {
// this makes debugging easier, by only printing compiled output once
throw new Error('skipping test, already failed');
@ -105,95 +105,98 @@ describe("runtime", () => {
const window = env();
try {
// 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();
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();
}
};
};
global.window = window;
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 }, svelte); // eslint-disable-line no-console
throw err;
}
global.window = window;
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
window.SvelteComponent = SvelteComponent;
global.window = window;
const target = window.document.querySelector("main");
// Put the constructor on window for testing
window.SvelteComponent = SvelteComponent;
const warnings = [];
const warn = console.warn;
console.warn = warning => {
warnings.push(warning);
};
const target = window.document.querySelector("main");
const options = Object.assign({}, {
target,
hydrate,
data: config.data
}, config.options || {});
const warnings = [];
const warn = console.warn;
console.warn = warning => {
warnings.push(warning);
};
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) {
unintendedError = true;
throw new Error("Expected a runtime error");
}
console.warn = warn;
if (config.warnings) {
assert.deepEqual(warnings, config.warnings);
} else if (warnings.length) {
unintendedError = true;
throw new Error("Received unexpected warnings");
}
if (config.error) {
unintendedError = true;
throw new Error("Expected a runtime error");
}
if (config.html) {
assert.htmlEqual(target.innerHTML, config.html);
}
if (config.warnings) {
assert.deepEqual(warnings, config.warnings);
} else if (warnings.length) {
unintendedError = true;
throw new Error("Received unexpected warnings");
}
if (config.test) {
await 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;
}
}
if (config.html) {
assert.htmlEqual(target.innerHTML, config.html);
}
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>
`,
async test(assert, component, target) {
test(assert, component, target) {
fulfil(42);
await thePromise;
assert.htmlEqual(target.innerHTML, `
<p>the value is 42</p>
`);
return thePromise
.then(() => {
assert.htmlEqual(target.innerHTML, `
<p>the value is 42</p>
`);
let reject;
let reject;
thePromise = new Promise((f, r) => {
reject = r;
});
thePromise = new Promise((f, r) => {
reject = r;
});
component.set({
thePromise
});
component.set({
thePromise
});
assert.htmlEqual(target.innerHTML, `
<p>loading...</p>
`);
assert.htmlEqual(target.innerHTML, `
<p>loading...</p>
`);
reject(new Error('something broke'));
reject(new Error('something broke'));
try {
await thePromise;
} catch (err) {
// do nothing
}
assert.htmlEqual(target.innerHTML, `
<p>oh no! something broke</p>
`);
return thePromise.catch(() => {});
})
.then(() => {
assert.htmlEqual(target.innerHTML, `
<p>oh no! something broke</p>
`);
});
}
};
Loading…
Cancel
Save