diff --git a/tests/js_empty/__snapshots__/jsfmt.spec.js.snap b/tests/js_empty/__snapshots__/jsfmt.spec.js.snap index 1a6d39560..c8a018f1b 100644 --- a/tests/js_empty/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js_empty/__snapshots__/jsfmt.spec.js.snap @@ -1,6 +1,18 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`empty.js format 1`] = ` +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 @@ -13,21 +25,47 @@ printWidth: 80 ================================================================================ `; -exports[`many-semicolons.js format 1`] = ` +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[`semicolon.js format 1`] = ` +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 @@ -39,3 +77,18 @@ printWidth: 80 ================================================================================ `; + +exports[`snippet: #6 format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +; + +;;; + +=====================================output===================================== + +================================================================================ +`; diff --git a/tests/js_empty/empty.js b/tests/js_empty/empty.js deleted file mode 100644 index 8b1378917..000000000 --- a/tests/js_empty/empty.js +++ /dev/null @@ -1 +0,0 @@ - diff --git a/tests/js_empty/jsfmt.spec.js b/tests/js_empty/jsfmt.spec.js index eb85eda6b..4f21218ea 100644 --- a/tests/js_empty/jsfmt.spec.js +++ b/tests/js_empty/jsfmt.spec.js @@ -1 +1,18 @@ -run_spec(__dirname, ["babel", "flow", "typescript"]); +run_spec( + { + dirname: __dirname, + snippets: [ + // empty + "", + // empty lines + "\n", + "\n\n\n\n", + // semicolons + ";", + ";;;;", + ";\n", + ";\n\n;;;\n", + ], + }, + ["babel", "flow", "typescript"] +); diff --git a/tests/js_empty/many-semicolons.js b/tests/js_empty/many-semicolons.js deleted file mode 100644 index 6b6add353..000000000 --- a/tests/js_empty/many-semicolons.js +++ /dev/null @@ -1,2 +0,0 @@ -;; -; diff --git a/tests/js_empty/semicolon.js b/tests/js_empty/semicolon.js deleted file mode 100644 index 092bc2b04..000000000 --- a/tests/js_empty/semicolon.js +++ /dev/null @@ -1 +0,0 @@ -; diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index d38ddc7e9..e70abc5e1 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -49,7 +49,10 @@ const unstableTests = new Map( const isTestDirectory = (dirname, name) => dirname.startsWith(path.join(__dirname, "../tests", name)); -global.run_spec = (dirname, parsers, options) => { +global.run_spec = (fixtures, parsers, options) => { + fixtures = typeof fixtures === "string" ? { dirname: fixtures } : fixtures; + const { dirname } = fixtures; + // `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone` const IS_PARSER_INFERENCE_TESTS = isTestDirectory( dirname, @@ -67,32 +70,50 @@ global.run_spec = (dirname, parsers, options) => { throw new Error(`No parsers were specified for ${dirname}`); } - const files = fs.readdirSync(dirname, { withFileTypes: true }); - for (const file of files) { - const basename = file.name; - const filename = path.join(dirname, basename); + const snippets = (fixtures.snippets || []).map((test, index) => { + test = typeof test === "string" ? { code: test } : test; + return { + name: `snippet: ${test.name || `#${index}`}`, + filename: test.filename, + code: test.code, + }; + }); - if ( - path.extname(basename) === ".snap" || - !file.isFile() || - basename[0] === "." || - basename === "jsfmt.spec.js" - ) { - continue; - } + const files = fs + .readdirSync(dirname, { withFileTypes: true }) + .map((file) => { + const basename = file.name; + const filename = path.join(dirname, basename); + if ( + path.extname(basename) === ".snap" || + !file.isFile() || + basename[0] === "." || + basename === "jsfmt.spec.js" + ) { + return; + } - const stringifiedOptions = stringifyOptions(options); + const text = fs.readFileSync(filename, "utf8"); - describe(`${basename}${ + return { + name: basename, + filename, + code: text, + }; + }) + .filter(Boolean); + + const stringifiedOptions = stringifyOptions(options); + + for (const { name, filename, code } of [...files, ...snippets]) { + describe(`${name}${ stringifiedOptions ? ` - ${stringifiedOptions}` : "" }`, () => { let rangeStart; let rangeEnd; let cursorOffset; - const text = fs.readFileSync(filename, "utf8"); - - const source = (TEST_CRLF ? text.replace(/\n/g, "\r\n") : text) + const source = (TEST_CRLF ? code.replace(/\n/g, "\r\n") : code) .replace(RANGE_START_PLACEHOLDER, (match, offset) => { rangeStart = offset; return ""; @@ -144,7 +165,7 @@ global.run_spec = (dirname, parsers, options) => { createSnapshot( hasEndOfLine ? visualizeEndOfLine( - text + code .replace(RANGE_START_PLACEHOLDER, "") .replace(RANGE_END_PLACEHOLDER, "") ) @@ -170,7 +191,7 @@ global.run_spec = (dirname, parsers, options) => { options && (options.disableBabelTS === true || (Array.isArray(options.disableBabelTS) && - options.disableBabelTS.includes(basename))) + options.disableBabelTS.includes(name))) ) { expect(() => { format(input, filename, verifyOptions);