Test: extract some code to createSnapshot (#10255)
This commit is contained in:
1 parent
8198f40727
commit
75c6dcd6a1
3 files changed
+77
-84
No files matched your search
@@ -8,9 +8,7 @@ const prettier = !TEST_STANDALONE
|
||||
? require("prettier-local")
|
||||
: require("prettier-standalone");
|
||||
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");
|
||||
@@ -252,49 +250,12 @@ function runTest({
|
||||
}
|
||||
|
||||
// All parsers have the same result, only snapshot the result from main parser
|
||||
// TODO: move this part to `createSnapshot`
|
||||
const hasEndOfLine = "endOfLine" in formatOptions;
|
||||
let codeForSnapshot = formatResult.inputWithCursor;
|
||||
let codeOffset = 0;
|
||||
let resultForSnapshot = formatResult.outputWithCursor;
|
||||
const { rangeStart, rangeEnd, cursorOffset } = formatResult.options;
|
||||
|
||||
if (typeof rangeStart === "number" || typeof rangeEnd === "number") {
|
||||
let rangeStartWithCursor = rangeStart;
|
||||
let rangeEndWithCursor = rangeEnd;
|
||||
if (typeof cursorOffset === "number") {
|
||||
if (
|
||||
typeof rangeStartWithCursor === "number" &&
|
||||
rangeStartWithCursor > cursorOffset
|
||||
) {
|
||||
rangeStartWithCursor += CURSOR_PLACEHOLDER.length;
|
||||
}
|
||||
if (
|
||||
typeof rangeEndWithCursor === "number" &&
|
||||
rangeEndWithCursor > cursorOffset
|
||||
) {
|
||||
rangeEndWithCursor += CURSOR_PLACEHOLDER.length;
|
||||
}
|
||||
}
|
||||
codeForSnapshot = visualizeRange(codeForSnapshot, {
|
||||
rangeStart: rangeStartWithCursor,
|
||||
rangeEnd: rangeEndWithCursor,
|
||||
});
|
||||
codeOffset = codeForSnapshot.match(/^>?\s+1 \|/)[0].length + 1;
|
||||
}
|
||||
|
||||
if (hasEndOfLine) {
|
||||
codeForSnapshot = visualizeEndOfLine(codeForSnapshot);
|
||||
resultForSnapshot = visualizeEndOfLine(resultForSnapshot);
|
||||
}
|
||||
|
||||
expect(
|
||||
createSnapshot(
|
||||
codeForSnapshot,
|
||||
resultForSnapshot,
|
||||
composeOptionsForSnapshot(formatResult.options, parsers),
|
||||
{ codeOffset }
|
||||
)
|
||||
createSnapshot(formatResult, {
|
||||
parsers,
|
||||
formatOptions,
|
||||
CURSOR_PLACEHOLDER,
|
||||
})
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
function composeOptionsForSnapshot(baseOptions, parsers) {
|
||||
const {
|
||||
plugins,
|
||||
filepath,
|
||||
parser,
|
||||
errors,
|
||||
|
||||
...snapshotOptions
|
||||
} = baseOptions;
|
||||
|
||||
return {
|
||||
...snapshotOptions,
|
||||
parsers,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = composeOptionsForSnapshot;
|
||||
@@ -1,10 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
const raw = require("jest-snapshot-serializer-raw").wrap;
|
||||
const visualizeRange = require("./visualize-range");
|
||||
const visualizeEndOfLine = require("./visualize-end-of-line");
|
||||
|
||||
function printSeparator(width, description = "") {
|
||||
const leftLength = Math.floor((width - description.length) / 2);
|
||||
const rightLength = width - leftLength - description.length;
|
||||
const SEPARATOR_WIDTH = 80;
|
||||
function printSeparator(description = "") {
|
||||
const leftLength = Math.floor((SEPARATOR_WIDTH - description.length) / 2);
|
||||
const rightLength = SEPARATOR_WIDTH - leftLength - description.length;
|
||||
return "=".repeat(leftLength) + description + "=".repeat(rightLength);
|
||||
}
|
||||
|
||||
@@ -15,31 +18,79 @@ function stringify(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");
|
||||
const {
|
||||
plugins,
|
||||
filepath,
|
||||
errors,
|
||||
parser,
|
||||
|
||||
...snapshotOptions
|
||||
} = options;
|
||||
|
||||
const keys = Object.keys(snapshotOptions).sort();
|
||||
return keys
|
||||
.map((key) => `${key}: ${stringify(snapshotOptions[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",
|
||||
]
|
||||
: [];
|
||||
function printWidthIndicator(printWidth, offset) {
|
||||
if (!Number.isFinite(printWidth) || printWidth < 1) {
|
||||
return "";
|
||||
}
|
||||
|
||||
let before = "";
|
||||
if (offset) {
|
||||
before = " ".repeat(offset - 1) + "|";
|
||||
}
|
||||
|
||||
return `${before}${" ".repeat(printWidth)}| printWidth`;
|
||||
}
|
||||
|
||||
function createSnapshot(
|
||||
formatResult,
|
||||
{ parsers, formatOptions, CURSOR_PLACEHOLDER }
|
||||
) {
|
||||
let {
|
||||
inputWithCursor: input,
|
||||
outputWithCursor: output,
|
||||
options,
|
||||
} = formatResult;
|
||||
let { rangeStart, rangeEnd, cursorOffset, printWidth } = options;
|
||||
|
||||
let codeOffset = 0;
|
||||
if (typeof rangeStart === "number" || typeof rangeEnd === "number") {
|
||||
if (typeof cursorOffset === "number") {
|
||||
if (typeof rangeStart === "number" && rangeStart > cursorOffset) {
|
||||
rangeStart += CURSOR_PLACEHOLDER.length;
|
||||
}
|
||||
if (typeof rangeEnd === "number" && rangeEnd > cursorOffset) {
|
||||
rangeEnd += CURSOR_PLACEHOLDER.length;
|
||||
}
|
||||
}
|
||||
|
||||
input = visualizeRange(input, { rangeStart, rangeEnd });
|
||||
codeOffset = input.match(/^>?\s+1 \|/)[0].length + 1;
|
||||
}
|
||||
|
||||
if ("endOfLine" in formatOptions) {
|
||||
input = visualizeEndOfLine(input);
|
||||
output = visualizeEndOfLine(output);
|
||||
}
|
||||
|
||||
const widthIndicator = printWidthIndicator(printWidth, codeOffset);
|
||||
|
||||
return raw(
|
||||
[
|
||||
printSeparator(separatorWidth, "options"),
|
||||
printOptions(options),
|
||||
...printWidthIndicator,
|
||||
printSeparator(separatorWidth, "input"),
|
||||
printSeparator("options"),
|
||||
printOptions({ ...options, parsers }),
|
||||
...(widthIndicator ? [widthIndicator] : []),
|
||||
printSeparator("input"),
|
||||
input,
|
||||
printSeparator(separatorWidth, "output"),
|
||||
printSeparator("output"),
|
||||
output,
|
||||
printSeparator(separatorWidth),
|
||||
printSeparator(),
|
||||
].join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user