Test: extract code to utils (#8433)
This commit is contained in:
1 parent
f31430d7be
commit
413f1a5db5
6 files changed
+124
-106
No files matched your search
+11
-106
@@ -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 "<LF>\n";
|
||||
case "\r\n":
|
||||
return "<CRLF>\n";
|
||||
case "\r":
|
||||
return "<CR>\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;
|
||||
}
|
||||
Reference in new issue
Block a user