Repository: prettier/prettier (JavaScript) — Bug Fix

Issue:
Trailing-comma config ignored in YAML
**Prettier 2.1.2**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEAzAhgSwDaagcyQAIAdKIo4MiikkCAIwCs4wYBaAEwjgGd2oEDqkwcA7qIAWedgAcATnnGZOMSXWJ122nXQA01GnQAqAUQDKxjaXAQAtnfREAkuaLpOnOJyKS48uDpDAF8yMjgAD1lWeE4kQypyI3pmGPYRGH4JNRkFJXYJVXVkG31DWhAzS2s6SAcnADkAeWNff0CQELCoXSSybj4iQRgiXgBXVBEwOGIAPgTym0YWNi4efmH00QKpXMVYHaKakB1dEAMkiqqrEtr7RyJm1r8AokAUAiIGMZHlmIByXhDCBidyjGD7fBEThjOBEGAQIh-WZ-IJJYLneiyGCYaC8ZCgdDyeTAgAKhIQeJQ6GwYnQAE88XoQAx5OgwABrOAwcyyNl4fDIcEwplwOwMbxeTgAGXQBDG6HwcAAYhB5I4YNiCMgQOhvhAMZIYHZsAB1aTwXi86bmCmiTAAN1EdO1vHBmDYGLwvH8MBJrPwjmQGGw3qZTF4EQAQqyOVzzOg7HApXg4EHqaGQOGIuZ+dg4ABFMZCVNIYMZ3nyb3ybV0hPYDF5WAmlRqZAADgADEyFBBvSbWbJtQo+P57ammQBHIvwP0QWSUnUbODebwYgJTzABP0KwOl9NwJneuyYQXyYUgXi5gvTktlg8gGDoBjNorIABMTPBWFwBAAwvd0G1QQoHHEAxm9Ywn0pO8mXtGFnCgLxYHMMBFCxABBRDzBgOk8zTEM4GCYIgA)
```sh
--html-whitespace-sensitivity strict
--parser yaml
--trailing-comma none
```

**Input:**
```yaml
failing: 
  {
    "object-does-not-fit-within-print-width": "------",
    "TEST": "comma IS added here"
  }

expected:
  {
    "object-fits-within-print-width": "",
    "TEST": "comma NOT here"
  }

---
  
does not suffice: >
  {
    "object-does-not-fit-within-print-width": "------",
    "TEST": "comma NOT here — but object's now a string due to '>'"
  }
```

**Output:**
```yaml
failing:
  {
    "object-does-not-fit-within-print-width": "------",
    "TEST": "comma IS added here",
  }

expected: { "object-fits-within-print-width": "", "TEST": "comma NOT here" }

---
does not suffice: >
  {
    "object-does-not-fit-within-print-width": "------",
    "TEST": "comma NOT here — but object's not a string due to '>'"
  }

```

**Expected behavior:**

I expect that the nested JSON object in the YAML output will **not** have a trailing comma since I have the `trailing-comma` config disabled. Side note, the trailing comma makes the nested JSON invalid, so it's more than a visual aesthetic.

Thanks in advance.


Discussion:

Resolving patch:
diff --git a/changelog_unreleased/yaml/pr-9665.md b/changelog_unreleased/yaml/pr-9665.md
new file mode 100644
index 000000000000..5e99902031ee
--- /dev/null
+++ b/changelog_unreleased/yaml/pr-9665.md
@@ -0,0 +1,46 @@
+#### Apply `trailingComma` option (#9665 by @fisker)
+
+When `--trailing-comma=none`, should not add trailing comma to flowMapping and flowSequence.
+
+<!-- prettier-ignore -->
+```yaml
+# Input
+flow-mapping:
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  }
+flow-sequence:
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma IS added here"
+  ]
+
+# Prettier stable
+mapping:
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here",
+  }
+flow-sequence:
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma IS added here",
+  ]
+
+# Prettier master
+flow-mapping:
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  }
+flow-sequence:
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma IS added here"
+  ]
+```
diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js
index fa13a50810a1..4177c3b3e773 100644
--- a/src/language-yaml/printer-yaml.js
+++ b/src/language-yaml/printer-yaml.js
@@ -499,6 +499,8 @@ function _print(node, parentNode, path, options, print) {
           lastItem.type === "flowMappingItem" &&
           isEmptyNode(lastItem.key) &&
           isEmptyNode(lastItem.value))(getLast(node.children));
+      const trailingComma =
+        options.trailingComma === "none" ? "" : ifBreak(",", "");
       return concat([
         openMarker,
         indent(
@@ -526,7 +528,7 @@ function _print(node, parentNode, path, options, print) {
                 "children"
               )
             ),
-            ifBreak(",", ""),
+            trailingComma,
             hasEndComments(node)
               ? concat([
                   hardline,


Test patch:
diff --git a/tests/yaml/flow-mapping/trailing-comma/__snapshots__/jsfmt.spec.js.snap b/tests/yaml/flow-mapping/trailing-comma/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000000..2a5515d0428b
--- /dev/null
+++ b/tests/yaml/flow-mapping/trailing-comma/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,184 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`flow-mapping.yml - {"trailingComma":"all"} format 1`] = `
+====================================options=====================================
+parsers: ["yaml"]
+printWidth: 80
+trailingComma: "all"
+                                                                                | printWidth
+=====================================input======================================
+failing:
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  }
+
+expected:
+  {
+    "object-fits-within-print-width": "",
+    "TEST": "comma NOT here"
+  }
+
+---
+
+does not suffice: >
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  }
+
+=====================================output=====================================
+failing:
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here",
+  }
+
+expected: { "object-fits-within-print-width": "", "TEST": "comma NOT here" }
+
+---
+does not suffice: >
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  }
+
+================================================================================
+`;
+
+exports[`flow-mapping.yml - {"trailingComma":"es5"} format 1`] = `
+====================================options=====================================
+parsers: ["yaml"]
+printWidth: 80
+trailingComma: "es5"
+                                                                                | printWidth
+=====================================input======================================
+failing:
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  }
+
+expected:
+  {
+    "object-fits-within-print-width": "",
+    "TEST": "comma NOT here"
+  }
+
+---
+
+does not suffice: >
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  }
+
+=====================================output=====================================
+failing:
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here",
+  }
+
+expected: { "object-fits-within-print-width": "", "TEST": "comma NOT here" }
+
+---
+does not suffice: >
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  }
+
+================================================================================
+`;
+
+exports[`flow-mapping.yml - {"trailingComma":"none"} format 1`] = `
+====================================options=====================================
+parsers: ["yaml"]
+printWidth: 80
+trailingComma: "none"
+                                                                                | printWidth
+=====================================input======================================
+failing:
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  }
+
+expected:
+  {
+    "object-fits-within-print-width": "",
+    "TEST": "comma NOT here"
+  }
+
+---
+
+does not suffice: >
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  }
+
+=====================================output=====================================
+failing:
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  }
+
+expected: { "object-fits-within-print-width": "", "TEST": "comma NOT here" }
+
+---
+does not suffice: >
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  }
+
+================================================================================
+`;
+
+exports[`flow-mapping.yml format 1`] = `
+====================================options=====================================
+parsers: ["yaml"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+failing:
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  }
+
+expected:
+  {
+    "object-fits-within-print-width": "",
+    "TEST": "comma NOT here"
+  }
+
+---
+
+does not suffice: >
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  }
+
+=====================================output=====================================
+failing:
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here",
+  }
+
+expected: { "object-fits-within-print-width": "", "TEST": "comma NOT here" }
+
+---
+does not suffice: >
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  }
+
+================================================================================
+`;
diff --git a/tests/yaml/flow-mapping/trailing-comma/flow-mapping.yml b/tests/yaml/flow-mapping/trailing-comma/flow-mapping.yml
new file mode 100644
index 000000000000..103e9c80352d
--- /dev/null
+++ b/tests/yaml/flow-mapping/trailing-comma/flow-mapping.yml
@@ -0,0 +1,19 @@
+failing:
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  }
+
+expected:
+  {
+    "object-fits-within-print-width": "",
+    "TEST": "comma NOT here"
+  }
+
+---
+
+does not suffice: >
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  }
diff --git a/tests/yaml/flow-mapping/trailing-comma/jsfmt.spec.js b/tests/yaml/flow-mapping/trailing-comma/jsfmt.spec.js
new file mode 100644
index 000000000000..6bdc1ebe60bf
--- /dev/null
+++ b/tests/yaml/flow-mapping/trailing-comma/jsfmt.spec.js
@@ -0,0 +1,4 @@
+run_spec(__dirname, ["yaml"]);
+run_spec(__dirname, ["yaml"], { trailingComma: "none" });
+run_spec(__dirname, ["yaml"], { trailingComma: "es5" });
+run_spec(__dirname, ["yaml"], { trailingComma: "all" });
diff --git a/tests/yaml/flow-sequence/trailing-comma/__snapshots__/jsfmt.spec.js.snap b/tests/yaml/flow-sequence/trailing-comma/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000000..a21a27c5b8fe
--- /dev/null
+++ b/tests/yaml/flow-sequence/trailing-comma/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,192 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`flow-sequence.yml - {"trailingComma":"all"} format 1`] = `
+====================================options=====================================
+parsers: ["yaml"]
+printWidth: 80
+trailingComma: "all"
+                                                                                | printWidth
+=====================================input======================================
+failing:
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma IS added here"
+  ]
+
+expected:
+  [
+    "object-fits-within-print-width", "",
+    "TEST", "comma NOT here"
+  ]
+
+---
+
+does not suffice: >
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+
+=====================================output=====================================
+failing:
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma IS added here",
+  ]
+
+expected: ["object-fits-within-print-width", "", "TEST", "comma NOT here"]
+
+---
+does not suffice: >
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+
+================================================================================
+`;
+
+exports[`flow-sequence.yml - {"trailingComma":"es5"} format 1`] = `
+====================================options=====================================
+parsers: ["yaml"]
+printWidth: 80
+trailingComma: "es5"
+                                                                                | printWidth
+=====================================input======================================
+failing:
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma IS added here"
+  ]
+
+expected:
+  [
+    "object-fits-within-print-width", "",
+    "TEST", "comma NOT here"
+  ]
+
+---
+
+does not suffice: >
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+
+=====================================output=====================================
+failing:
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma IS added here",
+  ]
+
+expected: ["object-fits-within-print-width", "", "TEST", "comma NOT here"]
+
+---
+does not suffice: >
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+
+================================================================================
+`;
+
+exports[`flow-sequence.yml - {"trailingComma":"none"} format 1`] = `
+====================================options=====================================
+parsers: ["yaml"]
+printWidth: 80
+trailingComma: "none"
+                                                                                | printWidth
+=====================================input======================================
+failing:
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma IS added here"
+  ]
+
+expected:
+  [
+    "object-fits-within-print-width", "",
+    "TEST", "comma NOT here"
+  ]
+
+---
+
+does not suffice: >
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+
+=====================================output=====================================
+failing:
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma IS added here"
+  ]
+
+expected: ["object-fits-within-print-width", "", "TEST", "comma NOT here"]
+
+---
+does not suffice: >
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+
+================================================================================
+`;
+
+exports[`flow-sequence.yml format 1`] = `
+====================================options=====================================
+parsers: ["yaml"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+failing:
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma IS added here"
+  ]
+
+expected:
+  [
+    "object-fits-within-print-width", "",
+    "TEST", "comma NOT here"
+  ]
+
+---
+
+does not suffice: >
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+
+=====================================output=====================================
+failing:
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma IS added here",
+  ]
+
+expected: ["object-fits-within-print-width", "", "TEST", "comma NOT here"]
+
+---
+does not suffice: >
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+
+================================================================================
+`;
diff --git a/tests/yaml/flow-sequence/trailing-comma/flow-sequence.yml b/tests/yaml/flow-sequence/trailing-comma/flow-sequence.yml
new file mode 100644
index 000000000000..769ff94bacf2
--- /dev/null
+++ b/tests/yaml/flow-sequence/trailing-comma/flow-sequence.yml
@@ -0,0 +1,19 @@
+failing:
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma IS added here"
+  ]
+
+expected:
+  [
+    "object-fits-within-print-width", "",
+    "TEST", "comma NOT here"
+  ]
+
+---
+
+does not suffice: >
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
diff --git a/tests/yaml/flow-sequence/trailing-comma/jsfmt.spec.js b/tests/yaml/flow-sequence/trailing-comma/jsfmt.spec.js
new file mode 100644
index 000000000000..6bdc1ebe60bf
--- /dev/null
+++ b/tests/yaml/flow-sequence/trailing-comma/jsfmt.spec.js
@@ -0,0 +1,4 @@
+run_spec(__dirname, ["yaml"]);
+run_spec(__dirname, ["yaml"], { trailingComma: "none" });
+run_spec(__dirname, ["yaml"], { trailingComma: "es5" });
+run_spec(__dirname, ["yaml"], { trailingComma: "all" });
diff --git a/tests/yaml/json/trailing-comma/__snapshots__/jsfmt.spec.js.snap b/tests/yaml/json/trailing-comma/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000000..9580a7a6b081
--- /dev/null
+++ b/tests/yaml/json/trailing-comma/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,252 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`json.yml - {"trailingComma":"all"} format 1`] = `
+====================================options=====================================
+parsers: ["yaml"]
+printWidth: 80
+trailingComma: "all"
+                                                                                | printWidth
+=====================================input======================================
+[
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  },
+  {
+    "object-fits-within-print-width": "",
+    "TEST": "comma NOT here"
+  },
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  },
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma IS added here"
+  ],
+  [
+    "object-fits-within-print-width", "",
+    "TEST", "comma NOT here"
+  ],
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+]
+
+=====================================output=====================================
+[
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here",
+  },
+  { "object-fits-within-print-width": "", "TEST": "comma NOT here" },
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'",
+  },
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma IS added here",
+  ],
+  ["object-fits-within-print-width", "", "TEST", "comma NOT here"],
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma NOT here — but object's now a string due to '>'",
+  ],
+]
+
+================================================================================
+`;
+
+exports[`json.yml - {"trailingComma":"es5"} format 1`] = `
+====================================options=====================================
+parsers: ["yaml"]
+printWidth: 80
+trailingComma: "es5"
+                                                                                | printWidth
+=====================================input======================================
+[
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  },
+  {
+    "object-fits-within-print-width": "",
+    "TEST": "comma NOT here"
+  },
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  },
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma IS added here"
+  ],
+  [
+    "object-fits-within-print-width", "",
+    "TEST", "comma NOT here"
+  ],
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+]
+
+=====================================output=====================================
+[
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here",
+  },
+  { "object-fits-within-print-width": "", "TEST": "comma NOT here" },
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'",
+  },
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma IS added here",
+  ],
+  ["object-fits-within-print-width", "", "TEST", "comma NOT here"],
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma NOT here — but object's now a string due to '>'",
+  ],
+]
+
+================================================================================
+`;
+
+exports[`json.yml - {"trailingComma":"none"} format 1`] = `
+====================================options=====================================
+parsers: ["yaml"]
+printWidth: 80
+trailingComma: "none"
+                                                                                | printWidth
+=====================================input======================================
+[
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  },
+  {
+    "object-fits-within-print-width": "",
+    "TEST": "comma NOT here"
+  },
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  },
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma IS added here"
+  ],
+  [
+    "object-fits-within-print-width", "",
+    "TEST", "comma NOT here"
+  ],
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+]
+
+=====================================output=====================================
+[
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  },
+  { "object-fits-within-print-width": "", "TEST": "comma NOT here" },
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  },
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma IS added here"
+  ],
+  ["object-fits-within-print-width", "", "TEST", "comma NOT here"],
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma NOT here — but object's now a string due to '>'"
+  ]
+]
+
+================================================================================
+`;
+
+exports[`json.yml format 1`] = `
+====================================options=====================================
+parsers: ["yaml"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+[
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  },
+  {
+    "object-fits-within-print-width": "",
+    "TEST": "comma NOT here"
+  },
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  },
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma IS added here"
+  ],
+  [
+    "object-fits-within-print-width", "",
+    "TEST", "comma NOT here"
+  ],
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+]
+
+=====================================output=====================================
+[
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here",
+  },
+  { "object-fits-within-print-width": "", "TEST": "comma NOT here" },
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'",
+  },
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma IS added here",
+  ],
+  ["object-fits-within-print-width", "", "TEST", "comma NOT here"],
+  [
+    "object-does-not-fit-within-print-width",
+    "------",
+    "TEST",
+    "comma NOT here — but object's now a string due to '>'",
+  ],
+]
+
+================================================================================
+`;
diff --git a/tests/yaml/json/trailing-comma/jsfmt.spec.js b/tests/yaml/json/trailing-comma/jsfmt.spec.js
new file mode 100644
index 000000000000..6bdc1ebe60bf
--- /dev/null
+++ b/tests/yaml/json/trailing-comma/jsfmt.spec.js
@@ -0,0 +1,4 @@
+run_spec(__dirname, ["yaml"]);
+run_spec(__dirname, ["yaml"], { trailingComma: "none" });
+run_spec(__dirname, ["yaml"], { trailingComma: "es5" });
+run_spec(__dirname, ["yaml"], { trailingComma: "all" });
diff --git a/tests/yaml/json/trailing-comma/json.yml b/tests/yaml/json/trailing-comma/json.yml
new file mode 100644
index 000000000000..c317d3820439
--- /dev/null
+++ b/tests/yaml/json/trailing-comma/json.yml
@@ -0,0 +1,26 @@
+[
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma IS added here"
+  },
+  {
+    "object-fits-within-print-width": "",
+    "TEST": "comma NOT here"
+  },
+  {
+    "object-does-not-fit-within-print-width": "------",
+    "TEST": "comma NOT here — but object's now a string due to '>'"
+  },
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma IS added here"
+  ],
+  [
+    "object-fits-within-print-width", "",
+    "TEST", "comma NOT here"
+  ],
+  [
+    "object-does-not-fit-within-print-width", "------",
+    "TEST", "comma NOT here — but object's now a string due to '>'"
+  ]
+]