|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +jest.mock('fs'); |
| 5 | +jest.mock('child_process'); |
| 6 | +const {versionAndPublish} = require('../src/publish'); |
| 7 | + |
| 8 | +const mockFs = require('fs'); |
| 9 | + |
| 10 | +jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 11 | +jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 12 | +jest.spyOn(process, 'exit').mockImplementation(() => {}); |
| 13 | + |
| 14 | +describe('versionAndPublish', () => { |
| 15 | + const packageJsonPath = 'packages/contact-center/test-workspace/package.json'; |
| 16 | + beforeEach(() => { |
| 17 | + jest.resetAllMocks(); |
| 18 | + }); |
| 19 | + test('Exits if we dont have enough arguments', () => { |
| 20 | + jest.spyOn(process, 'exit').mockImplementation(() => {}); |
| 21 | + process.argv = ['node', 'script.js', 'main']; |
| 22 | + versionAndPublish(); |
| 23 | + |
| 24 | + expect(console.error).toHaveBeenCalledWith( |
| 25 | + 'Error: Not enough positional arguments provided! node <relative_path_to_publish> <branchName> <nextVersion>' |
| 26 | + ); |
| 27 | + expect(process.exit).toHaveBeenCalledWith(1); |
| 28 | + |
| 29 | + process.argv = ['node', 'script.js', '1.2.3-test.12']; |
| 30 | + versionAndPublish(); |
| 31 | + |
| 32 | + expect(console.error).toHaveBeenCalledWith( |
| 33 | + 'Error: Not enough positional arguments provided! node <relative_path_to_publish> <branchName> <nextVersion>' |
| 34 | + ); |
| 35 | + expect(process.exit).toHaveBeenCalledWith(1); |
| 36 | + }); |
| 37 | + |
| 38 | + test('removes "stableVersion" key from package.json when it exists', () => { |
| 39 | + const packageJsonContent = JSON.stringify({ |
| 40 | + name: 'test-package', |
| 41 | + version: '1.0.0', |
| 42 | + stableVersion: '1.0.0', |
| 43 | + }); |
| 44 | + jest.spyOn(fs, 'readdirSync').mockReturnValue([{name: 'test-workspace', isDirectory: () => true}]); |
| 45 | + jest.spyOn(fs.Dirent.prototype, 'isDirectory').mockReturnValue(true); |
| 46 | + mockFs.readFileSync.mockReturnValue(packageJsonContent); |
| 47 | + mockFs.writeFileSync.mockImplementation(() => {}); |
| 48 | + |
| 49 | + process.argv = ['node', 'script.js', 'main', '1.3.3-test.1']; |
| 50 | + versionAndPublish(); |
| 51 | + |
| 52 | + const expectedPackageJsonContent = JSON.stringify({name: 'test-package', version: '1.0.0'}, null, 2); |
| 53 | + |
| 54 | + expect(mockFs.writeFileSync).toHaveBeenCalledWith(packageJsonPath, expectedPackageJsonContent, 'utf-8'); |
| 55 | + expect(console.log).toHaveBeenCalledWith("'stableVersion' key removed successfully."); |
| 56 | + }); |
| 57 | + |
| 58 | + test('error occured while writting "stableVersion" key to', () => { |
| 59 | + const packageJsonContent = JSON.stringify({ |
| 60 | + name: 'test-package', |
| 61 | + version: '1.0.0', |
| 62 | + stableVersion: '1.0.0', |
| 63 | + }); |
| 64 | + jest.spyOn(fs, 'readdirSync').mockReturnValue([{name: 'test-workspace', isDirectory: () => true}]); |
| 65 | + jest.spyOn(fs.Dirent.prototype, 'isDirectory').mockReturnValue(true); |
| 66 | + mockFs.writeFileSync.mockImplementation(() => { |
| 67 | + throw new Error('Error while writing to file'); |
| 68 | + }); |
| 69 | + |
| 70 | + process.argv = ['node', 'script.js', 'main', '1.3.3-test.1']; |
| 71 | + versionAndPublish(); |
| 72 | + |
| 73 | + expect(console.error).toHaveBeenCalledWith( |
| 74 | + "An error occurred while removing 'stableVersion':", |
| 75 | + '"undefined" is not valid JSON' |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + test('skips removing "stableVersion" if it does not exist', () => { |
| 80 | + const packageJsonContent = JSON.stringify({ |
| 81 | + name: 'test-package', |
| 82 | + version: '1.0.0', |
| 83 | + }); |
| 84 | + jest.spyOn(fs, 'readdirSync').mockReturnValue([{name: 'test-workspace', isDirectory: () => true}]); |
| 85 | + jest.spyOn(fs.Dirent.prototype, 'isDirectory').mockReturnValue(true); |
| 86 | + mockFs.readFileSync.mockReturnValue(packageJsonContent); |
| 87 | + mockFs.writeFileSync.mockImplementation(() => {}); |
| 88 | + |
| 89 | + process.argv = ['node', 'script.js', 'main', '1.3.3-test.1']; |
| 90 | + versionAndPublish(); |
| 91 | + |
| 92 | + expect(mockFs.writeFileSync).not.toHaveBeenCalled(); |
| 93 | + expect(console.log).toHaveBeenCalledWith("'stableVersion' key does not exist in package.json."); |
| 94 | + }); |
| 95 | + |
| 96 | + test('publishes dependencies first and other workspaces afterward', () => { |
| 97 | + const contactCenterPath = './packages/contact-center'; |
| 98 | + const dependencies = ['@webex/cc-store']; |
| 99 | + |
| 100 | + mockFs.readdirSync.mockReturnValue([ |
| 101 | + {name: 'store', isDirectory: () => true}, |
| 102 | + {name: 'station-login', isDirectory: () => true}, |
| 103 | + ]); |
| 104 | + |
| 105 | + mockFs.readFileSync.mockReturnValue('{}'); |
| 106 | + mockFs.writeFileSync.mockImplementation(() => {}); |
| 107 | + |
| 108 | + const mockExecSync = require('child_process').execSync; |
| 109 | + mockExecSync.mockImplementation(() => {}); |
| 110 | + |
| 111 | + const processArgvMock = ['node', 'script.js', 'main', '1.0.1']; |
| 112 | + process.argv = processArgvMock; |
| 113 | + |
| 114 | + versionAndPublish(); |
| 115 | + |
| 116 | + // Check the dependency workspace was published first |
| 117 | + expect(mockExecSync).toHaveBeenNthCalledWith(1, 'yarn workspace @webex/cc-store version 1.0.1', {stdio: 'inherit'}); |
| 118 | + |
| 119 | + expect(mockExecSync).toHaveBeenNthCalledWith(2, 'yarn workspace @webex/cc-store npm publish --tag main', { |
| 120 | + stdio: 'inherit', |
| 121 | + }); |
| 122 | + |
| 123 | + // Check the non-dependency workspace was published afterward |
| 124 | + expect(mockExecSync).toHaveBeenNthCalledWith(3, 'yarn workspace @webex/cc-station-login version 1.0.1', { |
| 125 | + stdio: 'inherit', |
| 126 | + }); |
| 127 | + |
| 128 | + expect(mockExecSync).toHaveBeenNthCalledWith(4, 'yarn workspace @webex/cc-station-login npm publish --tag main', { |
| 129 | + stdio: 'inherit', |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should export versionAndPublish when required as a module', () => { |
| 134 | + jest.resetModules(); |
| 135 | + // Simulate require.main !== module (require the module as a normal import) |
| 136 | + const script = require('../src/publish'); |
| 137 | + |
| 138 | + // Check if versionAndPublish function is exported |
| 139 | + expect(script).toHaveProperty('versionAndPublish'); |
| 140 | + }); |
| 141 | +}); |
0 commit comments