|
| 1 | +import test from 'ava' |
| 2 | +import sortPackageJson from '../index.js' |
| 3 | +import { sortPackageJsonAsObject } from './_helpers.js' |
| 4 | + |
| 5 | +test('workspaces array (yarn) should be sorted and unique', (t) => { |
| 6 | + const packageJson = { |
| 7 | + workspaces: ['packages/c', 'packages/a', 'packages/b', 'packages/a'], |
| 8 | + } |
| 9 | + const sorted = sortPackageJson(packageJson) |
| 10 | + t.deepEqual(sorted.workspaces, ['packages/a', 'packages/b', 'packages/c']) |
| 11 | +}) |
| 12 | + |
| 13 | +test('workspaces object should be sorted', (t) => { |
| 14 | + const sortedWorkspaces = sortPackageJsonAsObject({ |
| 15 | + path: 'workspaces', |
| 16 | + value: { |
| 17 | + zoo: '*', // other property |
| 18 | + packages: ['packages/c', 'packages/a', 'packages/b', 'packages/a'], |
| 19 | + catalog: { |
| 20 | + 'is-even': 'npm:is-even@1.0.0', |
| 21 | + 'is-odd': 'npm:is-odd@3.0.1', |
| 22 | + 'alpha-lib': 'npm:alpha-lib@1.0.0', |
| 23 | + }, |
| 24 | + animal: '*', // other property |
| 25 | + }, |
| 26 | + }) |
| 27 | + |
| 28 | + const expect = { |
| 29 | + packages: ['packages/a', 'packages/b', 'packages/c'], |
| 30 | + catalog: { |
| 31 | + 'alpha-lib': 'npm:alpha-lib@1.0.0', |
| 32 | + 'is-even': 'npm:is-even@1.0.0', |
| 33 | + 'is-odd': 'npm:is-odd@3.0.1', |
| 34 | + }, |
| 35 | + animal: '*', |
| 36 | + zoo: '*', |
| 37 | + } |
| 38 | + |
| 39 | + t.deepEqual(sortedWorkspaces, expect) |
| 40 | +}) |
| 41 | + |
| 42 | +test('workspaces with other types should be kept as it is', (t) => { |
| 43 | + for (const value of ['string', false, 2020, undefined, null]) { |
| 44 | + const type = value === null ? 'null' : typeof value |
| 45 | + t.is( |
| 46 | + sortPackageJsonAsObject({ path: 'workspaces', value }), |
| 47 | + value, |
| 48 | + `Should keep ${type} type \`workspaces\` as it is.`, |
| 49 | + ) |
| 50 | + } |
| 51 | +}) |
0 commit comments