Better error output in parser tests (#9491)

This commit is contained in:
Daniel Tschinder 2019-02-11 02:13:24 -08:00 committed by GitHub
parent 3a9743fce4
commit d349b74a4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 6 deletions

View File

@ -29,6 +29,7 @@
}, },
"devDependencies": { "devDependencies": {
"@babel/helper-fixtures": "^7.2.0", "@babel/helper-fixtures": "^7.2.0",
"@babel/code-frame": "^7.0.0",
"charcodes": "0.1.0", "charcodes": "0.1.0",
"unicode-11.0.0": "^0.7.8" "unicode-11.0.0": "^0.7.8"
}, },

View File

@ -1,7 +1,46 @@
import { multiple as getFixtures } from "@babel/helper-fixtures"; import { multiple as getFixtures } from "@babel/helper-fixtures";
import { codeFrameColumns } from "@babel/code-frame";
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
const rootPath = path.join(__dirname, "../../../..");
class FixtureError extends Error {
constructor(previousError, fixturePath, code) {
super(previousError.message);
const messageLines = (previousError.message.match(/\n/g) || []).length + 1;
let fixtureStackFrame = "";
if (previousError.loc) {
fixtureStackFrame =
codeFrameColumns(
code,
{
start: {
line: previousError.loc.line,
column: previousError.loc.column + 1,
},
},
{ highlightCode: true },
) +
"\n" +
`at fixture (${fixturePath}:${previousError.loc.line}:${previousError
.loc.column + 1})\n`;
}
this.stack =
previousError.constructor.name +
": " +
previousError.message +
"\n" +
fixtureStackFrame +
previousError.stack
.split("\n")
.slice(messageLines)
.join("\n");
}
}
export function runFixtureTests(fixturesPath, parseFunction) { export function runFixtureTests(fixturesPath, parseFunction) {
const fixtures = getFixtures(fixturesPath); const fixtures = getFixtures(fixturesPath);
@ -26,9 +65,11 @@ export function runFixtureTests(fixturesPath, parseFunction) {
} }
} }
err.message = const fixturePath = `${path.relative(
name + "/" + task.actual.filename + ": " + err.message; rootPath,
throw err; fixturesPath,
)}/${name}/${task.actual.filename}`;
throw new FixtureError(err, fixturePath, task.actual.code);
} }
}); });
}); });
@ -53,9 +94,11 @@ export function runThrowTestsWithEstree(fixturesPath, parseFunction) {
try { try {
runTest(task, parseFunction); runTest(task, parseFunction);
} catch (err) { } catch (err) {
err.message = const fixturePath = `${path.relative(
name + "/" + task.actual.filename + ": " + err.message; rootPath,
throw err; fixturesPath,
)}/${name}/${task.actual.filename}`;
throw new FixtureError(err, fixturePath, task.actual.code);
} }
}); });
}); });