more useful presentation of failing SSR code

pull/627/head
Rich Harris 8 years ago
parent 6eb80e3732
commit 5c53f5b6a2

@ -2,6 +2,7 @@ import jsdom from 'jsdom';
import assert from 'assert';
import glob from 'glob';
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import * as consoleGroup from 'console-group';
@ -162,13 +163,17 @@ export function addLineNumbers(code) {
.join('\n');
}
export function showOutput(cwd, shared) {
function capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}
export function showOutput(cwd, options) {
glob.sync('**/*.html', { cwd }).forEach(file => {
const { code } = svelte.compile(
fs.readFileSync(`${cwd}/${file}`, 'utf-8'),
{
shared
}
Object.assign(options, {
name: capitalize(file.slice(0, -path.extname(file).length))
})
);
console.log( // eslint-disable-line no-console

@ -57,41 +57,26 @@ 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`, () => {
(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 inline helpers test');
}
const cwd = path.resolve(`test/runtime/samples/${dir}`);
let compiled;
compileOptions = config.compileOptions || {};
compileOptions.shared = shared;
compileOptions.dev = config.dev;
// check that no ES2015+ syntax slipped in
if (!config.allowES2015) {
try {
const source = fs.readFileSync(
`test/runtime/samples/${dir}/main.html`,
"utf-8"
);
compiled = svelte.compile(source, compileOptions);
} catch (err) {
if (config.compileError) {
config.compileError(err);
return;
} else {
failed.add(dir);
showOutput(cwd, shared);
throw err;
}
}
const { code } = compiled;
// check that no ES2015+ syntax slipped in
if (!config.allowES2015) {
try {
const { code } = svelte.compile(source, compileOptions);
const startIndex = code.indexOf("function create_main_fragment"); // may change!
if (startIndex === -1)
throw new Error("missing create_main_fragment");
@ -101,7 +86,7 @@ describe("runtime", () => {
acorn.parse(es5, { ecmaVersion: 5 });
} catch (err) {
failed.add(dir);
showOutput(cwd, shared); // eslint-disable-line no-console
showOutput(cwd, { shared }); // eslint-disable-line no-console
throw err;
}
}
@ -146,7 +131,7 @@ describe("runtime", () => {
try {
SvelteComponent = require(`./samples/${dir}/main.html`).default;
} catch (err) {
showOutput(cwd, shared); // eslint-disable-line no-console
showOutput(cwd, { shared }); // eslint-disable-line no-console
throw err;
}
@ -210,12 +195,12 @@ describe("runtime", () => {
config.error(assert, err);
} else {
failed.add(dir);
showOutput(cwd, shared); // eslint-disable-line no-console
showOutput(cwd, { shared }); // eslint-disable-line no-console
throw err;
}
})
.then(() => {
if (config.show) showOutput(cwd, shared);
if (config.show) showOutput(cwd, { shared });
});
});
}

@ -3,10 +3,9 @@ import * as fs from "fs";
import * as path from "path";
import {
addLineNumbers,
showOutput,
loadConfig,
setupHtmlEqual,
svelte,
tryToLoadJson
} from "../helpers.js";
@ -19,10 +18,6 @@ function tryToReadFile(file) {
}
}
function capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}
describe("ssr", () => {
before(() => {
require(process.env.COVERAGE
@ -64,18 +59,7 @@ describe("ssr", () => {
error = e;
}
if (show) {
fs.readdirSync(dir).forEach(file => {
if (file[0] === "_") return;
const source = fs.readFileSync(`${dir}/${file}`, "utf-8");
const name = capitalize(file.slice(0, -path.extname(file).length));
const { code } = svelte.compile(source, { generate: "ssr", name });
console.group(file);
console.log(addLineNumbers(code));
console.groupEnd();
});
}
if (show) showOutput(dir, { generate: "ssr" });
if (error) throw error;
fs.writeFileSync(`${dir}/_actual.html`, html);
@ -102,22 +86,7 @@ describe("ssr", () => {
if (config["skip-ssr"]) return;
(config.skip ? it.skip : config.solo ? it.only : it)(dir, () => {
let compiled;
try {
const source = fs.readFileSync(
`test/runtime/samples/${dir}/main.html`,
"utf-8"
);
compiled = svelte.compile(source, { generate: "ssr" });
} catch (err) {
if (config.compileError) {
config.compileError(err);
return;
} else {
throw err;
}
}
const cwd = path.resolve("test/runtime/samples", dir);
fs.readdirSync(`test/runtime/samples/${dir}`).forEach(file => {
const resolved = require.resolve(`../runtime/samples/${dir}/${file}`);
@ -134,7 +103,7 @@ describe("ssr", () => {
assert.htmlEqual(html, config.html);
}
} catch (err) {
console.log(addLineNumbers(compiled.code)); // eslint-disable-line no-console
showOutput(cwd, { generate: "ssr" });
throw err;
}
});

Loading…
Cancel
Save