|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { join } = require('path'); |
| 4 | +const ValidationError = require('schema-utils/src/ValidationError'); |
| 5 | +const webpack = require('webpack'); |
| 6 | +const { createFsFromVolume, Volume } = require('memfs'); |
| 7 | +const Server = require('../lib/Server'); |
| 8 | +const options = require('../lib/options.json'); |
| 9 | +const config = require('./fixtures/simple-config/webpack.config'); |
| 10 | + |
| 11 | +describe('options', () => { |
| 12 | + it('should match properties and errorMessage', () => { |
| 13 | + const properties = Object.keys(options.properties); |
| 14 | + const messages = Object.keys(options.errorMessage.properties); |
| 15 | + |
| 16 | + expect(properties.length).toEqual(messages.length); |
| 17 | + |
| 18 | + const res = properties.every((name) => messages.includes(name)); |
| 19 | + |
| 20 | + expect(res).toEqual(true); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('validation', () => { |
| 24 | + let server; |
| 25 | + |
| 26 | + afterAll((done) => { |
| 27 | + if (server) { |
| 28 | + server.close(() => { |
| 29 | + done(); |
| 30 | + }); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + function validateOption(propertyName, cases) { |
| 35 | + const successCount = cases.success.length; |
| 36 | + const testCases = []; |
| 37 | + |
| 38 | + for (const key of Object.keys(cases)) { |
| 39 | + testCases.push(...cases[key]); |
| 40 | + } |
| 41 | + |
| 42 | + let current = 0; |
| 43 | + |
| 44 | + return testCases.reduce((p, value) => { |
| 45 | + let compiler = webpack(config); |
| 46 | + |
| 47 | + return p |
| 48 | + .then(() => { |
| 49 | + const opts = |
| 50 | + Object.prototype.toString.call(value) === '[object Object]' && |
| 51 | + Object.keys(value) !== 0 |
| 52 | + ? value |
| 53 | + : { |
| 54 | + [propertyName]: value, |
| 55 | + }; |
| 56 | + |
| 57 | + server = new Server(compiler, opts); |
| 58 | + }) |
| 59 | + .then(() => { |
| 60 | + if (current <= successCount) { |
| 61 | + expect(true).toBeTruthy(); |
| 62 | + } else { |
| 63 | + expect(false).toBeTruthy(); |
| 64 | + } |
| 65 | + }) |
| 66 | + .catch((err) => { |
| 67 | + if (current >= successCount) { |
| 68 | + expect(err).toBeInstanceOf(ValidationError); |
| 69 | + } else { |
| 70 | + expect(false).toBeTruthy(); |
| 71 | + } |
| 72 | + }) |
| 73 | + .then(() => { |
| 74 | + return new Promise((resolve) => { |
| 75 | + if (server) { |
| 76 | + server.close(() => { |
| 77 | + compiler = null; |
| 78 | + server = null; |
| 79 | + resolve(); |
| 80 | + }); |
| 81 | + } else { |
| 82 | + resolve(); |
| 83 | + } |
| 84 | + }); |
| 85 | + }) |
| 86 | + .then(() => { |
| 87 | + current += 1; |
| 88 | + }); |
| 89 | + }, Promise.resolve()); |
| 90 | + } |
| 91 | + |
| 92 | + const memfs = createFsFromVolume(new Volume()); |
| 93 | + // We need to patch memfs |
| 94 | + // https://github.com/webpack/webpack-dev-middleware#fs |
| 95 | + memfs.join = join; |
| 96 | + |
| 97 | + const cases = { |
| 98 | + after: { |
| 99 | + success: [() => {}], |
| 100 | + failure: [false], |
| 101 | + }, |
| 102 | + before: { |
| 103 | + success: [() => {}], |
| 104 | + failure: [false], |
| 105 | + }, |
| 106 | + allowedHosts: { |
| 107 | + success: [[], ['']], |
| 108 | + failure: [[false], false], |
| 109 | + }, |
| 110 | + bonjour: { |
| 111 | + success: [false], |
| 112 | + failure: [''], |
| 113 | + }, |
| 114 | + ca: { |
| 115 | + success: ['', Buffer.from('')], |
| 116 | + failure: [false], |
| 117 | + }, |
| 118 | + cert: { |
| 119 | + success: ['', Buffer.from('')], |
| 120 | + failure: [false], |
| 121 | + }, |
| 122 | + clientLogLevel: { |
| 123 | + success: ['silent', 'info', 'error', 'warn', 'trace', 'debug'], |
| 124 | + failure: ['whoops!', 'none', 'warning'], |
| 125 | + }, |
| 126 | + compress: { |
| 127 | + success: [true], |
| 128 | + failure: [''], |
| 129 | + }, |
| 130 | + contentBase: { |
| 131 | + success: [0, '.', false], |
| 132 | + failure: [[1], [false]], |
| 133 | + }, |
| 134 | + disableHostCheck: { |
| 135 | + success: [true], |
| 136 | + failure: [''], |
| 137 | + }, |
| 138 | + features: { |
| 139 | + success: [['before'], []], |
| 140 | + failure: [false], |
| 141 | + }, |
| 142 | + filename: { |
| 143 | + success: ['', new RegExp(''), () => {}], |
| 144 | + failure: [false], |
| 145 | + }, |
| 146 | + fs: { |
| 147 | + success: [ |
| 148 | + { |
| 149 | + fs: memfs, |
| 150 | + }, |
| 151 | + ], |
| 152 | + failure: [false], |
| 153 | + }, |
| 154 | + headers: { |
| 155 | + success: [{}], |
| 156 | + failure: [false], |
| 157 | + }, |
| 158 | + historyApiFallback: { |
| 159 | + success: [{}, true], |
| 160 | + failure: [''], |
| 161 | + }, |
| 162 | + host: { |
| 163 | + success: ['', null], |
| 164 | + failure: [false], |
| 165 | + }, |
| 166 | + hot: { |
| 167 | + success: [true], |
| 168 | + failure: [''], |
| 169 | + }, |
| 170 | + hotOnly: { |
| 171 | + success: [true], |
| 172 | + failure: [''], |
| 173 | + }, |
| 174 | + http2: { |
| 175 | + success: [true], |
| 176 | + failure: [''], |
| 177 | + }, |
| 178 | + https: { |
| 179 | + success: [true, {}], |
| 180 | + failure: [''], |
| 181 | + }, |
| 182 | + index: { |
| 183 | + success: [''], |
| 184 | + failure: [false], |
| 185 | + }, |
| 186 | + injectClient: { |
| 187 | + success: [true, () => {}], |
| 188 | + failure: [''], |
| 189 | + }, |
| 190 | + injectHot: { |
| 191 | + success: [true, () => {}], |
| 192 | + failure: [''], |
| 193 | + }, |
| 194 | + inline: { |
| 195 | + success: [true], |
| 196 | + failure: [''], |
| 197 | + }, |
| 198 | + key: { |
| 199 | + success: ['', Buffer.from('')], |
| 200 | + failure: [false], |
| 201 | + }, |
| 202 | + lazy: { |
| 203 | + success: [ |
| 204 | + { |
| 205 | + lazy: true, |
| 206 | + filename: '.', |
| 207 | + }, |
| 208 | + ], |
| 209 | + failure: [ |
| 210 | + { |
| 211 | + lazy: '', |
| 212 | + filename: '.', |
| 213 | + }, |
| 214 | + ], |
| 215 | + }, |
| 216 | + log: { |
| 217 | + success: [() => {}], |
| 218 | + failure: [''], |
| 219 | + }, |
| 220 | + logLevel: { |
| 221 | + success: ['silent', 'info', 'error', 'warn', 'trace', 'debug'], |
| 222 | + failure: [false], |
| 223 | + }, |
| 224 | + logTime: { |
| 225 | + success: [true], |
| 226 | + failure: [''], |
| 227 | + }, |
| 228 | + mimeTypes: { |
| 229 | + success: [{}], |
| 230 | + failure: [false], |
| 231 | + }, |
| 232 | + noInfo: { |
| 233 | + success: [true], |
| 234 | + failure: [''], |
| 235 | + }, |
| 236 | + open: { |
| 237 | + success: [true, ''], |
| 238 | + failure: [{}], |
| 239 | + }, |
| 240 | + openPage: { |
| 241 | + success: [''], |
| 242 | + failure: [false], |
| 243 | + }, |
| 244 | + overlay: { |
| 245 | + success: [ |
| 246 | + true, |
| 247 | + {}, |
| 248 | + { |
| 249 | + overlay: { |
| 250 | + errors: true, |
| 251 | + }, |
| 252 | + }, |
| 253 | + { |
| 254 | + overlay: { |
| 255 | + warnings: true, |
| 256 | + }, |
| 257 | + }, |
| 258 | + { |
| 259 | + overlay: { |
| 260 | + arbitrary: '', |
| 261 | + }, |
| 262 | + }, |
| 263 | + ], |
| 264 | + failure: [ |
| 265 | + '', |
| 266 | + { |
| 267 | + overlay: { |
| 268 | + errors: '', |
| 269 | + }, |
| 270 | + }, |
| 271 | + { |
| 272 | + overlay: { |
| 273 | + warnings: '', |
| 274 | + }, |
| 275 | + }, |
| 276 | + ], |
| 277 | + }, |
| 278 | + pfx: { |
| 279 | + success: ['', Buffer.from('')], |
| 280 | + failure: [false], |
| 281 | + }, |
| 282 | + pfxPassphrase: { |
| 283 | + success: [''], |
| 284 | + failure: [false], |
| 285 | + }, |
| 286 | + port: { |
| 287 | + success: ['', 0, null], |
| 288 | + failure: [false], |
| 289 | + }, |
| 290 | + progress: { |
| 291 | + success: [false], |
| 292 | + failure: [''], |
| 293 | + }, |
| 294 | + proxy: { |
| 295 | + success: [ |
| 296 | + { |
| 297 | + proxy: { |
| 298 | + '/api': 'http://localhost:3000', |
| 299 | + }, |
| 300 | + }, |
| 301 | + ], |
| 302 | + failure: [[], () => {}, false], |
| 303 | + }, |
| 304 | + public: { |
| 305 | + success: [''], |
| 306 | + failure: [false], |
| 307 | + }, |
| 308 | + publicPath: { |
| 309 | + success: [''], |
| 310 | + failure: [false], |
| 311 | + }, |
| 312 | + quiet: { |
| 313 | + success: [true], |
| 314 | + failure: [''], |
| 315 | + }, |
| 316 | + reporter: { |
| 317 | + success: [() => {}], |
| 318 | + failure: [''], |
| 319 | + }, |
| 320 | + requestCert: { |
| 321 | + success: [true], |
| 322 | + failure: [''], |
| 323 | + }, |
| 324 | + serveIndex: { |
| 325 | + success: [true], |
| 326 | + failure: [''], |
| 327 | + }, |
| 328 | + serverSideRender: { |
| 329 | + success: [true], |
| 330 | + failure: [''], |
| 331 | + }, |
| 332 | + setup: { |
| 333 | + success: [() => {}], |
| 334 | + failure: [''], |
| 335 | + }, |
| 336 | + socket: { |
| 337 | + success: [''], |
| 338 | + failure: [false], |
| 339 | + }, |
| 340 | + sockPath: { |
| 341 | + success: [''], |
| 342 | + failure: [false], |
| 343 | + }, |
| 344 | + sockPort: { |
| 345 | + success: ['', 0, null], |
| 346 | + failure: [false], |
| 347 | + }, |
| 348 | + staticOptions: { |
| 349 | + success: [{}], |
| 350 | + failure: [false], |
| 351 | + }, |
| 352 | + stats: { |
| 353 | + success: [ |
| 354 | + true, |
| 355 | + {}, |
| 356 | + 'none', |
| 357 | + 'errors-only', |
| 358 | + 'minimal', |
| 359 | + 'normal', |
| 360 | + 'verbose', |
| 361 | + ], |
| 362 | + failure: [false, 'whoops!', null], |
| 363 | + }, |
| 364 | + useLocalIp: { |
| 365 | + success: [false], |
| 366 | + failure: [''], |
| 367 | + }, |
| 368 | + warn: { |
| 369 | + success: [() => {}], |
| 370 | + failure: [''], |
| 371 | + }, |
| 372 | + watchContentBase: { |
| 373 | + success: [true], |
| 374 | + failure: [''], |
| 375 | + }, |
| 376 | + watchOptions: { |
| 377 | + success: [{}], |
| 378 | + failure: [''], |
| 379 | + }, |
| 380 | + writeToDisk: { |
| 381 | + success: [true, () => {}], |
| 382 | + failure: [''], |
| 383 | + }, |
| 384 | + }; |
| 385 | + |
| 386 | + Object.keys(cases).forEach((key) => { |
| 387 | + it(key, () => { |
| 388 | + return validateOption(key, cases[key]); |
| 389 | + }); |
| 390 | + }); |
| 391 | + }); |
| 392 | +}); |
0 commit comments