Files
test-repo/tests_integration/__tests__/debug-api.js
T
Georgii Dolzhykov 56840d4890 Followup for #10169: minor tweaks (#10177)
* Followup for #10169: minor tweaks

* Add Copy doc button to Playground
2021-01-30 18:25:43 +02:00

79 lines
2.3 KiB
JavaScript

"use strict";
const {
__debug: { parse, formatAST, formatDoc, printToDoc, printDocToString },
doc: {
builders,
utils: { cleanDoc },
},
} = require("prettier-local");
const { outdent } = require("outdent");
const code = outdent`
const foo = 'bar'
`;
const formatted = 'const foo = "bar";\n';
const options = { parser: "babel", originalText: code };
describe("API", () => {
const { ast } = parse(code, options);
test("prettier.parse", () => {
expect(Array.isArray(ast.program.body)).toBe(true);
});
const { formatted: formatResultFromAST } = formatAST(ast, options);
test("prettier.formatAST", () => {
expect(formatResultFromAST).toBe(formatted);
});
const doc = printToDoc(code, options);
test("prettier.printToDoc", (done) => {
// If it's array, it's a `concat`
if (!Array.isArray(doc)) {
expect(doc.type).toBe("concat");
expect(Array.isArray(doc.parts)).toBe(true);
}
done();
});
const formatResultFromDoc = formatDoc(doc, options);
test("prettier.formatDoc", () => {
expect(formatResultFromDoc).toMatchSnapshot();
});
const { formatted: stringFromDoc } = printDocToString(doc, options);
test("prettier.printDocToString", () => {
expect(stringFromDoc).toBe(formatted);
});
const doc2 = new Function(
`{ ${Object.keys(builders)} }`,
`return ${formatResultFromDoc}`
)(builders);
const { formatted: stringFromDoc2 } = printDocToString(doc2, options);
const formatResultFromDoc2 = formatDoc(doc2, options);
test("output of prettier.formatDoc can be reused as code", () => {
expect(stringFromDoc2).toBe(formatted);
expect(formatResultFromDoc2).toBe(formatResultFromDoc);
});
test("prettier.formatDoc prints things as expected", () => {
const { indent, hardline, literalline, fill } = builders;
expect(formatDoc([indent(hardline), indent(literalline)])).toBe(
"[indent(hardline), indent(literalline)]"
);
expect(formatDoc(fill(["foo", hardline, "bar", literalline, "baz"]))).toBe(
'fill(["foo", hardline, "bar", literalline, "baz"])'
);
expect(
formatDoc(
// The argument of fill must not be passed to cleanDoc because it's not a doc
fill(cleanDoc(["foo", literalline, "bar"])) // invalid fill
)
).toBe('fill(["foo", literallineWithoutBreakParent, breakParent, "bar"])');
});
});