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

@ -57,41 +57,26 @@ 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`, () => { (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 inline helpers test'); throw new Error('skipping inline helpers test');
} }
const cwd = path.resolve(`test/runtime/samples/${dir}`); const cwd = path.resolve(`test/runtime/samples/${dir}`);
let compiled;
compileOptions = config.compileOptions || {}; compileOptions = config.compileOptions || {};
compileOptions.shared = shared; compileOptions.shared = shared;
compileOptions.dev = config.dev; compileOptions.dev = config.dev;
// check that no ES2015+ syntax slipped in
if (!config.allowES2015) {
try { try {
const source = fs.readFileSync( const source = fs.readFileSync(
`test/runtime/samples/${dir}/main.html`, `test/runtime/samples/${dir}/main.html`,
"utf-8" "utf-8"
); );
compiled = svelte.compile(source, compileOptions); const { code } = 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 startIndex = code.indexOf("function create_main_fragment"); // may change! const startIndex = code.indexOf("function create_main_fragment"); // may change!
if (startIndex === -1) if (startIndex === -1)
throw new Error("missing create_main_fragment"); throw new Error("missing create_main_fragment");
@ -101,7 +86,7 @@ describe("runtime", () => {
acorn.parse(es5, { ecmaVersion: 5 }); acorn.parse(es5, { ecmaVersion: 5 });
} catch (err) { } catch (err) {
failed.add(dir); failed.add(dir);
showOutput(cwd, shared); // eslint-disable-line no-console showOutput(cwd, { shared }); // eslint-disable-line no-console
throw err; throw err;
} }
} }
@ -146,7 +131,7 @@ describe("runtime", () => {
try { try {
SvelteComponent = require(`./samples/${dir}/main.html`).default; SvelteComponent = require(`./samples/${dir}/main.html`).default;
} catch (err) { } catch (err) {
showOutput(cwd, shared); // eslint-disable-line no-console showOutput(cwd, { shared }); // eslint-disable-line no-console
throw err; throw err;
} }
@ -210,12 +195,12 @@ describe("runtime", () => {
config.error(assert, err); config.error(assert, err);
} else { } else {
failed.add(dir); failed.add(dir);
showOutput(cwd, shared); // eslint-disable-line no-console showOutput(cwd, { shared }); // eslint-disable-line no-console
throw err; throw err;
} }
}) })
.then(() => { .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 * as path from "path";
import { import {
addLineNumbers, showOutput,
loadConfig, loadConfig,
setupHtmlEqual, setupHtmlEqual,
svelte,
tryToLoadJson tryToLoadJson
} from "../helpers.js"; } from "../helpers.js";
@ -19,10 +18,6 @@ function tryToReadFile(file) {
} }
} }
function capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}
describe("ssr", () => { describe("ssr", () => {
before(() => { before(() => {
require(process.env.COVERAGE require(process.env.COVERAGE
@ -64,18 +59,7 @@ describe("ssr", () => {
error = e; error = e;
} }
if (show) { if (show) showOutput(dir, { generate: "ssr" });
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 (error) throw error; if (error) throw error;
fs.writeFileSync(`${dir}/_actual.html`, html); fs.writeFileSync(`${dir}/_actual.html`, html);
@ -102,22 +86,7 @@ describe("ssr", () => {
if (config["skip-ssr"]) return; if (config["skip-ssr"]) return;
(config.skip ? it.skip : config.solo ? it.only : it)(dir, () => { (config.skip ? it.skip : config.solo ? it.only : it)(dir, () => {
let compiled; const cwd = path.resolve("test/runtime/samples", dir);
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;
}
}
fs.readdirSync(`test/runtime/samples/${dir}`).forEach(file => { fs.readdirSync(`test/runtime/samples/${dir}`).forEach(file => {
const resolved = require.resolve(`../runtime/samples/${dir}/${file}`); const resolved = require.resolve(`../runtime/samples/${dir}/${file}`);
@ -134,7 +103,7 @@ describe("ssr", () => {
assert.htmlEqual(html, config.html); assert.htmlEqual(html, config.html);
} }
} catch (err) { } catch (err) {
console.log(addLineNumbers(compiled.code)); // eslint-disable-line no-console showOutput(cwd, { generate: "ssr" });
throw err; throw err;
} }
}); });

Loading…
Cancel
Save