// Tests for release-notes.js. Run from the CLI's root with: // node --test .github/scripts/release-notes.test.mjs import assert from 'node:assert/strict' import {execFileSync} from 'node:child_process' import {mkdirSync, mkdtempSync, rmSync, writeFileSync} from 'node:fs' import {tmpdir} from 'node:os' import {dirname, join} from 'node:path' import {fileURLToPath} from 'node:url' import {afterEach, test} from 'node:test' const script = join(dirname(fileURLToPath(import.meta.url)), 'release-notes.js') const scratchDirs = [] afterEach(() => { for (const dir of scratchDirs.splice(0)) rmSync(dir, {recursive: true, force: true}) }) // A repository root with `.changeset/config.json` and one directory per package under `packages`. function repo({fixed, packages}) { const root = mkdtempSync(join(tmpdir(), 'release-notes-')) scratchDirs.push(root) mkdirSync(join(root, '.changeset')) writeFileSync(join(root, '.changeset/config.json'), JSON.stringify({fixed: [fixed]})) for (const [directory, {name, changelog}] of Object.entries(packages)) { mkdirSync(join(root, 'packages', directory), {recursive: true}) writeFileSync(join(root, 'packages', directory, 'package.json'), JSON.stringify({name})) writeFileSync(join(root, 'packages', directory, 'CHANGELOG.md'), changelog) } return root } const releaseNotes = (root, version) => execFileSync('node', [script, version], {cwd: root, encoding: 'utf8'}) test('groups each entry once by area and leaves out dependency updates', () => { const root = repo({ fixed: ['@shopify/cli', '@shopify/app', '@shopify/cli-kit'], packages: { app: { name: '@shopify/app', changelog: '# @shopify/app\n\n## 5.2.0\n\n### Minor Changes\n\n- Add `shopify app logs`.\n- Shared entry.\n\n### Patch Changes\n\n- Updated dependencies\n - @shopify/cli-kit@5.2.0\n\n## 5.1.0\n\n- Older entry.\n', }, cli: { name: '@shopify/cli', changelog: '# @shopify/cli\n\n## 5.2.0\n\n### Minor Changes\n\n- Shared entry.\n', }, 'cli-kit': { name: '@shopify/cli-kit', changelog: '# @shopify/cli-kit\n\n## 5.2.0\n\n### Patch Changes\n\n- abc1234: Revoke tokens on logout.\n', }, }, }) assert.equal( releaseNotes(root, '5.2.0'), "## What's Changed\n\n### App\n* Add `shopify app logs`.\n* Shared entry.\n\n### CLI\n* Revoke tokens on logout.\n", ) }) test('leaves out packages outside the fixed group, whose changelog can have the same version from long ago', () => { const root = repo({ fixed: ['@shopify/cli'], packages: { cli: { name: '@shopify/cli', changelog: '# @shopify/cli\n\n## 5.2.0\n\n### Patch Changes\n\n- Revoke tokens on logout.\n', }, 'ui-extensions-server-kit': { name: '@shopify/ui-extensions-server-kit', changelog: '# @shopify/ui-extensions-server-kit\n\n## 5.5.1\n\n- Newer entry.\n\n## 5.2.0\n\n### Minor Changes\n\n- 1d6fe3475: Increase minimum Node version to 18\n', }, }, }) assert.equal(releaseNotes(root, '5.2.0'), "## What's Changed\n\n### CLI\n* Revoke tokens on logout.\n") }) test('fails instead of printing partial notes when the fixed group is unknown', () => { const packages = {cli: {name: '@shopify/cli', changelog: '# @shopify/cli\n\n## 5.2.0\n\n- An entry.\n'}} const withoutFixed = repo({fixed: [], packages}) writeFileSync(join(withoutFixed, '.changeset/config.json'), '{}') const withoutConfig = repo({fixed: [], packages}) rmSync(join(withoutConfig, '.changeset'), {recursive: true}) for (const root of [withoutFixed, withoutConfig]) { assert.throws(() => execFileSync('node', [script, '5.2.0'], {cwd: root, stdio: 'pipe'})) } })