Add ability to test errors (#8025)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-04-14 18:19:13 +08:00
1 parent e6f6cf262d
commit a522b1e556
8 files changed
+78 -1

No files matched your search

@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`type-interfaces.graphql error test 1`] = `
"Syntax Error: Unexpected \\"|\\". (5:21)
3 | # which will break seperator detection added in #7305
4 |
> 5 | type A implements A | B {
| ^
6 | a: a
7 | }
8 | "
`;
+1
View File
@@ -0,0 +1 @@
run_spec(__dirname, ["graphql"]);
@@ -0,0 +1,7 @@
# This test to grantee `graphql` parser won't allow
# something like `|` as seperator
# which will break seperator detection added in #7305
type A implements A | B {
a: a
}
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`partial-template-strings.js error test 1`] = `
"Unexpected token (6:42)
4 |
5 | const Diagnostics = {
> 6 | unexpected_token: \`Unexpected token: \${?}\`,
| ^
7 | name_not_found: \`'\${?}' not found.\`
8 | };
9 | Diagnostics.name_not_found(\\"foo\\"); // \\"'foo' not found.\\""
`;
exports[`static-import-source-should-not-has-extra-token.js error test 1`] = `
"Unexpected token (4:16)
2 | // be parenthesized, that accidentally break #8016
3 |
> 4 | import {} from (('a'));
| ^
5 | "
`;
+1
View File
@@ -0,0 +1 @@
run_spec(__dirname, ["babel"]);
@@ -0,0 +1,9 @@
// partial template strings is not supported by babel yet
// If parsers supported, should remove this and open a issue about this
// Proposal: https://github.com/tc39/proposal-partial-application/blob/master/README.md#proposal
const Diagnostics = {
unexpected_token: `Unexpected token: ${?}`,
name_not_found: `'${?}' not found.`
};
Diagnostics.name_not_found("foo"); // "'foo' not found."
@@ -0,0 +1,4 @@
// This test to grantee parsers won't allow static import source to
// be parenthesized, that accidentally break #8016
import {} from (('a'));
+23 -1
View File
@@ -46,9 +46,21 @@ const unstableTests = new Map(
})
);
const isTestDirectory = (dirname, name) =>
dirname.startsWith(path.join(__dirname, "../tests", name));
global.run_spec = (dirname, parsers, options) => {
// `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone`
const IS_PARSER_INFERENCE_TESTS = dirname.endsWith("parser-inference");
const IS_PARSER_INFERENCE_TESTS = isTestDirectory(
dirname,
"parser-inference"
);
// `IS_ERROR_TESTS` mean to watch errors like:
// - syntax parser hasn't supported yet
// - syntax errors that should throws
const IS_ERROR_TESTS = isTestDirectory(dirname, "errors");
if (IS_PARSER_INFERENCE_TESTS) {
parsers = [];
} else if (!parsers || !parsers.length) {
@@ -105,6 +117,16 @@ global.run_spec = (dirname, parsers, options) => {
};
const hasEndOfLine = "endOfLine" in mainOptions;
if (IS_ERROR_TESTS) {
test("error test", () => {
expect(() => {
format(input, filename, mainOptions);
}).toThrowErrorMatchingSnapshot();
});
return;
}
const output = format(input, filename, mainOptions);
const visualizedOutput = visualizeEndOfLine(output);