|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +jest.mock('../config/paths', () => ({ |
| 4 | + appPackageJson: './test/mockPackage.json', |
| 5 | +})); |
| 6 | + |
| 7 | +describe('splitChunks', () => { |
| 8 | + const mockData = { |
| 9 | + name: 'test', |
| 10 | + version: '1.0.0', |
| 11 | + 'backpack-react-scripts': {}, |
| 12 | + }; |
| 13 | + |
| 14 | + let isEnvDevelopment = true; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + jest.resetModules(); |
| 18 | + }); |
| 19 | + |
| 20 | + test('should return default if no config defined', () => { |
| 21 | + jest.doMock('./test/mockPackage.json', () => ({ |
| 22 | + ...mockData, |
| 23 | + 'backpack-react-scripts': {}, |
| 24 | + })); |
| 25 | + const splitChunks = require('../backpack-addons/splitChunks'); |
| 26 | + |
| 27 | + let res = splitChunks(isEnvDevelopment); |
| 28 | + |
| 29 | + expect(res).toEqual({ splitChunks: {} }); |
| 30 | + }); |
| 31 | + |
| 32 | + test('should apply basic defaults if automatic chunking enabled without vendors regex', () => { |
| 33 | + jest.doMock('./test/mockPackage.json', () => ({ |
| 34 | + ...mockData, |
| 35 | + 'backpack-react-scripts': { |
| 36 | + enableAutomaticChunking: true, |
| 37 | + }, |
| 38 | + })); |
| 39 | + const splitChunks = require('../backpack-addons/splitChunks'); |
| 40 | + |
| 41 | + let res = splitChunks(isEnvDevelopment); |
| 42 | + |
| 43 | + expect(res).toEqual({ |
| 44 | + splitChunks: { chunks: 'all', name: true, cacheGroups: {} }, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + test('should return empty if automatic chunking false and no other config is defined', () => { |
| 49 | + jest.doMock('./test/mockPackage.json', () => ({ |
| 50 | + ...mockData, |
| 51 | + 'backpack-react-scripts': { |
| 52 | + enableAutomaticChunking: false, |
| 53 | + }, |
| 54 | + })); |
| 55 | + const splitChunks = require('../backpack-addons/splitChunks'); |
| 56 | + |
| 57 | + let res = splitChunks(isEnvDevelopment); |
| 58 | + |
| 59 | + expect(res).toEqual({ splitChunks: {} }); |
| 60 | + }); |
| 61 | + |
| 62 | + test('should apply basic defaults and cacheGroup with vendors RegExp when automatic chunking enabled and vendors regex provided', () => { |
| 63 | + jest.doMock('./test/mockPackage.json', () => ({ |
| 64 | + ...mockData, |
| 65 | + 'backpack-react-scripts': { |
| 66 | + enableAutomaticChunking: true, |
| 67 | + vendorsChunkRegex: '[\\/]node_modules[\\/]', |
| 68 | + }, |
| 69 | + })); |
| 70 | + const splitChunks = require('../backpack-addons/splitChunks'); |
| 71 | + |
| 72 | + let res = splitChunks(isEnvDevelopment); |
| 73 | + |
| 74 | + expect(res).toEqual({ |
| 75 | + splitChunks: { |
| 76 | + chunks: 'all', |
| 77 | + name: true, |
| 78 | + cacheGroups: { vendors: { test: expect.any(RegExp) } }, |
| 79 | + }, |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + test('should return empty when automatic chunking disabled and vendors regex provided', () => { |
| 84 | + jest.doMock('./test/mockPackage.json', () => ({ |
| 85 | + ...mockData, |
| 86 | + 'backpack-react-scripts': { |
| 87 | + enableAutomaticChunking: false, |
| 88 | + vendorsChunkRegex: '[\\/]node_modules[\\/]', |
| 89 | + }, |
| 90 | + })); |
| 91 | + const splitChunks = require('../backpack-addons/splitChunks'); |
| 92 | + |
| 93 | + let res = splitChunks(isEnvDevelopment); |
| 94 | + |
| 95 | + expect(res).toEqual({ splitChunks: {} }); |
| 96 | + }); |
| 97 | + |
| 98 | + test('should ignore custom config when automatic chunking enabled and splitChunksConfig is also defined', () => { |
| 99 | + jest.doMock('./test/mockPackage.json', () => ({ |
| 100 | + ...mockData, |
| 101 | + 'backpack-react-scripts': { |
| 102 | + enableAutomaticChunking: true, |
| 103 | + splitChunksConfig: { |
| 104 | + cacheGroups: { |
| 105 | + vendors: { |
| 106 | + test: '[\\/]node_modules[\\/]', |
| 107 | + }, |
| 108 | + someCustomChunk: { |
| 109 | + test: '[\\/]some_regex[\\/]', |
| 110 | + priority: 100, |
| 111 | + chunks: 'all', |
| 112 | + name: 'someCustomChunk', |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + })); |
| 118 | + const splitChunks = require('../backpack-addons/splitChunks'); |
| 119 | + |
| 120 | + let res = splitChunks(isEnvDevelopment); |
| 121 | + |
| 122 | + expect(res).toEqual({ |
| 123 | + splitChunks: { chunks: 'all', name: true, cacheGroups: {} }, |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + test('should not ignore custom config when automatic chunking disabled and splitChunksConfig is defined', () => { |
| 128 | + jest.doMock('./test/mockPackage.json', () => ({ |
| 129 | + ...mockData, |
| 130 | + 'backpack-react-scripts': { |
| 131 | + enableAutomaticChunking: false, |
| 132 | + splitChunksConfig: { |
| 133 | + chunks: 'all', |
| 134 | + cacheGroups: { |
| 135 | + vendors: { |
| 136 | + test: '[\\/]node_modules[\\/]', |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + })); |
| 142 | + const splitChunks = require('../backpack-addons/splitChunks'); |
| 143 | + |
| 144 | + let res = splitChunks(isEnvDevelopment); |
| 145 | + |
| 146 | + expect(res).toEqual({ |
| 147 | + splitChunks: { |
| 148 | + chunks: 'all', |
| 149 | + name: true, |
| 150 | + cacheGroups: { |
| 151 | + vendors: { |
| 152 | + test: expect.any(RegExp), |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + test('should apply only the name field when splitChunks is empty', () => { |
| 160 | + jest.doMock('./test/mockPackage.json', () => ({ |
| 161 | + ...mockData, |
| 162 | + 'backpack-react-scripts': { |
| 163 | + enableAutomaticChunking: false, |
| 164 | + splitChunksConfig: {}, |
| 165 | + }, |
| 166 | + })); |
| 167 | + const splitChunks = require('../backpack-addons/splitChunks'); |
| 168 | + |
| 169 | + let res = splitChunks(isEnvDevelopment); |
| 170 | + |
| 171 | + expect(res).toEqual({ splitChunks: { name: true } }); |
| 172 | + }); |
| 173 | + |
| 174 | + test('should apply Regexes when multiple cacheGroups are applied', () => { |
| 175 | + jest.doMock('./test/mockPackage.json', () => ({ |
| 176 | + ...mockData, |
| 177 | + 'backpack-react-scripts': { |
| 178 | + enableAutomaticChunking: false, |
| 179 | + splitChunksConfig: { |
| 180 | + chunks: 'all', |
| 181 | + cacheGroups: { |
| 182 | + vendors: { |
| 183 | + test: '[\\/]node_modules[\\/]', |
| 184 | + }, |
| 185 | + someCustomChunk: { |
| 186 | + test: '[\\/]some_regex[\\/]', |
| 187 | + priority: 100, |
| 188 | + chunks: 'all', |
| 189 | + name: 'someCustomChunk', |
| 190 | + }, |
| 191 | + }, |
| 192 | + }, |
| 193 | + }, |
| 194 | + })); |
| 195 | + const splitChunks = require('../backpack-addons/splitChunks'); |
| 196 | + |
| 197 | + let res = splitChunks(isEnvDevelopment); |
| 198 | + |
| 199 | + expect(res).toEqual({ |
| 200 | + splitChunks: { |
| 201 | + chunks: 'all', |
| 202 | + name: true, |
| 203 | + cacheGroups: { |
| 204 | + vendors: { |
| 205 | + test: expect.any(RegExp), |
| 206 | + }, |
| 207 | + someCustomChunk: { |
| 208 | + test: expect.any(RegExp), |
| 209 | + priority: 100, |
| 210 | + chunks: 'all', |
| 211 | + name: 'someCustomChunk', |
| 212 | + }, |
| 213 | + }, |
| 214 | + }, |
| 215 | + }); |
| 216 | + }); |
| 217 | + |
| 218 | + test('should apply isEnvDevelopment boolean as name value', () => { |
| 219 | + let isEnvDevelopment = false; |
| 220 | + jest.doMock('./test/mockPackage.json', () => ({ |
| 221 | + ...mockData, |
| 222 | + 'backpack-react-scripts': { |
| 223 | + enableAutomaticChunking: true, |
| 224 | + }, |
| 225 | + })); |
| 226 | + const splitChunks = require('../backpack-addons/splitChunks'); |
| 227 | + |
| 228 | + let res = splitChunks(isEnvDevelopment); |
| 229 | + |
| 230 | + expect(res).toEqual({ |
| 231 | + splitChunks: { chunks: 'all', name: false, cacheGroups: {} }, |
| 232 | + }); |
| 233 | + }); |
| 234 | +}); |
0 commit comments