|
| 1 | +var should = require("should"); |
| 2 | +var path = require("path"); |
| 3 | +var fs = require("fs"); |
| 4 | +var vm = require("vm"); |
| 5 | +var Test = require("mocha/lib/test"); |
| 6 | +var checkArrayExpectation = require("./checkArrayExpectation"); |
| 7 | + |
| 8 | +var webpack = require("../lib/webpack"); |
| 9 | + |
| 10 | +describe("HotTestCases", function() { |
| 11 | + var casesPath = path.join(__dirname, "hotCases"); |
| 12 | + var categories = fs.readdirSync(casesPath).filter(function(dir) { |
| 13 | + return fs.statSync(path.join(casesPath, dir)).isDirectory(); |
| 14 | + }); |
| 15 | + categories = categories.map(function(cat) { |
| 16 | + return { |
| 17 | + name: cat, |
| 18 | + tests: fs.readdirSync(path.join(casesPath, cat)).filter(function(folder) { |
| 19 | + return folder.indexOf("_") < 0; |
| 20 | + }) |
| 21 | + }; |
| 22 | + }); |
| 23 | + categories.forEach(function(category) { |
| 24 | + describe(category.name, function() { |
| 25 | + category.tests.forEach(function(testName) { |
| 26 | + var suite = describe(testName, function() {}); |
| 27 | + it(testName + " should compile", function(done) { |
| 28 | + this.timeout(10000); |
| 29 | + var testDirectory = path.join(casesPath, category.name, testName); |
| 30 | + var outputDirectory = path.join(__dirname, "js", "hot-cases", category.name, testName); |
| 31 | + var recordsPath = path.join(outputDirectory, "records.json"); |
| 32 | + if(fs.exists(recordsPath)) |
| 33 | + fs.unlinkSync(recordsPath); |
| 34 | + var options = { |
| 35 | + context: testDirectory, |
| 36 | + entry: "./index.js", |
| 37 | + output: { |
| 38 | + path: outputDirectory, |
| 39 | + filename: "bundle.js" |
| 40 | + }, |
| 41 | + module: { |
| 42 | + loaders: [ |
| 43 | + { |
| 44 | + test: /\.js$/, |
| 45 | + loader: path.join(__dirname, "hotCases", "fake-update-loader.js") |
| 46 | + } |
| 47 | + ] |
| 48 | + }, |
| 49 | + target: "async-node", |
| 50 | + plugins: [ |
| 51 | + new webpack.HotModuleReplacementPlugin() |
| 52 | + ], |
| 53 | + recordsPath: recordsPath, |
| 54 | + updateIndex: 0 |
| 55 | + } |
| 56 | + var compiler = webpack(options); |
| 57 | + compiler.run(function(err, stats) { |
| 58 | + if(err) return done(err); |
| 59 | + var jsonStats = stats.toJson({ |
| 60 | + errorDetails: true |
| 61 | + }); |
| 62 | + if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return; |
| 63 | + if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return; |
| 64 | + var exportedTests = 0; |
| 65 | + |
| 66 | + function _it(title, fn) { |
| 67 | + var test = new Test(title, fn); |
| 68 | + suite.addTest(test); |
| 69 | + exportedTests++; |
| 70 | + return test; |
| 71 | + } |
| 72 | + |
| 73 | + function _next(callback) { |
| 74 | + options.updateIndex++; |
| 75 | + compiler.run(function(err, stats) { |
| 76 | + if(err) return done(err); |
| 77 | + var jsonStats = stats.toJson({ |
| 78 | + errorDetails: true |
| 79 | + }); |
| 80 | + if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return; |
| 81 | + if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return; |
| 82 | + if(callback) callback(); |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + function _require(module) { |
| 87 | + if(module.substr(0, 2) === "./") { |
| 88 | + var p = path.join(outputDirectory, module); |
| 89 | + var fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, NEXT) {" + fs.readFileSync(p, "utf-8") + "\n})", p); |
| 90 | + var module = { |
| 91 | + exports: {} |
| 92 | + }; |
| 93 | + fn.call(module.exports, _require, module, module.exports, outputDirectory, p, _it, _next); |
| 94 | + return module.exports; |
| 95 | + } else return require(module); |
| 96 | + } |
| 97 | + _require("./bundle.js"); |
| 98 | + if(exportedTests < 1) return done(new Error("No tests exported by test case")); |
| 99 | + process.nextTick(done); |
| 100 | + }); |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments