|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + */ |
| 9 | + |
| 10 | +const {getPackageVersionStrByTag, publishPackage} = require('../npm-utils'); |
| 11 | + |
| 12 | +const execMock = jest.fn(); |
| 13 | +jest.mock('shelljs', () => ({ |
| 14 | + exec: execMock, |
| 15 | +})); |
| 16 | + |
| 17 | +describe('npm-utils', () => { |
| 18 | + beforeEach(() => { |
| 19 | + jest.resetModules(); |
| 20 | + jest.resetAllMocks(); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('getPackageVersionStrByTag', () => { |
| 24 | + it('should return package version string', () => { |
| 25 | + execMock.mockImplementationOnce(() => ({code: 0, stdout: '0.34.2 \n'})); |
| 26 | + const versionStr = getPackageVersionStrByTag('my-package', 'next'); |
| 27 | + expect(versionStr).toBe('0.34.2'); |
| 28 | + }); |
| 29 | + it('should throw error when invalid result', () => { |
| 30 | + execMock.mockImplementationOnce(() => ({ |
| 31 | + code: 1, |
| 32 | + stderr: 'Some error message', |
| 33 | + })); |
| 34 | + |
| 35 | + expect(() => { |
| 36 | + getPackageVersionStrByTag('my-package', 'next'); |
| 37 | + }).toThrow('Failed to get next version from npm\nSome error message'); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('publishPackage', () => { |
| 42 | + it('should run publish command', () => { |
| 43 | + publishPackage( |
| 44 | + 'path/to/my-package', |
| 45 | + {tag: 'latest', otp: 'otp'}, |
| 46 | + {silent: true, cwd: 'i/expect/this/to/be/overriden'}, |
| 47 | + ); |
| 48 | + expect(execMock).toHaveBeenCalledWith( |
| 49 | + 'npm publish --tag latest --otp otp', |
| 50 | + {silent: true, cwd: 'path/to/my-package'}, |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should run publish command when no execOptions', () => { |
| 55 | + publishPackage('path/to/my-package', {tag: 'latest', otp: 'otp'}); |
| 56 | + expect(execMock).toHaveBeenCalledWith( |
| 57 | + 'npm publish --tag latest --otp otp', |
| 58 | + {cwd: 'path/to/my-package'}, |
| 59 | + ); |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
0 commit comments