releaseChangelogWriter.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { describe, expect, it } from "vitest";
  2. import { writeChangelog } from "conventional-changelog-writer";
  3. import releaseConfig from "../../../.release-it.mjs";
  4. import {
  5. createReleaseChangelogWriterOpts,
  6. extractCommitBodyBullets,
  7. } from "../../../scripts/release-it/changelog-writer.mjs";
  8. describe("release changelog writer", () => {
  9. it("只启用 release-it 的 conventional changelog 插件", () => {
  10. expect(Object.keys(releaseConfig.plugins)).toEqual(["@release-it/conventional-changelog"]);
  11. });
  12. it("extracts markdown bullet items from commit body", () => {
  13. expect(
  14. extractCommitBodyBullets(`- first change
  15. - second change
  16. plain paragraph
  17. * third change`),
  18. ).toEqual(["first change", "second change", "third change"]);
  19. });
  20. it("carries commit body bullets into transformed changelog commits", () => {
  21. const writerOpts = createReleaseChangelogWriterOpts();
  22. const transformed = writerOpts.transform?.(
  23. {
  24. type: "fix",
  25. scope: "web-remote",
  26. subject: "harden mobile reconnect flow",
  27. body:
  28. "- add safe home-only services\n\n" +
  29. "- extend workspace reconnect timeout\n\n" +
  30. "- bump package version to 1.10.0",
  31. notes: [],
  32. references: [],
  33. hash: "38dd68bc37234cdecb2e11276b7f715a56bf3e06",
  34. },
  35. {
  36. host: "https://git.example.invalid",
  37. owner: "codegeex",
  38. repository: "z-code",
  39. },
  40. );
  41. expect(transformed).toMatchObject({
  42. type: "Bug Fixes",
  43. scope: "web-remote",
  44. subject: "harden mobile reconnect flow",
  45. bodyBullets: [
  46. "add safe home-only services",
  47. "extend workspace reconnect timeout",
  48. "bump package version to 1.10.0",
  49. ],
  50. });
  51. });
  52. it("renders body bullets on their own indented lines", async () => {
  53. const changelog = writeChangelog(
  54. {
  55. version: "1.10.0",
  56. host: "https://git.example.invalid",
  57. owner: "codegeex",
  58. repository: "z-code",
  59. commit: "commit",
  60. linkCompare: false,
  61. linkReferences: true,
  62. },
  63. createReleaseChangelogWriterOpts(),
  64. );
  65. const chunks: string[] = [];
  66. for await (const chunk of changelog([
  67. {
  68. type: "fix",
  69. scope: "web-remote",
  70. subject: "harden mobile reconnect flow",
  71. body:
  72. "- add safe home-only services\n\n" +
  73. "- extend workspace reconnect timeout\n\n" +
  74. "- bump package version to 1.10.0",
  75. notes: [],
  76. references: [],
  77. hash: "38dd68bc37234cdecb2e11276b7f715a56bf3e06",
  78. },
  79. {
  80. type: "fix",
  81. scope: "zzz",
  82. subject: "keep next commit on its own line",
  83. body: null,
  84. notes: [],
  85. references: [],
  86. hash: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  87. },
  88. {
  89. type: "fix",
  90. scope: "ci",
  91. subject: "resolve Linux build images via CI_REGISTRY_IMAGE",
  92. body: null,
  93. notes: [],
  94. references: [],
  95. hash: "5e136a9a0c9c5bb3db1d75972d956d6626a3fcef",
  96. },
  97. ])) {
  98. chunks.push(String(chunk));
  99. }
  100. const output = chunks.join("");
  101. expect(output).toContain(
  102. "* **ci:** resolve Linux build images via CI_REGISTRY_IMAGE " +
  103. "([5e136a9](https://git.example.invalid/codegeex/z-code/commit/5e136a9a0c9c5bb3db1d75972d956d6626a3fcef))",
  104. );
  105. expect(output).toContain(
  106. "* **web-remote:** harden mobile reconnect flow " +
  107. "([38dd68b](https://git.example.invalid/codegeex/z-code/commit/38dd68bc37234cdecb2e11276b7f715a56bf3e06))\n" +
  108. " * add safe home-only services\n" +
  109. " * extend workspace reconnect timeout\n" +
  110. " * bump package version to 1.10.0\n",
  111. );
  112. expect(output).toContain(
  113. " * bump package version to 1.10.0\n" +
  114. "\n" +
  115. "* **zzz:** keep next commit on its own line",
  116. );
  117. expect(output).not.toMatch(/[^\n]\* \*\*/);
  118. expect(output).not.toMatch(/^[ \t]+\* \*\*/m);
  119. });
  120. });