diff --git a/cspell.json b/cspell.json index bc2291979..a60e4e112 100644 --- a/cspell.json +++ b/cspell.json @@ -180,6 +180,7 @@ "Kasturi", "Kearney", "Khatri", + "l’objectif", "lcov", "libdef", "linearize", @@ -244,6 +245,7 @@ "Okonetchnikov", "onwarn", "Oopsy", + "outdent", "overparenthesization", "overscroll", "packagejson", @@ -328,6 +330,7 @@ "suchipi", "superset", "supertypes", + "Supprimer", "templating", "tempy", "testname", diff --git a/package.json b/package.json index f55349e3b..b35d215a7 100644 --- a/package.json +++ b/package.json @@ -105,6 +105,7 @@ "jest-snapshot-serializer-ansi": "1.0.0", "jest-snapshot-serializer-raw": "1.1.0", "jest-watch-typeahead": "0.5.0", + "outdent": "0.7.1", "prettier": "2.0.5", "rimraf": "3.0.2", "rollup": "2.7.2", diff --git a/tests/js_empty/__snapshots__/jsfmt.spec.js.snap b/tests/js_empty/__snapshots__/jsfmt.spec.js.snap index 37db768c8..38dad2aaa 100644 --- a/tests/js_empty/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js_empty/__snapshots__/jsfmt.spec.js.snap @@ -1,98 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`snippet: #0 format 1`] = ` -====================================options===================================== -parsers: ["babel", "flow", "typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== - -=====================================output===================================== - -================================================================================ -`; - -exports[`snippet: #1 format 1`] = ` -====================================options===================================== -parsers: ["babel", "flow", "typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== - - -=====================================output===================================== - -================================================================================ -`; - -exports[`snippet: #2 format 1`] = ` -====================================options===================================== -parsers: ["babel", "flow", "typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== - - - - - -=====================================output===================================== - -================================================================================ -`; - -exports[`snippet: #3 format 1`] = ` -====================================options===================================== -parsers: ["babel", "flow", "typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== -; -=====================================output===================================== - -================================================================================ -`; - -exports[`snippet: #4 format 1`] = ` -====================================options===================================== -parsers: ["babel", "flow", "typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== -;;;; -=====================================output===================================== - -================================================================================ -`; - -exports[`snippet: #5 format 1`] = ` -====================================options===================================== -parsers: ["babel", "flow", "typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== -; - -=====================================output===================================== - -================================================================================ -`; - -exports[`snippet: #6 format 1`] = ` -====================================options===================================== -parsers: ["babel", "flow", "typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== -; - -;;; - -=====================================output===================================== - -================================================================================ -`; - exports[`snippet: #7 format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] diff --git a/tests/js_empty/jsfmt.spec.js b/tests/js_empty/jsfmt.spec.js index 1eaaa369d..0672272d9 100644 --- a/tests/js_empty/jsfmt.spec.js +++ b/tests/js_empty/jsfmt.spec.js @@ -2,16 +2,18 @@ run_spec( { dirname: __dirname, snippets: [ - // empty - "", - // empty lines - "\n", - "\n\n\n\n", - // semicolons - ";", - ";;;;", - ";\n", - ";\n\n;;;\n", + ...[ + // empty + "", + // empty lines + "\n", + "\n\n\n\n", + // semicolons + ";", + ";;;;", + ";\n", + ";\n\n;;;\n", + ].map((code) => ({ code, output: "" })), // comments "// comment", "/* comment */", diff --git a/tests/jsx-whitespace/__snapshots__/jsfmt.spec.js.snap b/tests/jsx-whitespace/__snapshots__/jsfmt.spec.js.snap deleted file mode 100644 index 05b57c47f..000000000 --- a/tests/jsx-whitespace/__snapshots__/jsfmt.spec.js.snap +++ /dev/null @@ -1,63 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`test.js format 1`] = ` -====================================options===================================== -parsers: ["flow", "typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== -// Treated as whitespace in JSX -spaces =
] [
-tabs =
] [
-slash_n =
] - - -[
-slash_r =
] - - -[
- - -// Not treated as whitespace in JSX -// NOTE: Some of the space characters here won't show up in an editor, -// but they are there! -slash_f =
] [
-slash_v =
] [
-non_breaking_spaces =
]   [
-em_space =
]   [
-hair_space =
]  [
-zero_width_space =
]​​​[
-real_world_non_breaking_spaces =

- Supprimer l’objectif « {goal.name} » ? -

-real_world_non_breaking_spaces2 =

- Supprimer l’objectif padding padding padding padding padding padding « {goal.name} » ? -

- -=====================================output===================================== -// Treated as whitespace in JSX -spaces =
] [
; -tabs =
] [
; -slash_n =
] [
; -slash_r =
] [
; - -// Not treated as whitespace in JSX -// NOTE: Some of the space characters here won't show up in an editor, -// but they are there! -slash_f =
] [
; -slash_v =
] [
; -non_breaking_spaces =
]   [
; -em_space =
]   [
; -hair_space =
]  [
; -zero_width_space =
]​​​[
; -real_world_non_breaking_spaces =

Supprimer l’objectif « {goal.name} » ?

; -real_world_non_breaking_spaces2 = ( -

- Supprimer l’objectif padding padding padding padding padding padding «  - {goal.name} » ? -

-); - -================================================================================ -`; diff --git a/tests/jsx-whitespace/jsfmt.spec.js b/tests/jsx-whitespace/jsfmt.spec.js index a842e1348..9d5fc32cf 100644 --- a/tests/jsx-whitespace/jsfmt.spec.js +++ b/tests/jsx-whitespace/jsfmt.spec.js @@ -1 +1,76 @@ -run_spec(__dirname, ["flow", "typescript"]); +/* eslint-disable no-irregular-whitespace */ +const { outdent } = require("outdent"); + +run_spec( + { + dirname: __dirname, + snippets: [ + { + code: "spaces =
] [
", + output: "spaces =
] [
;", + }, + { + code: "tabs =
] \t [
", + output: "tabs =
] [
;", + }, + { + code: "slash_n =
]\n\n[
", + output: "slash_n =
] [
;", + }, + { + code: "slash_r =
]\r\r[
", + output: "slash_r =
] [
;", + }, + { + code: "slash_f =
]\f\f[
", + output: "slash_f =
]\f\f[
;", + }, + { + code: "slash_v =
]\v\v[
", + output: "slash_v =
]\v\v[
;", + }, + { + code: "non_breaking_spaces =
]\u00A0\u00A0[
", + output: "non_breaking_spaces =
]\u00A0\u00A0[
;", + }, + { + code: "em_space =
]\u2003\u2003[
", + output: "em_space =
]\u2003\u2003[
;", + }, + { + code: "hair_space =
]\u200a\u200a[
", + output: "hair_space =
]\u200a\u200a[
;", + }, + { + code: "zero_width_space =
]\u200b\u200b[
", + output: "zero_width_space =
]\u200b\u200b[
;", + }, + { + code: outdent` + real_world_non_breaking_spaces =

+ Supprimer l’objectif «\u00A0{goal.name}\u00A0» ? +

+ `, + output: outdent` + real_world_non_breaking_spaces =

Supprimer l’objectif «\u00A0{goal.name}\u00A0» ?

; + `, + }, + { + code: outdent` + real_world_non_breaking_spaces2 =

+ Supprimer l’objectif padding padding padding padding padding padding «\u00A0{goal.name}\u00A0» ? +

+ `, + output: outdent` + real_world_non_breaking_spaces2 = ( +

+ Supprimer l’objectif padding padding padding padding padding padding «\u00A0 + {goal.name}\u00A0» ? +

+ ); + `, + }, + ].map((test) => ({ ...test, output: test.output + "\n" })), + }, + ["flow", "typescript"] +); diff --git a/tests/jsx-whitespace/test.js b/tests/jsx-whitespace/test.js deleted file mode 100644 index fa92e9d44..000000000 --- a/tests/jsx-whitespace/test.js +++ /dev/null @@ -1,28 +0,0 @@ -// Treated as whitespace in JSX -spaces =
] [
-tabs =
] [
-slash_n =
] - - -[
-slash_r =
] - - -[
- - -// Not treated as whitespace in JSX -// NOTE: Some of the space characters here won't show up in an editor, -// but they are there! -slash_f =
] [
-slash_v =
] [
-non_breaking_spaces =
]   [
-em_space =
]   [
-hair_space =
]  [
-zero_width_space =
]​​​[
-real_world_non_breaking_spaces =

- Supprimer l’objectif « {goal.name} » ? -

-real_world_non_breaking_spaces2 =

- Supprimer l’objectif padding padding padding padding padding padding « {goal.name} » ? -

diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index e70abc5e1..bc9dc4bfb 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -73,9 +73,8 @@ global.run_spec = (fixtures, parsers, options) => { const snippets = (fixtures.snippets || []).map((test, index) => { test = typeof test === "string" ? { code: test } : test; return { + ...test, name: `snippet: ${test.name || `#${index}`}`, - filename: test.filename, - code: test.code, }; }); @@ -105,7 +104,7 @@ global.run_spec = (fixtures, parsers, options) => { const stringifiedOptions = stringifyOptions(options); - for (const { name, filename, code } of [...files, ...snippets]) { + for (const { name, filename, code, output } of [...files, ...snippets]) { describe(`${name}${ stringifiedOptions ? ` - ${stringifiedOptions}` : "" }`, () => { @@ -153,28 +152,33 @@ global.run_spec = (fixtures, parsers, options) => { return; } - const output = format(input, filename, mainOptions); - const visualizedOutput = visualizeEndOfLine(output); + const formattedWithCursor = format(input, filename, mainOptions); + const formatted = formattedWithCursor.replace(CURSOR_PLACEHOLDER, ""); + const visualizedOutput = visualizeEndOfLine(formattedWithCursor); test("format", () => { expect(visualizedOutput).toEqual( - visualizeEndOfLine(consistentEndOfLine(output)) + visualizeEndOfLine(consistentEndOfLine(formattedWithCursor)) ); - expect( - raw( - createSnapshot( - hasEndOfLine - ? visualizeEndOfLine( - code - .replace(RANGE_START_PLACEHOLDER, "") - .replace(RANGE_END_PLACEHOLDER, "") - ) - : source, - hasEndOfLine ? visualizedOutput : output, - { ...baseOptions, parsers } + if (typeof output === "string") { + expect(formatted).toEqual(output); + } else { + expect( + raw( + createSnapshot( + hasEndOfLine + ? visualizeEndOfLine( + code + .replace(RANGE_START_PLACEHOLDER, "") + .replace(RANGE_END_PLACEHOLDER, "") + ) + : source, + hasEndOfLine ? visualizedOutput : formattedWithCursor, + { ...baseOptions, parsers } + ) ) - ) - ).toMatchSnapshot(); + ).toMatchSnapshot(); + } }); const parsersToVerify = parsers.slice(1); @@ -203,7 +207,6 @@ global.run_spec = (fixtures, parsers, options) => { }); } - const formatted = output.replace(CURSOR_PLACEHOLDER, ""); const isUnstable = unstableTests.get(filename); const isUnstableTest = isUnstable && isUnstable(options || {}); if ( @@ -219,9 +222,9 @@ global.run_spec = (fixtures, parsers, options) => { if (isUnstableTest) { // To keep eye on failed tests, this assert never supposed to pass, // if it fails, just remove the file from `unstableTests` - expect(secondOutput).not.toEqual(output); + expect(secondOutput).not.toEqual(formatted); } else { - expect(secondOutput).toEqual(output); + expect(secondOutput).toEqual(formatted); } }); } diff --git a/yarn.lock b/yarn.lock index b1007c7db..e1a1c96c4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5809,6 +5809,11 @@ os-tmpdir@~1.0.2: resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274" integrity sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ= +outdent@0.7.1: + version "0.7.1" + resolved "https://registry.yarnpkg.com/outdent/-/outdent-0.7.1.tgz#e9b400443622a97760b0bc74fa3223252ccd02a2" + integrity sha512-VjIzdUHunL74DdhcwMDt5FhNDQ8NYmTkuW0B+usIV2afS9aWT/1c9z1TsnFW349TP3nxmYeUl7Z++XpJRByvgg== + p-defer@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/p-defer/-/p-defer-1.0.0.tgz#9f6eb182f6c9aa8cd743004a7d4f96b196b0fb0c"