fork of hey-api/openapi-ts because I need some additional things
at main 8.0 kB view raw
1import type * as getGitHubInfo from '@changesets/get-github-info'; 2import parse from '@changesets/parse'; 3import type { 4 ModCompWithPackage, 5 NewChangesetWithCommit, 6 VersionType, 7} from '@changesets/types'; 8import { describe, expect, it, vi } from 'vitest'; 9 10import changelog from '../.changeset/changelog.js'; 11 12type GetGitHubInfo = typeof getGitHubInfo; 13 14const data = { 15 commit: 'a085003', 16 pull: 1613, 17 repo: 'hey-api/openapi-ts', 18 user: 'someone', 19}; 20 21vi.mock( 22 '@changesets/get-github-info', 23 (): GetGitHubInfo => ({ 24 async getInfo({ commit, repo }) { 25 const { pull, user } = data; 26 const links = { 27 commit: `[\`${commit}\`](https://github.com/${repo}/commit/${commit})`, 28 pull: `[#${pull}](https://github.com/${repo}/pull/${pull})`, 29 user: `[@${user}](https://github.com/${user})`, 30 }; 31 return { 32 links, 33 pull, 34 user, 35 }; 36 }, 37 async getInfoFromPullRequest({ pull, repo }) { 38 const { commit, user } = data; 39 const links = { 40 commit: `[\`${commit}\`](https://github.com/${repo}/commit/${commit})`, 41 pull: `[#${pull}](https://github.com/${repo}/pull/${pull})`, 42 user: `[@${user}](https://github.com/${user})`, 43 }; 44 return { 45 commit, 46 links, 47 user, 48 }; 49 }, 50 }), 51); 52 53const getChangeset = ( 54 content: string, 55 commit: string | undefined, 56): [NewChangesetWithCommit, VersionType, null | Record<string, any>] => [ 57 { 58 ...parse( 59 `--- 60pkg: "minor" 61--- 62 63something 64${content} 65`, 66 ), 67 commit, 68 id: 'some-id', 69 }, 70 'minor', 71 { repo: data.repo }, 72]; 73 74describe('changelog', () => { 75 it('formats dependency release lines', async () => { 76 const changesets: NewChangesetWithCommit[] = [ 77 { 78 commit: 'abc123', 79 id: 'fake-id', 80 releases: [], 81 summary: 'update deps', 82 }, 83 ]; 84 const deps: ModCompWithPackage[] = [ 85 { 86 changesets: ['fake-id'], 87 dir: '/fake/path', 88 name: '@hey-api/openapi-ts', 89 newVersion: '0.0.2', 90 oldVersion: '0.0.1', 91 packageJson: { 92 name: '@hey-api/openapi-ts', 93 version: '0.0.1', 94 }, 95 type: 'patch', 96 }, 97 ]; 98 99 const line = await changelog.getDependencyReleaseLine(changesets, deps, { 100 repo: 'org/repo', 101 }); 102 103 expect(line).toEqual( 104 '### Updated Dependencies:\n - @hey-api/openapi-ts@0.0.2', 105 ); 106 }); 107 108 it('formats regular release lines', async () => { 109 const changeset: NewChangesetWithCommit = { 110 commit: 'abc123', 111 id: 'fake-id', 112 releases: [], 113 summary: 'Fixed bug in parser', 114 }; 115 116 const line = await changelog.getReleaseLine(changeset, 'patch', { 117 repo: 'org/repo', 118 }); 119 120 expect(line).toContain('Fixed bug in parser'); 121 expect(line).toContain('abc123'); 122 }); 123 124 it('with multiple authors', async () => { 125 expect( 126 await changelog.getReleaseLine( 127 ...getChangeset( 128 ['author: @one', 'author: @two'].join('\n'), 129 data.commit, 130 ), 131 ), 132 ).toEqual( 133 `\n- something ([#1613](https://github.com/hey-api/openapi-ts/pull/1613)) ([\`a085003\`](https://github.com/hey-api/openapi-ts/commit/a085003)) by [@one](https://github.com/one), [@two](https://github.com/two)`, 134 ); 135 }); 136 137 it('places metadata on the first line and preserves markdown code blocks', async () => { 138 const summary = [ 139 'refactor(config): replace `off` with null to disable options', 140 '', 141 '### Updated `output` options', 142 '', 143 'We made the `output` configuration more consistent by using `null` to represent disabled options. [This change](https://heyapi.dev/openapi-ts/migrating#updated-output-options) does not affect boolean options.', 144 '', 145 '```js', 146 'export default {', 147 ' input: "hey-api/backend", // sign up at app.heyapi.dev', 148 ' output: {', 149 ' format: null,', 150 ' lint: null,', 151 ' path: "src/client",', 152 ' tsConfigPath: null,', 153 ' },', 154 '};', 155 '```', 156 ].join('\n'); 157 const changeset = { 158 commit: 'fcdd73b816d74babf47e6a1f46032f5b8ebb4b48', 159 id: 'fake-id', 160 releases: [], 161 summary, 162 }; 163 const line = await changelog.getReleaseLine(changeset, 'minor', { 164 repo: 'hey-api/openapi-ts', 165 }); 166 // Metadata should be on the first line 167 expect(line).toMatch( 168 /^\n- refactor\(config\): replace `off` with null to disable options \(\[#1613\]\(https:\/\/github.com\/hey-api\/openapi-ts\/pull\/1613\)\) \(\[`fcdd73b`\]\(https:\/\/github.com\/hey-api\/openapi-ts\/commit\/fcdd73b816d74babf47e6a1f46032f5b8ebb4b48\)\) by \[@someone\]\(https:\/\/github.com\/someone\)/, 169 ); 170 // Should not contain quadruple backticks 171 expect(line).not.toContain('````'); 172 // Should contain a markdown code block 173 expect(line).toContain('```js\nexport default {'); 174 expect(line).toContain( 175 ' input: "hey-api/backend", // sign up at app.heyapi.dev', 176 ); 177 expect(line).toContain(' output: {'); 178 expect(line).toContain(' format: null,'); 179 expect(line).toContain(' lint: null,'); 180 expect(line).toContain(' path: "src/client",'); 181 expect(line).toContain(' tsConfigPath: null,'); 182 expect(line).toContain(' },'); 183 expect(line).toContain('};'); 184 expect(line).toContain('```'); 185 }); 186 187 it('converts multiple code blocks and preserves non-code content', async () => { 188 const summary = [ 189 'feat: add foo', 190 '', 191 '```js', 192 'console.log(1);', 193 '```', 194 '', 195 'Some text.', 196 '', 197 '```ts', 198 'console.log(2);', 199 '```', 200 ].join('\n'); 201 const changeset = { 202 commit: 'abc123', 203 id: 'fake-id', 204 releases: [], 205 summary, 206 }; 207 const line = await changelog.getReleaseLine(changeset, 'minor', { 208 repo: 'hey-api/openapi-ts', 209 }); 210 expect(line).toContain('```js\nconsole.log(1);\n```'); 211 expect(line).toContain('```ts\nconsole.log(2);\n```'); 212 expect(line).toContain('Some text.'); 213 }); 214 215 describe.each(['author', 'user'])( 216 'override author with %s keyword', 217 (keyword) => { 218 it.each(['with @', 'without @'])('%s', async (kind) => { 219 expect( 220 await changelog.getReleaseLine( 221 ...getChangeset( 222 `${keyword}: ${kind === 'with @' ? '@' : ''}other`, 223 data.commit, 224 ), 225 ), 226 ).toEqual( 227 `\n- something ([#1613](https://github.com/hey-api/openapi-ts/pull/1613)) ([\`a085003\`](https://github.com/hey-api/openapi-ts/commit/a085003)) by [@other](https://github.com/other)`, 228 ); 229 }); 230 }, 231 ); 232 233 describe.each([data.commit, 'wrongcommit', undefined])( 234 'with commit from changeset of %s', 235 (commitFromChangeset) => { 236 describe.each(['pr', 'pull request', 'pull'])( 237 'override pr with %s keyword', 238 (keyword) => { 239 it.each(['with #', 'without #'])('%s', async (kind) => { 240 expect( 241 await changelog.getReleaseLine( 242 ...getChangeset( 243 `${keyword}: ${kind === 'with #' ? '#' : ''}${data.pull}`, 244 commitFromChangeset, 245 ), 246 ), 247 ).toEqual( 248 `\n- something ([#1613](https://github.com/hey-api/openapi-ts/pull/1613)) ([\`a085003\`](https://github.com/hey-api/openapi-ts/commit/a085003)) by [@someone](https://github.com/someone)`, 249 ); 250 }); 251 }, 252 ); 253 254 it('override commit with commit keyword', async () => { 255 expect( 256 await changelog.getReleaseLine( 257 ...getChangeset(`commit: ${data.commit}`, commitFromChangeset), 258 ), 259 ).toEqual( 260 `\n- something ([#1613](https://github.com/hey-api/openapi-ts/pull/1613)) ([\`a085003\`](https://github.com/hey-api/openapi-ts/commit/a085003)) by [@someone](https://github.com/someone)`, 261 ); 262 }); 263 }, 264 ); 265});