|
| 1 | +import { mocked } from 'ts-jest/utils'; |
| 2 | +import { |
| 3 | + checkForValidRuntimeHandlerVersion, |
| 4 | + isExactSemVerVersion, |
| 5 | +} from '../../src/checks/check-runtime-handler'; |
| 6 | +import { logger } from '../../src/utils/logger'; |
| 7 | + |
| 8 | +jest.mock('../../src/utils/logger', () => { |
| 9 | + return { |
| 10 | + logger: { |
| 11 | + error: jest.fn(), |
| 12 | + }, |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +describe('isExactSemVerVersion', () => { |
| 17 | + test('should not accept version ranges', () => { |
| 18 | + [ |
| 19 | + '>=1.2.7', |
| 20 | + '>=1.2.7 <1.3.0', |
| 21 | + '1.2.7 || >=1.2.9 <2.0.0', |
| 22 | + '<1.3.0', |
| 23 | + '>1.2.3-alpha.3', |
| 24 | + '1.2.3 - 2.3', |
| 25 | + '>=1.2.3 <2.4.0-0', |
| 26 | + '1.x', |
| 27 | + '*', |
| 28 | + '~1.2.3', |
| 29 | + '~1.2', |
| 30 | + '~1.2.3-beta.2', |
| 31 | + '^1.2.3', |
| 32 | + '1.0', |
| 33 | + '1', |
| 34 | + 'next', |
| 35 | + ].forEach((version) => { |
| 36 | + expect(isExactSemVerVersion(version)).toEqual(false); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + test('should return true for exact versions', () => { |
| 41 | + ['1.1.0', '1.1.0-rc.1', '1.2.0-alpha.3'].forEach((version) => { |
| 42 | + expect(isExactSemVerVersion(version)).toEqual(true); |
| 43 | + }); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe('checkForValidRuntimeHandlerversion', () => { |
| 48 | + test('should by default accept a missing runtime-handler', () => { |
| 49 | + expect(checkForValidRuntimeHandlerVersion({})).toEqual(true); |
| 50 | + expect(checkForValidRuntimeHandlerVersion({ dependencies: {} })).toEqual( |
| 51 | + true |
| 52 | + ); |
| 53 | + expect(checkForValidRuntimeHandlerVersion({ devDependencies: {} })).toEqual( |
| 54 | + true |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + test('should return false if missing runtime-handler and flag passed', () => { |
| 59 | + expect(checkForValidRuntimeHandlerVersion({}, true)).toEqual(false); |
| 60 | + expect( |
| 61 | + checkForValidRuntimeHandlerVersion({ dependencies: {} }, true) |
| 62 | + ).toEqual(false); |
| 63 | + expect( |
| 64 | + checkForValidRuntimeHandlerVersion({ devDependencies: {} }, true) |
| 65 | + ).toEqual(false); |
| 66 | + |
| 67 | + expect(mocked(logger.error)).toHaveBeenCalledTimes(3); |
| 68 | + expect(mocked(logger.error).mock.calls[0][1]).toEqual( |
| 69 | + 'Missing @twilio/runtime-handler declaration' |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + test('should detect if your runtime-handler is defined in devDependencies', () => { |
| 74 | + expect( |
| 75 | + checkForValidRuntimeHandlerVersion({ |
| 76 | + devDependencies: { '@twilio/runtime-handler': '1.2.0' }, |
| 77 | + }) |
| 78 | + ).toEqual(false); |
| 79 | + |
| 80 | + expect(mocked(logger.error)).toHaveBeenCalledTimes(1); |
| 81 | + expect(mocked(logger.error).mock.calls[0][1]).toEqual( |
| 82 | + 'Wrongly configured @twilio/runtime-handler declaration' |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + test('should specify the right version if possible', () => { |
| 87 | + expect( |
| 88 | + checkForValidRuntimeHandlerVersion({ |
| 89 | + dependencies: { '@twilio/runtime-handler': '~1.2.0' }, |
| 90 | + }) |
| 91 | + ).toEqual(false); |
| 92 | + |
| 93 | + expect(mocked(logger.error)).toHaveBeenCalledTimes(1); |
| 94 | + expect(mocked(logger.error).mock.calls[0][1]).toEqual( |
| 95 | + 'Invalid @twilio/runtime-handler version' |
| 96 | + ); |
| 97 | + expect(mocked(logger.error).mock.calls[0][0].includes('"~1.2.0"')).toEqual( |
| 98 | + true |
| 99 | + ); |
| 100 | + expect(mocked(logger.error).mock.calls[0][0].includes('"1.2.0"')).toEqual( |
| 101 | + true |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + test('should suggest a fallback when version is invalid', () => { |
| 106 | + expect( |
| 107 | + checkForValidRuntimeHandlerVersion({ |
| 108 | + dependencies: { '@twilio/runtime-handler': 'alpha' }, |
| 109 | + }) |
| 110 | + ).toEqual(false); |
| 111 | + |
| 112 | + expect(mocked(logger.error)).toHaveBeenCalledTimes(1); |
| 113 | + expect(mocked(logger.error).mock.calls[0][1]).toEqual( |
| 114 | + 'Invalid @twilio/runtime-handler version' |
| 115 | + ); |
| 116 | + expect(mocked(logger.error).mock.calls[0][0].includes('"alpha"')).toEqual( |
| 117 | + true |
| 118 | + ); |
| 119 | + expect(mocked(logger.error).mock.calls[0][0].includes('"1.1.0"')).toEqual( |
| 120 | + true |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + test('should pass a valid exact version', () => { |
| 125 | + expect( |
| 126 | + checkForValidRuntimeHandlerVersion({ |
| 127 | + dependencies: { '@twilio/runtime-handler': '1.1.0' }, |
| 128 | + }) |
| 129 | + ).toEqual(true); |
| 130 | + |
| 131 | + expect(mocked(logger.error)).toHaveBeenCalledTimes(0); |
| 132 | + }); |
| 133 | +}); |
0 commit comments