mirror of
https://github.com/LukeHagar/prettier-plugin-openapi.git
synced 2025-12-06 04:21:03 +00:00
Refactor markdown formatting tests by removing Prettier dependency and simplifying string conversion logic
This commit is contained in:
@@ -1,90 +1,13 @@
|
|||||||
import { describe, expect, it } from "bun:test";
|
import { describe, expect, it } from "bun:test";
|
||||||
import { printers } from "../src/index.js";
|
import { printers } from "../src/index.js";
|
||||||
import prettier from "prettier";
|
|
||||||
|
|
||||||
describe("Markdown Formatting in Descriptions", () => {
|
describe("Markdown Formatting in Descriptions", () => {
|
||||||
const printer = printers?.["openapi-ast"];
|
const printer = printers?.["openapi-ast"];
|
||||||
|
|
||||||
// Get printDocToString from Prettier's internals (call once per test)
|
|
||||||
function getPrintDocToString() {
|
|
||||||
// Try multiple paths to access Prettier's printDocToString
|
|
||||||
const prettierAny = prettier as any;
|
|
||||||
|
|
||||||
// Path 1: __internal.doc.printDocToString
|
|
||||||
if (prettierAny.__internal?.doc?.printDocToString) {
|
|
||||||
return prettierAny.__internal.doc.printDocToString;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Path 2: __internal.docPrinter.formatDoc
|
|
||||||
if (prettierAny.__internal?.docPrinter?.formatDoc) {
|
|
||||||
return prettierAny.__internal.docPrinter.formatDoc;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Path 3: Try require with different paths
|
|
||||||
try {
|
|
||||||
// Try prettier/standalone or other internal paths
|
|
||||||
const docUtils = require("prettier/standalone");
|
|
||||||
if (docUtils?.printDocToString) {
|
|
||||||
return docUtils.printDocToString;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Not available
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Try direct access to internal modules
|
|
||||||
const prettierDoc = require("prettier/doc");
|
|
||||||
if (prettierDoc?.printer?.printDocToString) {
|
|
||||||
return prettierDoc.printer.printDocToString;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Not available
|
|
||||||
}
|
|
||||||
|
|
||||||
// If all else fails, check if result is already a string
|
|
||||||
// (some Prettier versions return strings directly)
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function to convert Doc to formatted string using Prettier's printDocToString
|
|
||||||
function docToString(doc: any, printDocToString: any, options: any = {}): string {
|
|
||||||
// If already a string, return it
|
|
||||||
if (typeof doc === "string") {
|
|
||||||
return doc;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If printDocToString is not available, try to stringify the doc
|
|
||||||
if (!printDocToString) {
|
|
||||||
// Fallback: if it's an object, try JSON.stringify or inspect
|
|
||||||
if (doc && typeof doc === "object") {
|
|
||||||
// Try to see if it has a toString that works
|
|
||||||
const str = String(doc);
|
|
||||||
if (str !== "[object Object]") {
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
// Otherwise, we need printDocToString
|
|
||||||
throw new Error("printDocToString is required to convert Doc to string");
|
|
||||||
}
|
|
||||||
return String(doc);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use printDocToString to convert Doc to formatted string
|
|
||||||
const result = printDocToString(doc, {
|
|
||||||
printWidth: options.printWidth || 80,
|
|
||||||
tabWidth: options.tabWidth || 2,
|
|
||||||
useTabs: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
// printDocToString returns { formatted: string }
|
|
||||||
return result?.formatted || String(doc);
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("Basic markdown formatting", () => {
|
describe("Basic markdown formatting", () => {
|
||||||
it("should format description fields with markdown", () => {
|
it("should format description fields with markdown", () => {
|
||||||
expect(printer).toBeDefined();
|
expect(printer).toBeDefined();
|
||||||
|
|
||||||
const printDocToString = getPrintDocToString();
|
|
||||||
|
|
||||||
const testData = {
|
const testData = {
|
||||||
isOpenAPI: true,
|
isOpenAPI: true,
|
||||||
format: "yaml",
|
format: "yaml",
|
||||||
@@ -121,7 +44,7 @@ describe("Markdown Formatting in Descriptions", () => {
|
|||||||
throw new Error("Result is undefined");
|
throw new Error("Result is undefined");
|
||||||
}
|
}
|
||||||
|
|
||||||
const resultString = docToString(result, printDocToString, { tabWidth: 2 });
|
const resultString = String(result);
|
||||||
|
|
||||||
// Check that multiple spaces are normalized in the original content
|
// Check that multiple spaces are normalized in the original content
|
||||||
// Note: YAML may format this differently, but the content should be processed
|
// Note: YAML may format this differently, but the content should be processed
|
||||||
@@ -133,8 +56,6 @@ describe("Markdown Formatting in Descriptions", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should preserve code blocks in descriptions", () => {
|
it("should preserve code blocks in descriptions", () => {
|
||||||
const printDocToString = getPrintDocToString();
|
|
||||||
|
|
||||||
const testData = {
|
const testData = {
|
||||||
isOpenAPI: true,
|
isOpenAPI: true,
|
||||||
format: "yaml",
|
format: "yaml",
|
||||||
@@ -158,7 +79,7 @@ describe("Markdown Formatting in Descriptions", () => {
|
|||||||
throw new Error("Result is undefined");
|
throw new Error("Result is undefined");
|
||||||
}
|
}
|
||||||
|
|
||||||
const resultString = docToString(result, printDocToString, { tabWidth: 2 });
|
const resultString = String(result);
|
||||||
|
|
||||||
// Code blocks (4+ spaces) should be preserved
|
// Code blocks (4+ spaces) should be preserved
|
||||||
expect(resultString).toContain(" const x = 1;");
|
expect(resultString).toContain(" const x = 1;");
|
||||||
@@ -166,8 +87,6 @@ describe("Markdown Formatting in Descriptions", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should format markdown in nested objects", () => {
|
it("should format markdown in nested objects", () => {
|
||||||
const printDocToString = getPrintDocToString();
|
|
||||||
|
|
||||||
const testData = {
|
const testData = {
|
||||||
isOpenAPI: true,
|
isOpenAPI: true,
|
||||||
format: "yaml",
|
format: "yaml",
|
||||||
@@ -208,7 +127,7 @@ describe("Markdown Formatting in Descriptions", () => {
|
|||||||
throw new Error("Result is undefined");
|
throw new Error("Result is undefined");
|
||||||
}
|
}
|
||||||
|
|
||||||
const resultString = docToString(result, printDocToString, { tabWidth: 2 });
|
const resultString = String(result);
|
||||||
|
|
||||||
// Both parameter and response descriptions should be formatted
|
// Both parameter and response descriptions should be formatted
|
||||||
expect(resultString).toContain("description:");
|
expect(resultString).toContain("description:");
|
||||||
@@ -217,8 +136,6 @@ describe("Markdown Formatting in Descriptions", () => {
|
|||||||
|
|
||||||
describe("Summary field formatting", () => {
|
describe("Summary field formatting", () => {
|
||||||
it("should format summary fields", () => {
|
it("should format summary fields", () => {
|
||||||
const printDocToString = getPrintDocToString();
|
|
||||||
|
|
||||||
const testData = {
|
const testData = {
|
||||||
isOpenAPI: true,
|
isOpenAPI: true,
|
||||||
format: "yaml",
|
format: "yaml",
|
||||||
@@ -250,7 +167,7 @@ describe("Markdown Formatting in Descriptions", () => {
|
|||||||
throw new Error("Result is undefined");
|
throw new Error("Result is undefined");
|
||||||
}
|
}
|
||||||
|
|
||||||
const resultString = docToString(result, printDocToString, { tabWidth: 2 });
|
const resultString = String(result);
|
||||||
|
|
||||||
// Summary fields should be processed
|
// Summary fields should be processed
|
||||||
expect(resultString).toContain("summary:");
|
expect(resultString).toContain("summary:");
|
||||||
|
|||||||
Reference in New Issue
Block a user