diff --git a/tests/config/format-test.js b/tests/config/format-test.js index f99ef2ab3..743e03ee7 100644 --- a/tests/config/format-test.js +++ b/tests/config/format-test.js @@ -234,9 +234,19 @@ function runSpec(fixtures, parsers, options) { filepath: filename, parser, }; - const mainParserFormatResult = shouldThrowOnFormat(name, formatOptions) - ? { options: formatOptions, error: true } - : format(code, formatOptions); + const shouldThrowOnMainParserFormat = shouldThrowOnFormat( + name, + formatOptions + ); + + let mainParserFormatResult; + if (shouldThrowOnMainParserFormat) { + mainParserFormatResult = { options: formatOptions, error: true }; + } else { + beforeAll(async () => { + mainParserFormatResult = await format(code, formatOptions); + }); + } for (const currentParser of allParsers) { if ( @@ -246,22 +256,31 @@ function runSpec(fixtures, parsers, options) { ) { continue; } - runTest({ - parsers, - name, - filename, - code, - output, - parser: currentParser, - mainParserFormatResult, - mainParserFormatOptions: formatOptions, + + const testTitle = + shouldThrowOnMainParserFormat || + formatOptions.parser !== currentParser + ? `[${currentParser}] format` + : "format"; + + test(testTitle, async () => { + await runTest({ + parsers, + name, + filename, + code, + output, + parser: currentParser, + mainParserFormatResult, + mainParserFormatOptions: formatOptions, + }); }); } }); } } -function runTest({ +async function runTest({ parsers, name, filename, @@ -273,52 +292,46 @@ function runTest({ }) { let formatOptions = mainParserFormatOptions; let formatResult = mainParserFormatResult; - let formatTestTitle = "format"; // Verify parsers or error tests if ( mainParserFormatResult.error || mainParserFormatOptions.parser !== parser ) { - formatTestTitle = `[${parser}] format`; formatOptions = { ...mainParserFormatResult.options, parser }; const runFormat = () => format(code, formatOptions); if (shouldThrowOnFormat(name, formatOptions)) { - test(formatTestTitle, () => { - expect(runFormat).toThrowErrorMatchingSnapshot(); - }); + await expect(runFormat()).rejects.toThrowErrorMatchingSnapshot(); return; } // Verify parsers format result should be the same as main parser output = mainParserFormatResult.outputWithCursor; - formatResult = runFormat(); + formatResult = await runFormat(); } - test(formatTestTitle, () => { - // Make sure output has consistent EOL + // Make sure output has consistent EOL + expect(formatResult.eolVisualizedOutput).toEqual( + visualizeEndOfLine(consistentEndOfLine(formatResult.outputWithCursor)) + ); + + // The result is assert to equals to `output` + if (typeof output === "string") { expect(formatResult.eolVisualizedOutput).toEqual( - visualizeEndOfLine(consistentEndOfLine(formatResult.outputWithCursor)) + visualizeEndOfLine(output) ); + return; + } - // The result is assert to equals to `output` - if (typeof output === "string") { - expect(formatResult.eolVisualizedOutput).toEqual( - visualizeEndOfLine(output) - ); - return; - } - - // All parsers have the same result, only snapshot the result from main parser - expect( - createSnapshot(formatResult, { - parsers, - formatOptions, - CURSOR_PLACEHOLDER, - }) - ).toMatchSnapshot(); - }); + // All parsers have the same result, only snapshot the result from main parser + expect( + createSnapshot(formatResult, { + parsers, + formatOptions, + CURSOR_PLACEHOLDER, + }) + ).toMatchSnapshot(); if (!FULL_TEST) { return; @@ -330,63 +343,58 @@ function runTest({ // No range and cursor formatResult.input === code ) { - test(`[${parser}] second format`, () => { - const { eolVisualizedOutput: firstOutput, output } = formatResult; - const { eolVisualizedOutput: secondOutput } = format( - output, - formatOptions - ); - 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(firstOutput); - } else { - expect(secondOutput).toEqual(firstOutput); - } - }); + const { eolVisualizedOutput: firstOutput, output } = formatResult; + const { eolVisualizedOutput: secondOutput } = await format( + output, + formatOptions + ); + 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(firstOutput); + } else { + expect(secondOutput).toEqual(firstOutput); + } } const isAstUnstableTest = isAstUnstable(filename, formatOptions); // Some parsers skip parsing empty files if (formatResult.changed && code.trim()) { - test(`[${parser}] compare AST`, () => { - const { input, output } = formatResult; - const originalAst = parse(input, formatOptions); - const formattedAst = parse(output, formatOptions); - if (isAstUnstableTest) { - expect(formattedAst).not.toEqual(originalAst); - } else { - expect(formattedAst).toEqual(originalAst); - } - }); + const { input, output } = formatResult; + const originalAst = parse(input, formatOptions); + const formattedAst = parse(output, formatOptions); + if (isAstUnstableTest) { + expect(formattedAst).not.toEqual(originalAst); + } else { + expect(formattedAst).toEqual(originalAst); + } } if (!shouldSkipEolTest(code, formatResult.options)) { for (const eol of ["\r\n", "\r"]) { - test(`[${parser}] EOL ${JSON.stringify(eol)}`, () => { - const output = format( - code.replace(/\n/g, eol), - formatOptions - ).eolVisualizedOutput; - // Only if `endOfLine: "auto"` the result will be different - const expected = - formatOptions.endOfLine === "auto" - ? visualizeEndOfLine( - // All `code` use `LF`, so the `eol` of result is always `LF` - formatResult.outputWithCursor.replace(/\n/g, eol) - ) - : formatResult.eolVisualizedOutput; - expect(output).toEqual(expected); - }); + const { eolVisualizedOutput: output } = await format( + code.replace(/\n/g, eol), + formatOptions + ); + // Only if `endOfLine: "auto"` the result will be different + const expected = + formatOptions.endOfLine === "auto" + ? visualizeEndOfLine( + // All `code` use `LF`, so the `eol` of result is always `LF` + formatResult.outputWithCursor.replace(/\n/g, eol) + ) + : formatResult.eolVisualizedOutput; + expect(output).toEqual(expected); } } if (code.charAt(0) !== BOM) { - test(`[${parser}] BOM`, () => { - const output = format(BOM + code, formatOptions).eolVisualizedOutput; - const expected = BOM + formatResult.eolVisualizedOutput; - expect(output).toEqual(expected); - }); + const { eolVisualizedOutput: output } = await format( + BOM + code, + formatOptions + ); + const expected = BOM + formatResult.eolVisualizedOutput; + expect(output).toEqual(expected); } } @@ -453,7 +461,8 @@ const insertCursor = (text, cursorOffset) => CURSOR_PLACEHOLDER + text.slice(cursorOffset) : text; -function format(originalText, originalOptions) { +// eslint-disable-next-line require-await +async function format(originalText, originalOptions) { const { text: input, options } = replacePlaceholders( originalText, originalOptions