|
| 1 | +import { join } from 'path'; |
| 2 | +import { |
| 3 | + PackageManagerCommands, |
| 4 | + BunPackageManager, |
| 5 | +} from '../../../lib/package-managers'; |
| 6 | +import { BunRunner } from '../../../lib/runners/bun.runner'; |
| 7 | + |
| 8 | +jest.mock('../../../lib/runners/bun.runner'); |
| 9 | + |
| 10 | +describe('BunPackageManager', () => { |
| 11 | + let packageManager: BunPackageManager; |
| 12 | + beforeEach(() => { |
| 13 | + (BunRunner as any).mockClear(); |
| 14 | + (BunRunner as any).mockImplementation(() => { |
| 15 | + return { |
| 16 | + run: (): Promise<void> => Promise.resolve(), |
| 17 | + }; |
| 18 | + }); |
| 19 | + packageManager = new BunPackageManager(); |
| 20 | + }); |
| 21 | + it('should be created', () => { |
| 22 | + expect(packageManager).toBeInstanceOf(BunPackageManager); |
| 23 | + }); |
| 24 | + it('should have the correct cli commands', () => { |
| 25 | + const expectedValues: PackageManagerCommands = { |
| 26 | + install: 'install', |
| 27 | + add: 'add', |
| 28 | + update: 'install --force', |
| 29 | + remove: 'remove', |
| 30 | + saveFlag: '', |
| 31 | + saveDevFlag: '--development', |
| 32 | + silentFlag: '--silent', |
| 33 | + }; |
| 34 | + expect(packageManager.cli).toMatchObject(expectedValues); |
| 35 | + }); |
| 36 | + describe('install', () => { |
| 37 | + it('should use the proper command for installing', () => { |
| 38 | + const spy = jest.spyOn((packageManager as any).runner, 'run'); |
| 39 | + const dirName = '/tmp'; |
| 40 | + const testDir = join(process.cwd(), dirName); |
| 41 | + packageManager.install(dirName, 'bun'); |
| 42 | + expect(spy).toBeCalledWith('install --silent', true, testDir); |
| 43 | + }); |
| 44 | + }); |
| 45 | + describe('addProduction', () => { |
| 46 | + it('should use the proper command for adding production dependencies', () => { |
| 47 | + const spy = jest.spyOn((packageManager as any).runner, 'run'); |
| 48 | + const dependencies = ['@nestjs/common', '@nestjs/core']; |
| 49 | + const tag = '5.0.0'; |
| 50 | + const command = `add ${dependencies |
| 51 | + .map((dependency) => `${dependency}@${tag}`) |
| 52 | + .join(' ')}`; |
| 53 | + packageManager.addProduction(dependencies, tag); |
| 54 | + expect(spy).toBeCalledWith(command, true); |
| 55 | + }); |
| 56 | + }); |
| 57 | + describe('addDevelopment', () => { |
| 58 | + it('should use the proper command for adding development dependencies', () => { |
| 59 | + const spy = jest.spyOn((packageManager as any).runner, 'run'); |
| 60 | + const dependencies = ['@nestjs/common', '@nestjs/core']; |
| 61 | + const tag = '5.0.0'; |
| 62 | + const command = `add --development ${dependencies |
| 63 | + .map((dependency) => `${dependency}@${tag}`) |
| 64 | + .join(' ')}`; |
| 65 | + packageManager.addDevelopment(dependencies, tag); |
| 66 | + expect(spy).toBeCalledWith(command, true); |
| 67 | + }); |
| 68 | + }); |
| 69 | + describe('updateProduction', () => { |
| 70 | + it('should use the proper command for updating production dependencies', () => { |
| 71 | + const spy = jest.spyOn((packageManager as any).runner, 'run'); |
| 72 | + const dependencies = ['@nestjs/common', '@nestjs/core']; |
| 73 | + const command = `install --force ${dependencies.join(' ')}`; |
| 74 | + packageManager.updateProduction(dependencies); |
| 75 | + expect(spy).toBeCalledWith(command, true); |
| 76 | + }); |
| 77 | + }); |
| 78 | + describe('updateDevelopment', () => { |
| 79 | + it('should use the proper command for updating development dependencies', () => { |
| 80 | + const spy = jest.spyOn((packageManager as any).runner, 'run'); |
| 81 | + const dependencies = ['@nestjs/common', '@nestjs/core']; |
| 82 | + const command = `install --force ${dependencies.join(' ')}`; |
| 83 | + packageManager.updateDevelopment(dependencies); |
| 84 | + expect(spy).toBeCalledWith(command, true); |
| 85 | + }); |
| 86 | + }); |
| 87 | + describe('upgradeProduction', () => { |
| 88 | + it('should use the proper command for upgrading production dependencies', () => { |
| 89 | + const spy = jest.spyOn((packageManager as any).runner, 'run'); |
| 90 | + const dependencies = ['@nestjs/common', '@nestjs/core']; |
| 91 | + const tag = '5.0.0'; |
| 92 | + const uninstallCommand = `remove ${dependencies.join(' ')}`; |
| 93 | + |
| 94 | + const installCommand = `add ${dependencies |
| 95 | + .map((dependency) => `${dependency}@${tag}`) |
| 96 | + .join(' ')}`; |
| 97 | + |
| 98 | + return packageManager.upgradeProduction(dependencies, tag).then(() => { |
| 99 | + expect(spy.mock.calls).toEqual([ |
| 100 | + [uninstallCommand, true], |
| 101 | + [installCommand, true], |
| 102 | + ]); |
| 103 | + }); |
| 104 | + }); |
| 105 | + }); |
| 106 | + describe('upgradeDevelopment', () => { |
| 107 | + it('should use the proper command for upgrading production dependencies', () => { |
| 108 | + const spy = jest.spyOn((packageManager as any).runner, 'run'); |
| 109 | + const dependencies = ['@nestjs/common', '@nestjs/core']; |
| 110 | + const tag = '5.0.0'; |
| 111 | + const uninstallCommand = `remove --development ${dependencies.join(' ')}`; |
| 112 | + |
| 113 | + const installCommand = `add --development ${dependencies |
| 114 | + .map((dependency) => `${dependency}@${tag}`) |
| 115 | + .join(' ')}`; |
| 116 | + |
| 117 | + return packageManager.upgradeDevelopment(dependencies, tag).then(() => { |
| 118 | + expect(spy.mock.calls).toEqual([ |
| 119 | + [uninstallCommand, true], |
| 120 | + [installCommand, true], |
| 121 | + ]); |
| 122 | + }); |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments