|
| 1 | +import { afterEach } from "node:test"; |
| 2 | +import { exportCatalogs as exportCatalogsActual } from "../../src/commands/exportCatalogs.ts"; |
| 3 | +import type { Preset } from "../../src/types.ts"; |
| 4 | + |
| 5 | +describe("exportCatalogs()", () => { |
| 6 | + const fs = require("../__mocks__/fs.js"); |
| 7 | + |
| 8 | + const preset = { |
| 9 | + "0.82": { |
| 10 | + react: { |
| 11 | + name: "react", |
| 12 | + version: "19.1.1", |
| 13 | + }, |
| 14 | + core: { |
| 15 | + name: "react-native", |
| 16 | + version: "^0.82.0", |
| 17 | + }, |
| 18 | + }, |
| 19 | + "1.0": { |
| 20 | + react: { |
| 21 | + name: "react", |
| 22 | + version: "19.2.0", |
| 23 | + }, |
| 24 | + core: { |
| 25 | + name: "react-native", |
| 26 | + version: "^1.0.0", |
| 27 | + }, |
| 28 | + }, |
| 29 | + } as unknown as Preset; |
| 30 | + |
| 31 | + function exportCatalogs(dst: string, preset: Preset) { |
| 32 | + return exportCatalogsActual(dst, preset, fs); |
| 33 | + } |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + fs.__setMockContent({}); |
| 37 | + fs.__setMockFileWriter(undefined); |
| 38 | + }); |
| 39 | + |
| 40 | + it("throws for unsupported file formats", () => { |
| 41 | + const output = "catalogs.bin"; |
| 42 | + |
| 43 | + expect(() => exportCatalogs(output, preset)).toThrow( |
| 44 | + "Unsupported file format: .bin" |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it("supports pnpm catalogs", () => { |
| 49 | + const output = "catalogs.yaml"; |
| 50 | + |
| 51 | + fs.__setMockFileWriter((dst: string, content: string) => { |
| 52 | + expect(dst).toBe(output); |
| 53 | + expect(content).toBe(`catalogs: |
| 54 | + "0.82": |
| 55 | + react: 19.1.1 |
| 56 | + react-native: ^0.82.0 |
| 57 | + "1.0": |
| 58 | + react: 19.2.0 |
| 59 | + react-native: ^1.0.0 |
| 60 | +`); |
| 61 | + }); |
| 62 | + |
| 63 | + exportCatalogs(output, preset); |
| 64 | + }); |
| 65 | + |
| 66 | + it("preserves existing content in the output file", () => { |
| 67 | + const output = "catalogs.yaml"; |
| 68 | + |
| 69 | + fs.__setMockContent(` |
| 70 | +enableScripts: false |
| 71 | +globalFolder: .yarn/berry |
| 72 | +nodeLinker: pnpm |
| 73 | +catalogs: |
| 74 | + "0.81": |
| 75 | + react: 19.0.0 |
| 76 | + react-native: ^0.81.0 |
| 77 | + "0.82": |
| 78 | + react: 19.1.1 |
| 79 | + react-native: ^0.82.0-0 |
| 80 | +`); |
| 81 | + |
| 82 | + fs.__setMockFileWriter((dst: string, content: string) => { |
| 83 | + expect(dst).toBe(output); |
| 84 | + expect(content).toBe(`enableScripts: false |
| 85 | +globalFolder: .yarn/berry |
| 86 | +nodeLinker: pnpm |
| 87 | +catalogs: |
| 88 | + "0.81": |
| 89 | + react: 19.0.0 |
| 90 | + react-native: ^0.81.0 |
| 91 | + "0.82": |
| 92 | + react: 19.1.1 |
| 93 | + react-native: ^0.82.0 |
| 94 | + "1.0": |
| 95 | + react: 19.2.0 |
| 96 | + react-native: ^1.0.0 |
| 97 | +`); |
| 98 | + }); |
| 99 | + |
| 100 | + exportCatalogs(output, preset); |
| 101 | + }); |
| 102 | + |
| 103 | + it("supports Bun catalogs (under 'workspaces')", () => { |
| 104 | + const output = "package.json"; |
| 105 | + |
| 106 | + fs.__setMockContent({ |
| 107 | + name: "my-monorepo", |
| 108 | + workspaces: { |
| 109 | + packages: ["packages/*"], |
| 110 | + catalog: { |
| 111 | + react: "^19.0.0", |
| 112 | + "react-dom": "^19.0.0", |
| 113 | + }, |
| 114 | + catalogs: { |
| 115 | + testing: { |
| 116 | + jest: "30.0.0", |
| 117 | + "testing-library": "14.0.0", |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + fs.__setMockFileWriter((dst: string, content: string) => { |
| 124 | + expect(dst).toBe(output); |
| 125 | + expect(content).toBe(`{ |
| 126 | + "name": "my-monorepo", |
| 127 | + "workspaces": { |
| 128 | + "packages": [ |
| 129 | + "packages/*" |
| 130 | + ], |
| 131 | + "catalog": { |
| 132 | + "react": "^19.0.0", |
| 133 | + "react-dom": "^19.0.0" |
| 134 | + }, |
| 135 | + "catalogs": { |
| 136 | + "testing": { |
| 137 | + "jest": "30.0.0", |
| 138 | + "testing-library": "14.0.0" |
| 139 | + }, |
| 140 | + "0.82": { |
| 141 | + "react": "19.1.1", |
| 142 | + "react-native": "^0.82.0" |
| 143 | + }, |
| 144 | + "1.0": { |
| 145 | + "react": "19.2.0", |
| 146 | + "react-native": "^1.0.0" |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | +`); |
| 152 | + }); |
| 153 | + |
| 154 | + exportCatalogs(output, preset); |
| 155 | + }); |
| 156 | + |
| 157 | + it("supports Bun catalogs (at the root level)", () => { |
| 158 | + const output = "package.json"; |
| 159 | + |
| 160 | + fs.__setMockContent({ |
| 161 | + name: "my-monorepo", |
| 162 | + workspaces: { |
| 163 | + packages: ["packages/*"], |
| 164 | + }, |
| 165 | + catalog: { |
| 166 | + react: "^19.0.0", |
| 167 | + "react-dom": "^19.0.0", |
| 168 | + }, |
| 169 | + }); |
| 170 | + |
| 171 | + fs.__setMockFileWriter((dst: string, content: string) => { |
| 172 | + expect(dst).toBe(output); |
| 173 | + expect(content).toBe(`{ |
| 174 | + "name": "my-monorepo", |
| 175 | + "workspaces": { |
| 176 | + "packages": [ |
| 177 | + "packages/*" |
| 178 | + ] |
| 179 | + }, |
| 180 | + "catalog": { |
| 181 | + "react": "^19.0.0", |
| 182 | + "react-dom": "^19.0.0" |
| 183 | + }, |
| 184 | + "catalogs": { |
| 185 | + "0.82": { |
| 186 | + "react": "19.1.1", |
| 187 | + "react-native": "^0.82.0" |
| 188 | + }, |
| 189 | + "1.0": { |
| 190 | + "react": "19.2.0", |
| 191 | + "react-native": "^1.0.0" |
| 192 | + } |
| 193 | + } |
| 194 | +} |
| 195 | +`); |
| 196 | + }); |
| 197 | + |
| 198 | + exportCatalogs(output, preset); |
| 199 | + }); |
| 200 | + |
| 201 | + it("prefers catalogs under 'workspaces'", () => { |
| 202 | + const output = "package.json"; |
| 203 | + |
| 204 | + fs.__setMockContent({ name: "my-monorepo" }); |
| 205 | + |
| 206 | + fs.__setMockFileWriter((dst: string, content: string) => { |
| 207 | + expect(dst).toBe(output); |
| 208 | + expect(content).toBe(`{ |
| 209 | + "name": "my-monorepo", |
| 210 | + "workspaces": { |
| 211 | + "catalogs": { |
| 212 | + "0.82": { |
| 213 | + "react": "19.1.1", |
| 214 | + "react-native": "^0.82.0" |
| 215 | + }, |
| 216 | + "1.0": { |
| 217 | + "react": "19.2.0", |
| 218 | + "react-native": "^1.0.0" |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | +} |
| 223 | +`); |
| 224 | + }); |
| 225 | + |
| 226 | + exportCatalogs(output, preset); |
| 227 | + }); |
| 228 | +}); |
0 commit comments