|
1 | 1 | import { describe, expect, it, afterEach, jest } from '@jest/globals'; |
2 | 2 | import fs from 'fs'; |
3 | 3 | import path from 'path'; |
4 | | -import { addGitObserver, clearGitObservers } from 'workspace-tools'; |
| 4 | +import { addGitObserver, catalogsToYaml, clearGitObservers, type Catalogs } from 'workspace-tools'; |
5 | 5 | import { generateChangeFiles, getChangeFiles } from '../__fixtures__/changeFiles'; |
6 | 6 | import { defaultBranchName, defaultRemoteBranchName } from '../__fixtures__/gitDefaults'; |
7 | 7 | import { initMockLogs } from '../__fixtures__/mockLogs'; |
8 | 8 | import type { Repository } from '../__fixtures__/repository'; |
9 | | -import { type PackageJsonFixture, RepositoryFactory } from '../__fixtures__/repositoryFactory'; |
| 9 | +import { type PackageJsonFixture, type RepoFixture, RepositoryFactory } from '../__fixtures__/repositoryFactory'; |
10 | 10 | import { publish } from '../commands/publish'; |
11 | 11 | import type { ParsedOptions, RepoOptions } from '../types/BeachballOptions'; |
12 | 12 | import { _mockNpmPublish, initNpmMock } from '../__fixtures__/mockNpm'; |
@@ -62,6 +62,7 @@ describe('publish command (e2e)', () => { |
62 | 62 | deepFreezeProperties(context.bumpInfo); |
63 | 63 | deepFreezeProperties(context.originalPackageInfos); |
64 | 64 | await publish(parsedOptions.options, context); |
| 65 | + return context; |
65 | 66 | } |
66 | 67 |
|
67 | 68 | afterEach(() => { |
@@ -334,6 +335,72 @@ describe('publish command (e2e)', () => { |
334 | 335 | expect(newPackageInfos.foo.version).toBe('1.0.0'); |
335 | 336 | }); |
336 | 337 |
|
| 338 | + // Combine workspace/catalog/file cases since these tests are slow |
| 339 | + it('publishes packages with workspace: and catalog: deps and replaces versions', async () => { |
| 340 | + const monorepo: RepoFixture['folders'] = { |
| 341 | + packages: { |
| 342 | + 'pkg-1': { version: '1.0.0' }, |
| 343 | + 'pkg-2': { version: '1.0.0', dependencies: { 'pkg-1': 'workspace:~', react: 'catalog:react18' } }, |
| 344 | + 'pkg-3': { version: '1.0.0', dependencies: { 'pkg-2': 'workspace:^1.0.0' } }, |
| 345 | + 'pkg-4': { |
| 346 | + version: '1.0.0', |
| 347 | + dependencies: { 'pkg-1': 'catalog:' }, |
| 348 | + devDependencies: { 'pkg-2': 'file:../pkg-2' }, |
| 349 | + }, |
| 350 | + }, |
| 351 | + }; |
| 352 | + const catalogs: Catalogs = { |
| 353 | + default: { 'pkg-1': 'workspace:~' }, // only yarn supports workspace: inside catalog |
| 354 | + named: { react18: { react: '^18.0.0' } }, |
| 355 | + }; |
| 356 | + repositoryFactory = new RepositoryFactory({ |
| 357 | + folders: monorepo, |
| 358 | + extraFiles: { '.yarnrc.yml': catalogsToYaml(catalogs) }, |
| 359 | + }); |
| 360 | + repo = repositoryFactory.cloneRepository(); |
| 361 | + |
| 362 | + const { options, parsedOptions } = getOptions({ bumpDeps: true, fetch: false }); |
| 363 | + generateChangeFiles([{ packageName: 'pkg-1', type: 'minor' }], options); |
| 364 | + repo.push(); |
| 365 | + |
| 366 | + const { originalPackageInfos } = await publishWrapper(parsedOptions); |
| 367 | + repo.checkout(defaultBranchName); |
| 368 | + repo.pull(); |
| 369 | + |
| 370 | + expect(repo.getCurrentTags()).toEqual(['pkg-1_v1.1.0', 'pkg-2_v1.0.1', 'pkg-3_v1.0.1', 'pkg-4_v1.0.1']); |
| 371 | + |
| 372 | + // All the dependent packages are bumped despite the workspace: dep specs. |
| 373 | + // The literal workspace: specs are preserved in git. |
| 374 | + const packageInfos = getPackageInfos(parsedOptions); |
| 375 | + expect(packageInfos['pkg-1'].version).toBe('1.1.0'); |
| 376 | + // workspace:~ and catalog: ranges aren't changed |
| 377 | + expect(packageInfos['pkg-2']).toEqual({ ...originalPackageInfos['pkg-2'], version: '1.0.1' }); |
| 378 | + expect(packageInfos['pkg-2'].version).toBe('1.0.1'); |
| 379 | + // workspace: range with number is updated |
| 380 | + expect(packageInfos['pkg-3'].dependencies).toEqual({ 'pkg-2': 'workspace:^1.0.1' }); |
| 381 | + // catalog: range isn't changed |
| 382 | + expect(packageInfos['pkg-4']).toEqual({ ...originalPackageInfos['pkg-4'], version: '1.0.1' }); |
| 383 | + |
| 384 | + // The changelogs are adequately covered by the similar bump test. |
| 385 | + |
| 386 | + // Verify that the published packages have the actual resolved versions |
| 387 | + expect(npmMock.getPublishedPackage('pkg-1')).toMatchObject({ version: '1.1.0' }); |
| 388 | + expect(npmMock.getPublishedPackage('pkg-2')).toMatchObject({ |
| 389 | + version: '1.0.1', |
| 390 | + dependencies: { 'pkg-1': '~1.1.0', react: '^18.0.0' }, |
| 391 | + }); |
| 392 | + expect(npmMock.getPublishedPackage('pkg-3')).toMatchObject({ |
| 393 | + version: '1.0.1', |
| 394 | + dependencies: { 'pkg-2': '^1.0.1' }, |
| 395 | + }); |
| 396 | + expect(npmMock.getPublishedPackage('pkg-4')).toMatchObject({ |
| 397 | + version: '1.0.1', |
| 398 | + dependencies: { 'pkg-1': '~1.1.0' }, |
| 399 | + // file: deps aren't currently replaced (definitely fine for dev deps, questionable for prod) |
| 400 | + devDependencies: { 'pkg-2': 'file:../pkg-2' }, |
| 401 | + }); |
| 402 | + }); |
| 403 | + |
337 | 404 | // These tests are slow, so combine pre and post hooks |
338 | 405 | it('respects prepublish/postpublish hooks', async () => { |
339 | 406 | repositoryFactory = new RepositoryFactory('monorepo'); |
@@ -499,6 +566,7 @@ describe('publish command (e2e)', () => { |
499 | 566 | }); |
500 | 567 |
|
501 | 568 | // Just test postpublish (prepublish should have the same logic) |
| 569 | + // TODO: possibly move to in-memory test |
502 | 570 | it('respects concurrency limit for publish hooks', async () => { |
503 | 571 | const packagesToPublish = ['pkg1', 'pkg2', 'pkg3', 'pkg4']; |
504 | 572 | type ExtraPackageJsonFixture = PackageJsonFixture & { customAfterPublish?: { notify: string } }; |
|
0 commit comments