diff --git a/changelog_unreleased/javascript/20043.md b/changelog_unreleased/javascript/20043.md new file mode 100644 index 000000000..7b674b648 --- /dev/null +++ b/changelog_unreleased/javascript/20043.md @@ -0,0 +1,30 @@ +#### Preserve comment order in test calls (#20043 by @lllleolin-max) + + +```jsx +// Input +test("x", () => { + run(); +}, // first +// second +// third +60000); + +// Prettier stable +test("x", () => { + run(); +}, // second // first +// third +60000); + +// Prettier main +test( + "x", + () => { + run(); + }, // first + // second + // third + 60000, +); +``` diff --git a/src/language-js/utilities/test-libraries.js b/src/language-js/utilities/test-libraries.js index f8207423f..f1788d58c 100644 --- a/src/language-js/utilities/test-libraries.js +++ b/src/language-js/utilities/test-libraries.js @@ -1,4 +1,5 @@ import { getCallArguments } from "./call-arguments.js"; +import { hasComment } from "./comments.js"; import { getFunctionParameters } from "./function-parameters.js"; import { isNodeMatches } from "./is-node-matches.js"; import { @@ -104,6 +105,10 @@ function isTestCall(node, parent) { const args = getCallArguments(node); + if (args.some((arg) => hasComment(arg))) { + return false; + } + if (args.length === 1) { if (isAngularTestWrapper(node) && isTestCall(parent)) { return isFunctionOrArrowExpression(args[0]); diff --git a/tests/format/js/test-declarations/__snapshots__/format.test.js.snap b/tests/format/js/test-declarations/__snapshots__/format.test.js.snap index 581d64d24..9fa6c0c81 100644 --- a/tests/format/js/test-declarations/__snapshots__/format.test.js.snap +++ b/tests/format/js/test-declarations/__snapshots__/format.test.js.snap @@ -534,6 +534,151 @@ function x() { ================================================================================ `; +exports[`comments.js - {"arrowParens":"avoid"} format 1`] = ` +====================================options===================================== +arrowParens: "avoid" +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +test("comments after callback", () => { + run(); +}, // first +// second +// third +60000); + +it("comments before timeout", function (done) { + done(); +}, +// first +// second +60000); + +describe.only("comments after callback", () => { + run(); +}, // first +// second +60000); + +test("comments before callback", // first +// second +() => { + run(); +}); + +=====================================output===================================== +test( + "comments after callback", + () => { + run(); + }, // first + // second + // third + 60000, +); + +it( + "comments before timeout", + function (done) { + done(); + }, + // first + // second + 60000, +); + +describe.only( + "comments after callback", + () => { + run(); + }, // first + // second + 60000, +); + +test( + "comments before callback", // first + // second + () => { + run(); + }, +); + +================================================================================ +`; + +exports[`comments.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +test("comments after callback", () => { + run(); +}, // first +// second +// third +60000); + +it("comments before timeout", function (done) { + done(); +}, +// first +// second +60000); + +describe.only("comments after callback", () => { + run(); +}, // first +// second +60000); + +test("comments before callback", // first +// second +() => { + run(); +}); + +=====================================output===================================== +test( + "comments after callback", + () => { + run(); + }, // first + // second + // third + 60000, +); + +it( + "comments before timeout", + function (done) { + done(); + }, + // first + // second + 60000, +); + +describe.only( + "comments after callback", + () => { + run(); + }, // first + // second + 60000, +); + +test( + "comments before callback", // first + // second + () => { + run(); + }, +); + +================================================================================ +`; + exports[`jest-each.js - {"arrowParens":"avoid"} format 1`] = ` ====================================options===================================== arrowParens: "avoid" diff --git a/tests/format/js/test-declarations/comments.js b/tests/format/js/test-declarations/comments.js new file mode 100644 index 000000000..1b439f0ca --- /dev/null +++ b/tests/format/js/test-declarations/comments.js @@ -0,0 +1,25 @@ +test("comments after callback", () => { + run(); +}, // first +// second +// third +60000); + +it("comments before timeout", function (done) { + done(); +}, +// first +// second +60000); + +describe.only("comments after callback", () => { + run(); +}, // first +// second +60000); + +test("comments before callback", // first +// second +() => { + run(); +});