|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +// eslint-disable-next-line depend/ban-dependencies |
| 4 | +import { execaCommand as rawExecaCommand } from 'execa'; |
| 5 | +import { detect as rawDetect } from 'package-manager-detector'; |
| 6 | + |
| 7 | +import { getPackageManagerInfo } from './get-package-manager-info'; |
| 8 | + |
| 9 | +vi.mock('execa', async () => { |
| 10 | + return { |
| 11 | + execaCommand: vi.fn(), |
| 12 | + }; |
| 13 | +}); |
| 14 | + |
| 15 | +vi.mock('package-manager-detector', async () => { |
| 16 | + return { |
| 17 | + detect: vi.fn(), |
| 18 | + }; |
| 19 | +}); |
| 20 | + |
| 21 | +vi.mock('../common', async () => { |
| 22 | + return { |
| 23 | + getProjectRoot: () => '/mock/project/root', |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
| 27 | +const execaCommand = vi.mocked(rawExecaCommand); |
| 28 | +const detect = vi.mocked(rawDetect); |
| 29 | + |
| 30 | +beforeEach(() => { |
| 31 | + execaCommand.mockReset(); |
| 32 | + detect.mockReset(); |
| 33 | +}); |
| 34 | + |
| 35 | +describe('getPackageManagerInfo', () => { |
| 36 | + describe('when no package manager is detected', () => { |
| 37 | + it('should return undefined', async () => { |
| 38 | + detect.mockResolvedValue(null); |
| 39 | + |
| 40 | + const result = await getPackageManagerInfo(); |
| 41 | + |
| 42 | + expect(result).toBeUndefined(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('when yarn is detected', () => { |
| 47 | + beforeEach(() => { |
| 48 | + detect.mockResolvedValue({ |
| 49 | + name: 'yarn', |
| 50 | + version: '3.6.0', |
| 51 | + agent: 'yarn@berry', |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should return yarn info with default nodeLinker when command fails', async () => { |
| 56 | + execaCommand.mockRejectedValue(new Error('Command failed')); |
| 57 | + |
| 58 | + const result = await getPackageManagerInfo(); |
| 59 | + |
| 60 | + expect(result).toEqual({ |
| 61 | + type: 'yarn', |
| 62 | + version: '3.6.0', |
| 63 | + agent: 'yarn@berry', |
| 64 | + nodeLinker: 'node_modules', |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should return yarn info with node_modules nodeLinker', async () => { |
| 69 | + execaCommand.mockResolvedValue({ |
| 70 | + stdout: 'node_modules\n', |
| 71 | + } as any); |
| 72 | + |
| 73 | + const result = await getPackageManagerInfo(); |
| 74 | + |
| 75 | + expect(result).toEqual({ |
| 76 | + type: 'yarn', |
| 77 | + version: '3.6.0', |
| 78 | + agent: 'yarn@berry', |
| 79 | + nodeLinker: 'node_modules', |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should return yarn info with pnp nodeLinker', async () => { |
| 84 | + execaCommand.mockResolvedValue({ |
| 85 | + stdout: 'pnp\n', |
| 86 | + } as any); |
| 87 | + |
| 88 | + const result = await getPackageManagerInfo(); |
| 89 | + |
| 90 | + expect(result).toEqual({ |
| 91 | + type: 'yarn', |
| 92 | + version: '3.6.0', |
| 93 | + agent: 'yarn@berry', |
| 94 | + nodeLinker: 'pnp', |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('when pnpm is detected', () => { |
| 100 | + beforeEach(() => { |
| 101 | + detect.mockResolvedValue({ |
| 102 | + name: 'pnpm', |
| 103 | + version: '8.15.0', |
| 104 | + agent: 'pnpm', |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should return pnpm info with default isolated nodeLinker when command fails', async () => { |
| 109 | + execaCommand.mockRejectedValue(new Error('Command failed')); |
| 110 | + |
| 111 | + const result = await getPackageManagerInfo(); |
| 112 | + |
| 113 | + expect(result).toEqual({ |
| 114 | + type: 'pnpm', |
| 115 | + version: '8.15.0', |
| 116 | + agent: 'pnpm', |
| 117 | + nodeLinker: 'node_modules', |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should return pnpm info with isolated nodeLinker', async () => { |
| 122 | + execaCommand.mockResolvedValue({ |
| 123 | + stdout: 'isolated\n', |
| 124 | + } as any); |
| 125 | + |
| 126 | + const result = await getPackageManagerInfo(); |
| 127 | + |
| 128 | + expect(result).toEqual({ |
| 129 | + type: 'pnpm', |
| 130 | + version: '8.15.0', |
| 131 | + agent: 'pnpm', |
| 132 | + nodeLinker: 'isolated', |
| 133 | + }); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('when npm is detected', () => { |
| 138 | + beforeEach(() => { |
| 139 | + detect.mockResolvedValue({ |
| 140 | + name: 'npm', |
| 141 | + version: '9.8.0', |
| 142 | + agent: 'npm', |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should return npm info with default node_modules nodeLinker', async () => { |
| 147 | + const result = await getPackageManagerInfo(); |
| 148 | + |
| 149 | + expect(result).toEqual({ |
| 150 | + type: 'npm', |
| 151 | + version: '9.8.0', |
| 152 | + agent: 'npm', |
| 153 | + nodeLinker: 'node_modules', |
| 154 | + }); |
| 155 | + expect(execaCommand).not.toHaveBeenCalled(); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + describe('when bun is detected', () => { |
| 160 | + beforeEach(() => { |
| 161 | + detect.mockResolvedValue({ |
| 162 | + name: 'bun', |
| 163 | + version: '1.0.0', |
| 164 | + agent: 'bun', |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should return bun info with default node_modules nodeLinker', async () => { |
| 169 | + const result = await getPackageManagerInfo(); |
| 170 | + |
| 171 | + expect(result).toEqual({ |
| 172 | + type: 'bun', |
| 173 | + version: '1.0.0', |
| 174 | + agent: 'bun', |
| 175 | + nodeLinker: 'node_modules', |
| 176 | + }); |
| 177 | + expect(execaCommand).not.toHaveBeenCalled(); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + describe('error handling', () => { |
| 182 | + beforeEach(() => { |
| 183 | + detect.mockResolvedValue({ |
| 184 | + name: 'yarn', |
| 185 | + version: '3.6.0', |
| 186 | + agent: 'yarn', |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should handle yarn command errors gracefully', async () => { |
| 191 | + execaCommand.mockRejectedValue(new Error('yarn command not found')); |
| 192 | + |
| 193 | + const result = await getPackageManagerInfo(); |
| 194 | + |
| 195 | + expect(result).toEqual({ |
| 196 | + type: 'yarn', |
| 197 | + version: '3.6.0', |
| 198 | + agent: 'yarn', |
| 199 | + nodeLinker: 'node_modules', |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should handle pnpm command errors gracefully', async () => { |
| 204 | + detect.mockResolvedValue({ |
| 205 | + name: 'pnpm', |
| 206 | + version: '8.15.0', |
| 207 | + agent: 'pnpm', |
| 208 | + }); |
| 209 | + execaCommand.mockRejectedValue(new Error('pnpm command not found')); |
| 210 | + |
| 211 | + const result = await getPackageManagerInfo(); |
| 212 | + |
| 213 | + expect(result).toEqual({ |
| 214 | + type: 'pnpm', |
| 215 | + version: '8.15.0', |
| 216 | + agent: 'pnpm', |
| 217 | + nodeLinker: 'node_modules', |
| 218 | + }); |
| 219 | + }); |
| 220 | + }); |
| 221 | +}); |
0 commit comments