|
| 1 | +import { SwcCompiler } from '../../../../lib/compiler/swc/swc-compiler'; |
| 2 | +import { PluginsLoader } from '../../../../lib/compiler/plugins/plugins-loader'; |
| 3 | + |
| 4 | +import * as swcDefaults from '../../../../lib/compiler/defaults/swc-defaults'; |
| 5 | +import * as getValueOrDefault from '../../../../lib/compiler/helpers/get-value-or-default'; |
| 6 | + |
| 7 | +describe('SWC Compiler', () => { |
| 8 | + let compiler: SwcCompiler; |
| 9 | + let swcDefaultsFactoryMock = jest.fn(); |
| 10 | + let getValueOrDefaultMock = jest.fn(); |
| 11 | + let debounceMock = jest.fn(); |
| 12 | + |
| 13 | + const callRunCompiler = async ({ |
| 14 | + configuration, |
| 15 | + tsconfig, |
| 16 | + appName, |
| 17 | + extras, |
| 18 | + onSuccess, |
| 19 | + }: any) => { |
| 20 | + return await compiler.run( |
| 21 | + configuration || {}, |
| 22 | + tsconfig || '', |
| 23 | + appName || '', |
| 24 | + extras || {}, |
| 25 | + onSuccess ?? jest.fn(), |
| 26 | + ); |
| 27 | + }; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + const PluginsLoaderStub = { |
| 31 | + load: () => jest.fn(), |
| 32 | + resolvePluginReferences: () => jest.fn(), |
| 33 | + } as unknown as PluginsLoader; |
| 34 | + |
| 35 | + compiler = new SwcCompiler(PluginsLoaderStub); |
| 36 | + |
| 37 | + (swcDefaults as any).swcDefaultsFactory = swcDefaultsFactoryMock; |
| 38 | + (getValueOrDefault as any).getValueOrDefault = getValueOrDefaultMock; |
| 39 | + |
| 40 | + compiler['runSwc'] = jest.fn(); |
| 41 | + compiler['runTypeChecker'] = jest.fn(); |
| 42 | + compiler['debounce'] = debounceMock; |
| 43 | + compiler['watchFilesInOutDir'] = jest.fn(); |
| 44 | + |
| 45 | + jest.clearAllMocks(); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('run', () => { |
| 49 | + it('should call swcDefaultsFactory with tsOptions and configuration', async () => { |
| 50 | + const fixture = { |
| 51 | + extras: { |
| 52 | + tsOptions: { |
| 53 | + _tsOptionsTest: {}, |
| 54 | + }, |
| 55 | + }, |
| 56 | + configuration: { |
| 57 | + _configurationTest: {}, |
| 58 | + }, |
| 59 | + }; |
| 60 | + |
| 61 | + await callRunCompiler({ |
| 62 | + configuration: fixture.configuration, |
| 63 | + extras: fixture.extras, |
| 64 | + }); |
| 65 | + expect(swcDefaultsFactoryMock).toHaveBeenCalledWith( |
| 66 | + fixture.extras.tsOptions, |
| 67 | + fixture.configuration, |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should call getValueOrDefault with configuration, swcrcPath and appName', async () => { |
| 72 | + const fixture = { |
| 73 | + configuration: '_configurationTest', |
| 74 | + appName: 'appNameTest', |
| 75 | + }; |
| 76 | + |
| 77 | + await callRunCompiler({ |
| 78 | + configuration: fixture.configuration, |
| 79 | + appName: fixture.appName, |
| 80 | + }); |
| 81 | + |
| 82 | + expect(getValueOrDefaultMock).toHaveBeenCalledWith( |
| 83 | + fixture.configuration, |
| 84 | + 'compilerOptions.builder.options.swcrcPath', |
| 85 | + fixture.appName, |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should not call runTypeChecker when extras.typeCheck is false', async () => { |
| 90 | + const fixture = { |
| 91 | + extras: { |
| 92 | + watch: false, |
| 93 | + typeCheck: false, |
| 94 | + tsOptions: null, |
| 95 | + }, |
| 96 | + }; |
| 97 | + |
| 98 | + fixture.extras.watch = true; |
| 99 | + await callRunCompiler({ |
| 100 | + extras: fixture.extras, |
| 101 | + }); |
| 102 | + |
| 103 | + fixture.extras.watch = false; |
| 104 | + await callRunCompiler({ |
| 105 | + extras: fixture.extras, |
| 106 | + }); |
| 107 | + |
| 108 | + expect(compiler['runTypeChecker']).not.toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should call runTypeChecker when extras.typeCheck is true', async () => { |
| 112 | + const fixture = { |
| 113 | + configuration: '_configurationTest', |
| 114 | + tsConfigPath: 'tsConfigPathTest', |
| 115 | + appName: 'appNameTest', |
| 116 | + extras: { |
| 117 | + watch: false, |
| 118 | + typeCheck: true, |
| 119 | + tsOptions: null, |
| 120 | + }, |
| 121 | + }; |
| 122 | + |
| 123 | + fixture.extras.watch = true; |
| 124 | + await callRunCompiler({ |
| 125 | + configuration: fixture.configuration, |
| 126 | + extras: fixture.extras, |
| 127 | + appName: fixture.appName, |
| 128 | + tsconfig: fixture.tsConfigPath, |
| 129 | + }); |
| 130 | + |
| 131 | + fixture.extras.watch = false; |
| 132 | + await callRunCompiler({ |
| 133 | + configuration: fixture.configuration, |
| 134 | + extras: fixture.extras, |
| 135 | + appName: fixture.appName, |
| 136 | + tsconfig: fixture.tsConfigPath, |
| 137 | + }); |
| 138 | + |
| 139 | + expect(compiler['runTypeChecker']).toHaveBeenCalledTimes(2); |
| 140 | + expect(compiler['runTypeChecker']).toHaveBeenCalledWith( |
| 141 | + fixture.configuration, |
| 142 | + fixture.tsConfigPath, |
| 143 | + fixture.appName, |
| 144 | + fixture.extras, |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should call runSwc', async () => { |
| 149 | + swcDefaultsFactoryMock.mockReturnValueOnce('swcOptionsTest'); |
| 150 | + getValueOrDefaultMock.mockReturnValueOnce('swcrcPathTest'); |
| 151 | + |
| 152 | + const fixture = { |
| 153 | + extras: { |
| 154 | + watch: false, |
| 155 | + }, |
| 156 | + }; |
| 157 | + |
| 158 | + fixture.extras.watch = true; |
| 159 | + await callRunCompiler({ |
| 160 | + extras: fixture.extras, |
| 161 | + }); |
| 162 | + |
| 163 | + expect(compiler['runSwc']).toHaveBeenCalledWith( |
| 164 | + 'swcOptionsTest', |
| 165 | + fixture.extras, |
| 166 | + 'swcrcPathTest', |
| 167 | + ); |
| 168 | + |
| 169 | + fixture.extras.watch = false; |
| 170 | + await callRunCompiler({ |
| 171 | + extras: fixture.extras, |
| 172 | + }); |
| 173 | + |
| 174 | + expect(compiler['runSwc']).toHaveBeenCalledWith( |
| 175 | + 'swcOptionsTest', |
| 176 | + fixture.extras, |
| 177 | + 'swcrcPathTest', |
| 178 | + ); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should not call onSuccess method when is not defined', async () => { |
| 182 | + expect(async () => { |
| 183 | + await callRunCompiler({}); |
| 184 | + }).not.toThrow(); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should call onSuccess method when is defined', async () => { |
| 188 | + const onSuccessMock = jest.fn(); |
| 189 | + await callRunCompiler({ |
| 190 | + onSuccess: onSuccessMock, |
| 191 | + extras: { |
| 192 | + watch: false, |
| 193 | + }, |
| 194 | + }); |
| 195 | + |
| 196 | + await callRunCompiler({ |
| 197 | + onSuccess: onSuccessMock, |
| 198 | + extras: { |
| 199 | + watch: true, |
| 200 | + }, |
| 201 | + }); |
| 202 | + |
| 203 | + expect(onSuccessMock).toHaveBeenCalledTimes(2); |
| 204 | + }); |
| 205 | + |
| 206 | + it('should not call watchFilesInOutDir or debounce method when extras.watch is false', async () => { |
| 207 | + await callRunCompiler({ |
| 208 | + extras: { |
| 209 | + watch: false, |
| 210 | + }, |
| 211 | + }); |
| 212 | + |
| 213 | + expect(compiler['watchFilesInOutDir']).not.toHaveBeenCalled(); |
| 214 | + expect(compiler['debounce']).not.toHaveBeenCalled(); |
| 215 | + }); |
| 216 | + |
| 217 | + it('should not call watchFilesInOutDir or debounce method when extras.watch is true but onSuccess is not defined', async () => { |
| 218 | + await callRunCompiler({ |
| 219 | + extras: { |
| 220 | + watch: true, |
| 221 | + }, |
| 222 | + onSuccess: false, |
| 223 | + }); |
| 224 | + |
| 225 | + expect(compiler['watchFilesInOutDir']).not.toHaveBeenCalled(); |
| 226 | + expect(compiler['debounce']).not.toHaveBeenCalled(); |
| 227 | + }); |
| 228 | + |
| 229 | + it('should call debounce method with debounceTime and onSuccess method and when extras.watch is true', async () => { |
| 230 | + const fixture = { |
| 231 | + onSuccess: jest.fn(), |
| 232 | + }; |
| 233 | + |
| 234 | + await callRunCompiler({ |
| 235 | + onSuccess: fixture.onSuccess, |
| 236 | + extras: { |
| 237 | + watch: true, |
| 238 | + }, |
| 239 | + }); |
| 240 | + |
| 241 | + expect(compiler['debounce']).toHaveBeenCalledWith(fixture.onSuccess, 150); |
| 242 | + }); |
| 243 | + |
| 244 | + it('should call watchFilesInOutDir method with swcOptions and callback when extras.watch is true', async () => { |
| 245 | + swcDefaultsFactoryMock.mockReturnValueOnce('swcOptionsTest'); |
| 246 | + debounceMock.mockReturnValueOnce('debounceTest'); |
| 247 | + |
| 248 | + await callRunCompiler({ |
| 249 | + extras: { |
| 250 | + watch: true, |
| 251 | + }, |
| 252 | + }); |
| 253 | + |
| 254 | + expect(compiler['watchFilesInOutDir']).toHaveBeenCalledWith( |
| 255 | + 'swcOptionsTest', |
| 256 | + 'debounceTest', |
| 257 | + ); |
| 258 | + }); |
| 259 | + |
| 260 | + it('should not call closeWatchers method when extras.watch is true', async () => { |
| 261 | + const closeWatchersMock = jest.fn(); |
| 262 | + const fixture = { |
| 263 | + extras: { |
| 264 | + watch: true, |
| 265 | + assetsManager: { |
| 266 | + closeWatchers: closeWatchersMock, |
| 267 | + }, |
| 268 | + }, |
| 269 | + }; |
| 270 | + |
| 271 | + await callRunCompiler({ |
| 272 | + extras: fixture.extras, |
| 273 | + }); |
| 274 | + |
| 275 | + await callRunCompiler({ |
| 276 | + extras: fixture.extras, |
| 277 | + onSuccess: false, |
| 278 | + }); |
| 279 | + |
| 280 | + expect(closeWatchersMock).not.toHaveBeenCalled(); |
| 281 | + }); |
| 282 | + |
| 283 | + it('should call closeWatchers method when extras.watch is false', async () => { |
| 284 | + const closeWatchersMock = jest.fn(); |
| 285 | + const fixture = { |
| 286 | + extras: { |
| 287 | + watch: false, |
| 288 | + assetsManager: { |
| 289 | + closeWatchers: closeWatchersMock, |
| 290 | + }, |
| 291 | + }, |
| 292 | + }; |
| 293 | + |
| 294 | + await callRunCompiler({ |
| 295 | + extras: fixture.extras, |
| 296 | + onSuccess: false, |
| 297 | + }); |
| 298 | + |
| 299 | + await callRunCompiler({ |
| 300 | + extras: fixture.extras, |
| 301 | + }); |
| 302 | + |
| 303 | + expect(closeWatchersMock).toHaveBeenCalledTimes(2); |
| 304 | + }); |
| 305 | + }); |
| 306 | +}); |
0 commit comments