Files
test-repo/tests/integration/__tests__/doc-trim.js
T
fisker Cheung c96f148ccf Merge branch 'main' into next
# Conflicts:
#	tests/config/install-prettier.js
#	tests/format/js/cursor/jsfmt.spec.js
#	tests/integration/__tests__/doc-trim.js
#	tests/integration/__tests__/format.js
#	tests/integration/__tests__/line-suffix-boundary.js
#	tests/integration/__tests__/load-toml.js
#	tests/integration/__tests__/stdin-filepath.js
#	tests/integration/__tests__/with-parser-inference.js
2022-03-17 21:32:14 +08:00

58 lines
1.4 KiB
JavaScript

import prettier from "prettier-local";
import { outdent } from "outdent";
const docPrinter = prettier.doc.printer;
const docBuilders = prettier.doc.builders;
const { printDocToString } = docPrinter;
const { concat, line, trim, group, indent } = docBuilders;
// These tests don't use `runPrettier` because `trim` is not used by any
// bundled parser (only third-party plugins).
describe("trim", () => {
test.each([
["trims the current line", group(concat(["hello ", trim])), "hello"],
[
"trims existing indentation",
group(
concat([
"function()",
line,
"{",
indent(
concat([
line,
group(concat([trim, "#if DEBUG"])),
line,
"alert(42);",
line,
group(concat([trim, "#endif"])),
])
),
line,
"}",
])
),
outdent`
function()
{
#if DEBUG
alert(42);
#endif
}
`,
],
[
"ignores trimmed characters when fitting the line",
group(concat(["hello ", " ", trim, line, "world!"])),
"hello world!",
],
])("%s", (_, doc, expected) => {
const result = printDocToString(doc, { printWidth: 12, tabWidth: 2 });
expect(result).toBeDefined();
expect(result.formatted).toEqual(expected);
});
});