From a522b1e556a7c02529d786da3dadddf59db1bf4e Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 14 Apr 2020 18:19:13 +0800 Subject: [PATCH] Add ability to test errors (#8025) --- .../graphql/__snapshots__/jsfmt.spec.js.snap | 12 ++++++++++ tests/errors/graphql/jsfmt.spec.js | 1 + tests/errors/graphql/type-interfaces.graphql | 7 ++++++ .../js/__snapshots__/jsfmt.spec.js.snap | 21 ++++++++++++++++ tests/errors/js/jsfmt.spec.js | 1 + tests/errors/js/partial-template-strings.js | 9 +++++++ ...mport-source-should-not-has-extra-token.js | 4 ++++ tests_config/run_spec.js | 24 ++++++++++++++++++- 8 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 tests/errors/graphql/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/errors/graphql/jsfmt.spec.js create mode 100644 tests/errors/graphql/type-interfaces.graphql create mode 100644 tests/errors/js/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/errors/js/jsfmt.spec.js create mode 100644 tests/errors/js/partial-template-strings.js create mode 100644 tests/errors/js/static-import-source-should-not-has-extra-token.js diff --git a/tests/errors/graphql/__snapshots__/jsfmt.spec.js.snap b/tests/errors/graphql/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..b76ad98e9 --- /dev/null +++ b/tests/errors/graphql/__snapshots__/jsfmt.spec.js.snap @@ -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 | " +`; diff --git a/tests/errors/graphql/jsfmt.spec.js b/tests/errors/graphql/jsfmt.spec.js new file mode 100644 index 000000000..4612e6ac5 --- /dev/null +++ b/tests/errors/graphql/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["graphql"]); diff --git a/tests/errors/graphql/type-interfaces.graphql b/tests/errors/graphql/type-interfaces.graphql new file mode 100644 index 000000000..7ccfab276 --- /dev/null +++ b/tests/errors/graphql/type-interfaces.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 +} diff --git a/tests/errors/js/__snapshots__/jsfmt.spec.js.snap b/tests/errors/js/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..4e1fe84c0 --- /dev/null +++ b/tests/errors/js/__snapshots__/jsfmt.spec.js.snap @@ -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 | " +`; diff --git a/tests/errors/js/jsfmt.spec.js b/tests/errors/js/jsfmt.spec.js new file mode 100644 index 000000000..8382eddeb --- /dev/null +++ b/tests/errors/js/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["babel"]); diff --git a/tests/errors/js/partial-template-strings.js b/tests/errors/js/partial-template-strings.js new file mode 100644 index 000000000..9bff507cc --- /dev/null +++ b/tests/errors/js/partial-template-strings.js @@ -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." diff --git a/tests/errors/js/static-import-source-should-not-has-extra-token.js b/tests/errors/js/static-import-source-should-not-has-extra-token.js new file mode 100644 index 000000000..bd0fd25fb --- /dev/null +++ b/tests/errors/js/static-import-source-should-not-has-extra-token.js @@ -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')); diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index 81d811b5e..37c86d178 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -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);