| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-27 04:47:47 +08:00
										 |  |  | /*globals describe it */ | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | const path = require("path"); | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | const webpack = require("../lib/webpack"); | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | const base = path.join(__dirname, "fixtures", "errors"); | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | describe("Errors", () => { | 
					
						
							| 
									
										
										
										
											2015-04-27 04:47:47 +08:00
										 |  |  | 	function customOutputFilesystem(c) { | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 		const files = {}; | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 		c.outputFileSystem = { | 
					
						
							|  |  |  | 			join: path.join.bind(path), | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			mkdirp(path, callback) { | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 				callback(); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			writeFile(name, content, callback) { | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 				files[name] = content.toString("utf-8"); | 
					
						
							|  |  |  | 				callback(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2015-04-27 04:47:47 +08:00
										 |  |  | 		return files; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-27 04:47:47 +08:00
										 |  |  | 	function getErrors(options, callback) { | 
					
						
							|  |  |  | 		options.context = base; | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 		const c = webpack(options); | 
					
						
							| 
									
										
										
										
											2015-04-27 04:47:47 +08:00
										 |  |  | 		customOutputFilesystem(c); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 		c.run((err, stats) => { | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 			if(err) throw err; | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(typeof stats).toBe("object"); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			stats = stats.toJson({ | 
					
						
							|  |  |  | 				errorDetails: false | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(typeof stats).toBe("object"); | 
					
						
							|  |  |  | 			expect(stats).toHaveProperty("errors"); | 
					
						
							|  |  |  | 			expect(stats).toHaveProperty("warnings"); | 
					
						
							|  |  |  | 			expect(Array.isArray(stats.errors)).toBe(true); | 
					
						
							|  |  |  | 			expect(Array.isArray(stats.warnings)).toBe(true); | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 			callback(stats.errors, stats.warnings); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 	it("should throw an error if file doesn't exist", (done) => { | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 		getErrors({ | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 			mode: "development", | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 			entry: "./missingFile" | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 		}, (errors, warnings) => { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(errors).toHaveLength(2); | 
					
						
							|  |  |  | 			expect(warnings).toHaveLength(0); | 
					
						
							| 
									
										
										
										
											2014-05-26 17:36:59 +08:00
										 |  |  | 			errors.sort(); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 			let lines = errors[0].split("\n"); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(lines[0]).toMatch(/missingFile.js/); | 
					
						
							|  |  |  | 			expect(lines[1]).toMatch(/^Module not found/); | 
					
						
							|  |  |  | 			expect(lines[1]).toMatch(/\.\/dir\/missing2/); | 
					
						
							|  |  |  | 			expect(lines[2]).toMatch(/missingFile.js 12:9/); | 
					
						
							| 
									
										
										
										
											2015-04-27 04:47:47 +08:00
										 |  |  | 			lines = errors[1].split("\n"); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(lines[0]).toMatch(/missingFile.js/); | 
					
						
							|  |  |  | 			expect(lines[1]).toMatch(/^Module not found/); | 
					
						
							|  |  |  | 			expect(lines[1]).toMatch(/\.\/missing/); | 
					
						
							|  |  |  | 			expect(lines[2]).toMatch(/missingFile.js 4:0/); | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 	it("should report require.extensions as unsupported", (done) => { | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 		getErrors({ | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 			mode: "development", | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 			entry: "./require.extensions" | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 		}, (errors, warnings) => { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(errors).toHaveLength(0); | 
					
						
							|  |  |  | 			expect(warnings).toHaveLength(1); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 			const lines = warnings[0].split("\n"); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(lines[0]).toMatch(/require.extensions\.js/); | 
					
						
							|  |  |  | 			expect(lines[1]).toMatch(/require.extensions is not supported by webpack/); | 
					
						
							| 
									
										
										
										
											2013-10-11 16:42:25 +08:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 	it("should warn about case-sensitive module names", (done) => { | 
					
						
							| 
									
										
										
										
											2014-03-31 17:33:17 +08:00
										 |  |  | 		getErrors({ | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 			mode: "development", | 
					
						
							| 
									
										
										
										
											2014-03-31 17:33:17 +08:00
										 |  |  | 			entry: "./case-sensitive" | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 		}, (errors, warnings) => { | 
					
						
							| 
									
										
										
										
											2014-03-31 17:39:01 +08:00
										 |  |  | 			if(errors.length === 0) { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 				expect(warnings).toHaveLength(1); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 				const lines = warnings[0].split("\n"); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 				expect(lines[4]).toMatch(/FILE\.js/); | 
					
						
							|  |  |  | 				expect(lines[5]).toMatch(/Used by/); | 
					
						
							|  |  |  | 				expect(lines[6]).toMatch(/case-sensitive/); | 
					
						
							|  |  |  | 				expect(lines[7]).toMatch(/file\.js/); | 
					
						
							|  |  |  | 				expect(lines[8]).toMatch(/Used by/); | 
					
						
							|  |  |  | 				expect(lines[9]).toMatch(/case-sensitive/); | 
					
						
							| 
									
										
										
										
											2014-03-31 17:39:01 +08:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 				expect(errors).toHaveLength(1); | 
					
						
							|  |  |  | 				expect(warnings).toHaveLength(0); | 
					
						
							| 
									
										
										
										
											2014-03-31 17:39:01 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-03-31 17:33:17 +08:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 	it("should warn when not using mode", (done) => { | 
					
						
							|  |  |  | 		getErrors({ | 
					
						
							|  |  |  | 			entry: "./entry-point", | 
					
						
							|  |  |  | 		}, (errors, warnings) => { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(errors).toHaveLength(0); | 
					
						
							|  |  |  | 			expect(warnings).toHaveLength(1); | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 			let lines = warnings[0].split("\n"); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(lines[0]).toMatch(/configuration/); | 
					
						
							|  |  |  | 			expect(lines[1]).toMatch(/mode/); | 
					
						
							|  |  |  | 			expect(lines[1]).toMatch(/development/); | 
					
						
							|  |  |  | 			expect(lines[1]).toMatch(/production/); | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 	it("should not warn if the NoEmitOnErrorsPlugin is used over the NoErrorsPlugin", (done) => { | 
					
						
							| 
									
										
										
										
											2016-12-30 16:52:37 +08:00
										 |  |  | 		getErrors({ | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 			mode: "production", | 
					
						
							|  |  |  | 			entry: "./no-errors-deprecate" | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 		}, (errors, warnings) => { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(errors).toHaveLength(0); | 
					
						
							|  |  |  | 			expect(warnings).toHaveLength(0); | 
					
						
							| 
									
										
										
										
											2016-12-30 16:52:37 +08:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 	it("should not not emit if NoEmitOnErrorsPlugin is used and there is an error", (done) => { | 
					
						
							| 
									
										
										
										
											2016-12-30 16:52:37 +08:00
										 |  |  | 		getErrors({ | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 			mode: "production", | 
					
						
							|  |  |  | 			entry: "./missingFile" | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 		}, (errors, warnings) => { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(errors).toHaveLength(2); | 
					
						
							|  |  |  | 			expect(warnings).toHaveLength(0); | 
					
						
							| 
									
										
										
										
											2016-12-30 16:52:37 +08:00
										 |  |  | 			errors.sort(); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 			let lines = errors[0].split("\n"); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(lines[0]).toMatch(/missingFile.js/); | 
					
						
							|  |  |  | 			expect(lines[1]).toMatch(/^Module not found/); | 
					
						
							|  |  |  | 			expect(lines[1]).toMatch(/\.\/dir\/missing2/); | 
					
						
							|  |  |  | 			expect(lines[2]).toMatch(/missingFile.js 12:9/); | 
					
						
							| 
									
										
										
										
											2016-12-30 16:52:37 +08:00
										 |  |  | 			lines = errors[1].split("\n"); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(lines[0]).toMatch(/missingFile.js/); | 
					
						
							|  |  |  | 			expect(lines[1]).toMatch(/^Module not found/); | 
					
						
							|  |  |  | 			expect(lines[1]).toMatch(/\.\/missing/); | 
					
						
							|  |  |  | 			expect(lines[2]).toMatch(/missingFile.js 4:0/); | 
					
						
							| 
									
										
										
										
											2016-12-30 16:52:37 +08:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 	it("should throw an error when trying to use [chunkhash] when it's invalid", (done) => { | 
					
						
							| 
									
										
										
										
											2015-06-27 17:34:17 +08:00
										 |  |  | 		getErrors({ | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 			mode: "development", | 
					
						
							| 
									
										
										
										
											2015-06-27 17:34:17 +08:00
										 |  |  | 			entry: { | 
					
						
							|  |  |  | 				a: "./entry-point", | 
					
						
							|  |  |  | 				b: "./entry-point", | 
					
						
							|  |  |  | 				c: "./entry-point" | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			output: { | 
					
						
							|  |  |  | 				filename: "[chunkhash].js" | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			plugins: [ | 
					
						
							|  |  |  | 				new webpack.HotModuleReplacementPlugin() | 
					
						
							|  |  |  | 			] | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 		}, (errors, warnings) => { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 			expect(errors).toHaveLength(3); | 
					
						
							|  |  |  | 			expect(warnings).toHaveLength(0); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:29:38 +08:00
										 |  |  | 			errors.forEach((error) => { | 
					
						
							|  |  |  | 				const lines = error.split("\n"); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 				expect(lines[0]).toMatch(/chunk (a|b|c)/); | 
					
						
							|  |  |  | 				expect(lines[2]).toMatch(/\[chunkhash\].js/); | 
					
						
							|  |  |  | 				expect(lines[2]).toMatch(/use \[hash\] instead/); | 
					
						
							| 
									
										
										
										
											2015-06-27 17:34:17 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); |