|
| 1 | +import { log } from '@hey-api/codegen-core'; |
| 2 | + |
| 3 | +import { warnOnConflictingDuplicatePlugins } from '../duplicate'; |
| 4 | + |
| 5 | +describe('warnOnConflictingDuplicatePlugins', () => { |
| 6 | + afterEach(() => { |
| 7 | + vi.restoreAllMocks(); |
| 8 | + }); |
| 9 | + |
| 10 | + const warningMessage = |
| 11 | + 'Plugin "@hey-api/client-fetch" is configured multiple times. Only the last instance will take effect.'; |
| 12 | + |
| 13 | + it('does not warn for duplicate string plugins', () => { |
| 14 | + const warnSpy = vi.spyOn(log, 'warn').mockImplementation(() => {}); |
| 15 | + |
| 16 | + warnOnConflictingDuplicatePlugins(['@hey-api/client-fetch', '@hey-api/client-fetch']); |
| 17 | + |
| 18 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('does not warn for duplicate plugins with identical config in different key order', () => { |
| 22 | + const warnSpy = vi.spyOn(log, 'warn').mockImplementation(() => {}); |
| 23 | + |
| 24 | + warnOnConflictingDuplicatePlugins([ |
| 25 | + { |
| 26 | + foo: 'bar', |
| 27 | + name: '@hey-api/client-fetch', |
| 28 | + output: 'sdk', |
| 29 | + }, |
| 30 | + { |
| 31 | + output: 'sdk', |
| 32 | + // eslint-disable-next-line sort-keys-fix/sort-keys-fix |
| 33 | + foo: 'bar', |
| 34 | + name: '@hey-api/client-fetch', |
| 35 | + }, |
| 36 | + ]); |
| 37 | + |
| 38 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('does not warn when a string and an object with only name are specified', () => { |
| 42 | + const warnSpy = vi.spyOn(log, 'warn').mockImplementation(() => {}); |
| 43 | + |
| 44 | + warnOnConflictingDuplicatePlugins(['@hey-api/client-fetch', { name: '@hey-api/client-fetch' }]); |
| 45 | + |
| 46 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('does not warn for duplicate plugins with identical object config', () => { |
| 50 | + const warnSpy = vi.spyOn(log, 'warn').mockImplementation(() => {}); |
| 51 | + |
| 52 | + warnOnConflictingDuplicatePlugins([ |
| 53 | + { |
| 54 | + foo: 'bar', |
| 55 | + name: '@hey-api/client-fetch', |
| 56 | + }, |
| 57 | + { |
| 58 | + foo: 'bar', |
| 59 | + name: '@hey-api/client-fetch', |
| 60 | + }, |
| 61 | + ]); |
| 62 | + |
| 63 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('does not warn when nested object configs differ only in key order', () => { |
| 67 | + const warnSpy = vi.spyOn(log, 'warn').mockImplementation(() => {}); |
| 68 | + |
| 69 | + warnOnConflictingDuplicatePlugins([ |
| 70 | + { |
| 71 | + definitions: { case: 'PascalCase', name: 'foo' }, |
| 72 | + name: '@hey-api/client-fetch', |
| 73 | + }, |
| 74 | + { |
| 75 | + // eslint-disable-next-line sort-keys-fix/sort-keys-fix |
| 76 | + definitions: { name: 'foo', case: 'PascalCase' }, |
| 77 | + name: '@hey-api/client-fetch', |
| 78 | + }, |
| 79 | + ]); |
| 80 | + |
| 81 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('warns for duplicate plugins with conflicting config', () => { |
| 85 | + const warnSpy = vi.spyOn(log, 'warn').mockImplementation(() => {}); |
| 86 | + |
| 87 | + warnOnConflictingDuplicatePlugins([ |
| 88 | + { |
| 89 | + foo: 'bar', |
| 90 | + name: '@hey-api/client-fetch', |
| 91 | + }, |
| 92 | + { |
| 93 | + foo: 'baz', |
| 94 | + name: '@hey-api/client-fetch', |
| 95 | + }, |
| 96 | + ]); |
| 97 | + |
| 98 | + expect(warnSpy).toHaveBeenCalledOnce(); |
| 99 | + expect(warnSpy).toHaveBeenCalledWith(warningMessage); |
| 100 | + }); |
| 101 | + |
| 102 | + it('warns when a string plugin conflicts with an object plugin of the same name', () => { |
| 103 | + const warnSpy = vi.spyOn(log, 'warn').mockImplementation(() => {}); |
| 104 | + |
| 105 | + warnOnConflictingDuplicatePlugins([ |
| 106 | + '@hey-api/client-fetch', |
| 107 | + { |
| 108 | + name: '@hey-api/client-fetch', |
| 109 | + output: 'sdk', |
| 110 | + }, |
| 111 | + ]); |
| 112 | + |
| 113 | + expect(warnSpy).toHaveBeenCalledOnce(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('does not warn when array-valued options differ only in element key order', () => { |
| 117 | + const warnSpy = vi.spyOn(log, 'warn').mockImplementation(() => {}); |
| 118 | + |
| 119 | + warnOnConflictingDuplicatePlugins([ |
| 120 | + { |
| 121 | + items: [{ from: 'foo', name: 'bar' }], |
| 122 | + name: '@hey-api/client-fetch', |
| 123 | + }, |
| 124 | + { |
| 125 | + // eslint-disable-next-line sort-keys-fix/sort-keys-fix |
| 126 | + items: [{ name: 'bar', from: 'foo' }], |
| 127 | + name: '@hey-api/client-fetch', |
| 128 | + }, |
| 129 | + ]); |
| 130 | + |
| 131 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('warns when array-valued options differ in element order', () => { |
| 135 | + const warnSpy = vi.spyOn(log, 'warn').mockImplementation(() => {}); |
| 136 | + |
| 137 | + warnOnConflictingDuplicatePlugins([ |
| 138 | + { |
| 139 | + items: [ |
| 140 | + { from: 'a', name: 'x' }, |
| 141 | + { from: 'b', name: 'y' }, |
| 142 | + ], |
| 143 | + name: '@hey-api/client-fetch', |
| 144 | + }, |
| 145 | + { |
| 146 | + items: [ |
| 147 | + { from: 'b', name: 'y' }, |
| 148 | + { from: 'a', name: 'x' }, |
| 149 | + ], |
| 150 | + name: '@hey-api/client-fetch', |
| 151 | + }, |
| 152 | + ]); |
| 153 | + |
| 154 | + expect(warnSpy).toHaveBeenCalledOnce(); |
| 155 | + expect(warnSpy).toHaveBeenCalledWith(warningMessage); |
| 156 | + }); |
| 157 | + |
| 158 | + it('does not warn when function-valued options have identical source', () => { |
| 159 | + const warnSpy = vi.spyOn(log, 'warn').mockImplementation(() => {}); |
| 160 | + const transform = (value: string) => value.toUpperCase(); |
| 161 | + |
| 162 | + warnOnConflictingDuplicatePlugins([ |
| 163 | + { |
| 164 | + definitions: { name: transform }, |
| 165 | + name: '@hey-api/client-fetch', |
| 166 | + }, |
| 167 | + { |
| 168 | + definitions: { name: transform }, |
| 169 | + name: '@hey-api/client-fetch', |
| 170 | + }, |
| 171 | + ]); |
| 172 | + |
| 173 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 174 | + }); |
| 175 | + |
| 176 | + it('warns when function-valued options differ', () => { |
| 177 | + const warnSpy = vi.spyOn(log, 'warn').mockImplementation(() => {}); |
| 178 | + |
| 179 | + warnOnConflictingDuplicatePlugins([ |
| 180 | + { |
| 181 | + definitions: { name: (value: string) => value.toUpperCase() }, |
| 182 | + name: '@hey-api/client-fetch', |
| 183 | + }, |
| 184 | + { |
| 185 | + definitions: { name: (value: string) => value.toLowerCase() }, |
| 186 | + name: '@hey-api/client-fetch', |
| 187 | + }, |
| 188 | + ]); |
| 189 | + |
| 190 | + expect(warnSpy).toHaveBeenCalledOnce(); |
| 191 | + expect(warnSpy).toHaveBeenCalledWith(warningMessage); |
| 192 | + }); |
| 193 | +}); |
0 commit comments