diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index e9972698f..f48e7a820 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -2,10 +2,14 @@ const fs = require("fs"); const path = require("path"); -const raw = require("jest-snapshot-serializer-raw").wrap; const { isCI } = require("ci-info"); const checkParsers = require("./utils/check-parsers"); const visualizeRange = require("./utils/visualize-range"); +const createSnapshot = require("./utils/create-snapshot"); +const composeOptionsForSnapshot = require("./utils/compose-options-for-snapshot"); +const visualizeEndOfLine = require("./utils/visualize-end-of-line"); +const consistentEndOfLine = require("./utils/consistent-end-of-line"); +const stringifyOptionsForTitle = require("./utils/stringify-options-for-title"); const { TEST_STANDALONE } = process.env; const AST_COMPARE = isCI || process.env.AST_COMPARE; @@ -110,7 +114,7 @@ global.run_spec = (fixtures, parsers, options) => { checkParsers({ dirname, files }, parsers); } - const stringifiedOptions = stringifyOptions(options); + const stringifiedOptions = stringifyOptionsForTitle(options); for (const { name, filename, code, output } of [...files, ...snippets]) { describe(`${name}${ @@ -191,13 +195,11 @@ global.run_spec = (fixtures, parsers, options) => { } expect( - raw( - createSnapshot( - codeForSnapshot, - hasEndOfLine ? visualizedOutput : formattedWithCursor, - { ...baseOptions, parsers }, - { codeOffset } - ) + createSnapshot( + codeForSnapshot, + hasEndOfLine ? visualizedOutput : formattedWithCursor, + composeOptionsForSnapshot(baseOptions, parsers), + { codeOffset } ) ).toMatchSnapshot(); } @@ -279,100 +281,3 @@ function format(source, filename, options) { result.formatted.slice(result.cursorOffset) : result.formatted; } - -function consistentEndOfLine(text) { - let firstEndOfLine; - return text.replace(/\r\n?|\n/g, (endOfLine) => { - if (!firstEndOfLine) { - firstEndOfLine = endOfLine; - } - return firstEndOfLine; - }); -} - -function visualizeEndOfLine(text) { - return text.replace(/\r\n?|\n/g, (endOfLine) => { - switch (endOfLine) { - case "\n": - return "\n"; - case "\r\n": - return "\n"; - case "\r": - return "\n"; - default: - throw new Error(`Unexpected end of line ${JSON.stringify(endOfLine)}`); - } - }); -} - -function createSnapshot(input, output, options, { codeOffset }) { - const separatorWidth = 80; - const printWidthIndicator = - options.printWidth > 0 && Number.isFinite(options.printWidth) - ? (codeOffset ? " ".repeat(codeOffset - 1) + "|" : "") + - " ".repeat(options.printWidth) + - "| printWidth" - : []; - return [] - .concat( - printSeparator(separatorWidth, "options"), - printOptions( - omit( - options, - (k) => - k === "rangeStart" || - k === "rangeEnd" || - k === "cursorOffset" || - k === "disableBabelTS" - ) - ), - printWidthIndicator, - printSeparator(separatorWidth, "input"), - input, - printSeparator(separatorWidth, "output"), - output, - printSeparator(separatorWidth) - ) - .join("\n"); -} - -function printSeparator(width, description) { - description = description || ""; - const leftLength = Math.floor((width - description.length) / 2); - const rightLength = width - leftLength - description.length; - return "=".repeat(leftLength) + description + "=".repeat(rightLength); -} - -function printOptions(options) { - const keys = Object.keys(options).sort(); - return keys.map((key) => `${key}: ${stringify(options[key])}`).join("\n"); - function stringify(value) { - return value === Infinity - ? "Infinity" - : Array.isArray(value) - ? `[${value.map((v) => JSON.stringify(v)).join(", ")}]` - : JSON.stringify(value); - } -} - -function omit(obj, fn) { - return Object.keys(obj).reduce((reduced, key) => { - const value = obj[key]; - if (!fn(key, value)) { - reduced[key] = value; - } - return reduced; - }, {}); -} - -function stringifyOptions(options) { - const string = JSON.stringify(options || {}, (key, value) => - key === "disableBabelTS" - ? undefined - : value === Infinity - ? "Infinity" - : value - ); - - return string === "{}" ? "" : string; -} diff --git a/tests_config/utils/compose-options-for-snapshot.js b/tests_config/utils/compose-options-for-snapshot.js new file mode 100644 index 000000000..723e32fd1 --- /dev/null +++ b/tests_config/utils/compose-options-for-snapshot.js @@ -0,0 +1,19 @@ +"use strict"; + +function composeOptionsForSnapshot(baseOptions, parsers) { + const { + rangeStart, + rangeEnd, + cursorOffset, + disableBabelTS, + + ...snapshotOptions + } = baseOptions; + + return { + ...snapshotOptions, + parsers, + }; +} + +module.exports = composeOptionsForSnapshot; diff --git a/tests_config/utils/consistent-end-of-line.js b/tests_config/utils/consistent-end-of-line.js new file mode 100644 index 000000000..63d990181 --- /dev/null +++ b/tests_config/utils/consistent-end-of-line.js @@ -0,0 +1,13 @@ +"use strict"; + +function consistentEndOfLine(text) { + let firstEndOfLine; + return text.replace(/\r\n?|\n/g, (endOfLine) => { + if (!firstEndOfLine) { + firstEndOfLine = endOfLine; + } + return firstEndOfLine; + }); +} + +module.exports = consistentEndOfLine; diff --git a/tests_config/utils/create-snapshot.js b/tests_config/utils/create-snapshot.js new file mode 100644 index 000000000..32cf77d33 --- /dev/null +++ b/tests_config/utils/create-snapshot.js @@ -0,0 +1,48 @@ +"use strict"; + +const raw = require("jest-snapshot-serializer-raw").wrap; + +function printSeparator(width, description) { + description = description || ""; + const leftLength = Math.floor((width - description.length) / 2); + const rightLength = width - leftLength - description.length; + return "=".repeat(leftLength) + description + "=".repeat(rightLength); +} + +function stringify(value) { + return value === Infinity + ? "Infinity" + : Array.isArray(value) + ? `[${value.map((v) => JSON.stringify(v)).join(", ")}]` + : JSON.stringify(value); +} +function printOptions(options) { + const keys = Object.keys(options).sort(); + return keys.map((key) => `${key}: ${stringify(options[key])}`).join("\n"); +} + +function createSnapshot(input, output, options, { codeOffset }) { + const separatorWidth = 80; + const printWidthIndicator = + options.printWidth > 0 && Number.isFinite(options.printWidth) + ? (codeOffset ? " ".repeat(codeOffset - 1) + "|" : "") + + " ".repeat(options.printWidth) + + "| printWidth" + : []; + return raw( + [] + .concat( + printSeparator(separatorWidth, "options"), + printOptions(options), + printWidthIndicator, + printSeparator(separatorWidth, "input"), + input, + printSeparator(separatorWidth, "output"), + output, + printSeparator(separatorWidth) + ) + .join("\n") + ); +} + +module.exports = createSnapshot; diff --git a/tests_config/utils/stringify-options-for-title.js b/tests_config/utils/stringify-options-for-title.js new file mode 100644 index 000000000..cf94a578e --- /dev/null +++ b/tests_config/utils/stringify-options-for-title.js @@ -0,0 +1,15 @@ +"use strict"; + +function stringifyOptions(options) { + const string = JSON.stringify(options || {}, (key, value) => + key === "disableBabelTS" + ? undefined + : value === Infinity + ? "Infinity" + : value + ); + + return string === "{}" ? "" : string; +} + +module.exports = stringifyOptions; diff --git a/tests_config/utils/visualize-end-of-line.js b/tests_config/utils/visualize-end-of-line.js new file mode 100644 index 000000000..13a30befc --- /dev/null +++ b/tests_config/utils/visualize-end-of-line.js @@ -0,0 +1,18 @@ +"use strict"; + +function visualizeEndOfLine(text) { + return text.replace(/\r\n?|\n/g, (endOfLine) => { + switch (endOfLine) { + case "\n": + return "\n"; + case "\r\n": + return "\n"; + case "\r": + return "\n"; + default: + throw new Error(`Unexpected end of line ${JSON.stringify(endOfLine)}`); + } + }); +} + +module.exports = visualizeEndOfLine;