|
1 | 1 | "use strict";
|
2 | 2 |
|
3 |
| -/* globals describe it */ |
4 |
| -require("should"); |
| 3 | +/* globals describe expect it fit */ |
5 | 4 | const path = require("path");
|
6 | 5 | const fs = require("fs");
|
7 | 6 | const vm = require("vm");
|
8 | 7 | const mkdirp = require("mkdirp");
|
9 |
| -const Test = require("mocha/lib/test"); |
10 | 8 | const checkArrayExpectation = require("./checkArrayExpectation");
|
| 9 | +const async = require("async"); |
11 | 10 |
|
12 | 11 | const Stats = require("../lib/Stats");
|
13 | 12 | const webpack = require("../lib/webpack");
|
@@ -36,113 +35,114 @@ describe("ConfigTestCases", () => {
|
36 | 35 | categories.forEach((category) => {
|
37 | 36 | describe(category.name, () => {
|
38 | 37 | category.tests.forEach((testName) => {
|
39 |
| - const suite = describe(testName, () => {}); |
40 |
| - it(testName + " should compile", function(done) { |
41 |
| - const testDirectory = path.join(casesPath, category.name, testName); |
42 |
| - const outputDirectory = path.join(__dirname, "js", "config", category.name, testName); |
43 |
| - const options = prepareOptions(require(path.join(testDirectory, "webpack.config.js"))); |
44 |
| - const optionsArr = [].concat(options); |
45 |
| - optionsArr.forEach((options, idx) => { |
46 |
| - if(!options.context) options.context = testDirectory; |
47 |
| - if(!options.mode) options.mode = "production"; |
48 |
| - if(!options.optimization) options.optimization = {}; |
49 |
| - if(options.optimization.minimize === undefined) options.optimization.minimize = false; |
50 |
| - if(!options.entry) options.entry = "./index.js"; |
51 |
| - if(!options.target) options.target = "async-node"; |
52 |
| - if(!options.output) options.output = {}; |
53 |
| - if(!options.output.path) options.output.path = outputDirectory; |
54 |
| - if(typeof options.output.pathinfo === "undefined") options.output.pathinfo = true; |
55 |
| - if(!options.output.filename) options.output.filename = "bundle" + idx + ".js"; |
56 |
| - }); |
57 |
| - let testConfig = { |
58 |
| - findBundle: function(i, options) { |
59 |
| - if(fs.existsSync(path.join(options.output.path, "bundle" + i + ".js"))) { |
60 |
| - return "./bundle" + i + ".js"; |
| 38 | + describe(testName, () => { |
| 39 | + it(testName + " should compile", (done) => { |
| 40 | + const testDirectory = path.join(casesPath, category.name, testName); |
| 41 | + const outputDirectory = path.join(__dirname, "js", "config", category.name, testName); |
| 42 | + const options = prepareOptions(require(path.join(testDirectory, "webpack.config.js"))); |
| 43 | + const optionsArr = [].concat(options); |
| 44 | + optionsArr.forEach((options, idx) => { |
| 45 | + if(!options.context) options.context = testDirectory; |
| 46 | + if(!options.mode) options.mode = "production"; |
| 47 | + if(!options.optimization) options.optimization = {}; |
| 48 | + if(options.optimization.minimize === undefined) options.optimization.minimize = false; |
| 49 | + if(!options.entry) options.entry = "./index.js"; |
| 50 | + if(!options.target) options.target = "async-node"; |
| 51 | + if(!options.output) options.output = {}; |
| 52 | + if(!options.output.path) options.output.path = outputDirectory; |
| 53 | + if(typeof options.output.pathinfo === "undefined") options.output.pathinfo = true; |
| 54 | + if(!options.output.filename) options.output.filename = "bundle" + idx + ".js"; |
| 55 | + }); |
| 56 | + let testConfig = { |
| 57 | + findBundle: function(i, options) { |
| 58 | + if(fs.existsSync(path.join(options.output.path, "bundle" + i + ".js"))) { |
| 59 | + return "./bundle" + i + ".js"; |
| 60 | + } |
| 61 | + }, |
| 62 | + timeout: 30000 |
| 63 | + }; |
| 64 | + try { |
| 65 | + // try to load a test file |
| 66 | + testConfig = Object.assign(testConfig, require(path.join(testDirectory, "test.config.js"))); |
| 67 | + } catch(e) {} |
| 68 | + |
| 69 | + // this.timeout(testConfig.timeout); |
| 70 | + |
| 71 | + webpack(options, (err, stats) => { |
| 72 | + if(err) { |
| 73 | + const fakeStats = { |
| 74 | + errors: [err.stack] |
| 75 | + }; |
| 76 | + if(checkArrayExpectation(testDirectory, fakeStats, "error", "Error", done)) return; |
| 77 | + // Wait for uncatched errors to occur |
| 78 | + return setTimeout(done, 200); |
61 | 79 | }
|
62 |
| - }, |
63 |
| - timeout: 30000 |
64 |
| - }; |
65 |
| - try { |
66 |
| - // try to load a test file |
67 |
| - testConfig = Object.assign(testConfig, require(path.join(testDirectory, "test.config.js"))); |
68 |
| - } catch(e) {} |
| 80 | + const statOptions = Stats.presetToOptions("verbose"); |
| 81 | + statOptions.colors = false; |
| 82 | + mkdirp.sync(outputDirectory); |
| 83 | + fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8"); |
| 84 | + const jsonStats = stats.toJson({ |
| 85 | + errorDetails: true |
| 86 | + }); |
| 87 | + if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return; |
| 88 | + if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return; |
| 89 | + let exportedTests = []; |
69 | 90 |
|
70 |
| - this.timeout(testConfig.timeout); |
| 91 | + function _it(title, fn) { |
| 92 | + exportedTests.push(fit(title, fn)); |
| 93 | + } |
71 | 94 |
|
72 |
| - webpack(options, (err, stats) => { |
73 |
| - if(err) { |
74 |
| - const fakeStats = { |
75 |
| - errors: [err.stack] |
| 95 | + const globalContext = { |
| 96 | + console: console |
76 | 97 | };
|
77 |
| - if(checkArrayExpectation(testDirectory, fakeStats, "error", "Error", done)) return; |
78 |
| - // Wait for uncatched errors to occur |
79 |
| - return setTimeout(done, 200); |
80 |
| - } |
81 |
| - const statOptions = Stats.presetToOptions("verbose"); |
82 |
| - statOptions.colors = false; |
83 |
| - mkdirp.sync(outputDirectory); |
84 |
| - fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8"); |
85 |
| - const jsonStats = stats.toJson({ |
86 |
| - errorDetails: true |
87 |
| - }); |
88 |
| - if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return; |
89 |
| - if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return; |
90 |
| - let exportedTests = 0; |
91 |
| - |
92 |
| - function _it(title, fn) { |
93 |
| - const test = new Test(title, fn); |
94 |
| - suite.addTest(test); |
95 |
| - exportedTests++; |
96 |
| - return test; |
97 |
| - } |
98 | 98 |
|
99 |
| - const globalContext = { |
100 |
| - console: console |
101 |
| - }; |
| 99 | + function _require(currentDirectory, module) { |
| 100 | + if(Array.isArray(module) || /^\.\.?\//.test(module)) { |
| 101 | + let fn; |
| 102 | + let content; |
| 103 | + let p; |
| 104 | + if(Array.isArray(module)) { |
| 105 | + p = path.join(currentDirectory, module[0]); |
| 106 | + content = module.map((arg) => { |
| 107 | + p = path.join(currentDirectory, arg); |
| 108 | + return fs.readFileSync(p, "utf-8"); |
| 109 | + }).join("\n"); |
| 110 | + } else { |
| 111 | + p = path.join(currentDirectory, module); |
| 112 | + content = fs.readFileSync(p, "utf-8"); |
| 113 | + } |
| 114 | + if(options.target === "web" || options.target === "webworker") { |
| 115 | + fn = vm.runInNewContext("(function(require, module, exports, __dirname, __filename, it, expect, window) {" + content + "\n})", globalContext, p); |
| 116 | + } else { |
| 117 | + fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, expect) {" + content + "\n})", p); |
| 118 | + } |
| 119 | + const m = { |
| 120 | + exports: {} |
| 121 | + }; |
| 122 | + fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, expect, globalContext); |
| 123 | + return m.exports; |
| 124 | + } else if(testConfig.modules && module in testConfig.modules) { |
| 125 | + return testConfig.modules[module]; |
| 126 | + } else return require(module); |
| 127 | + } |
| 128 | + let filesCount = 0; |
102 | 129 |
|
103 |
| - function _require(currentDirectory, module) { |
104 |
| - if(Array.isArray(module) || /^\.\.?\//.test(module)) { |
105 |
| - let fn; |
106 |
| - let content; |
107 |
| - let p; |
108 |
| - if(Array.isArray(module)) { |
109 |
| - p = path.join(currentDirectory, module[0]); |
110 |
| - content = module.map((arg) => { |
111 |
| - p = path.join(currentDirectory, arg); |
112 |
| - return fs.readFileSync(p, "utf-8"); |
113 |
| - }).join("\n"); |
114 |
| - } else { |
115 |
| - p = path.join(currentDirectory, module); |
116 |
| - content = fs.readFileSync(p, "utf-8"); |
| 130 | + if(testConfig.noTests) return process.nextTick(done); |
| 131 | + for(let i = 0; i < optionsArr.length; i++) { |
| 132 | + const bundlePath = testConfig.findBundle(i, optionsArr[i]); |
| 133 | + if(bundlePath) { |
| 134 | + filesCount++; |
| 135 | + _require(outputDirectory, bundlePath); |
117 | 136 | }
|
118 |
| - if(options.target === "web" || options.target === "webworker") { |
119 |
| - fn = vm.runInNewContext("(function(require, module, exports, __dirname, __filename, it, window) {" + content + "\n})", globalContext, p); |
120 |
| - } else { |
121 |
| - fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it) {" + content + "\n})", p); |
122 |
| - } |
123 |
| - const m = { |
124 |
| - exports: {} |
125 |
| - }; |
126 |
| - fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, globalContext); |
127 |
| - return m.exports; |
128 |
| - } else if(testConfig.modules && module in testConfig.modules) { |
129 |
| - return testConfig.modules[module]; |
130 |
| - } else return require(module); |
131 |
| - } |
132 |
| - let filesCount = 0; |
133 |
| - |
134 |
| - if(testConfig.noTests) return process.nextTick(done); |
135 |
| - for(let i = 0; i < optionsArr.length; i++) { |
136 |
| - const bundlePath = testConfig.findBundle(i, optionsArr[i]); |
137 |
| - if(bundlePath) { |
138 |
| - filesCount++; |
139 |
| - _require(outputDirectory, bundlePath); |
140 | 137 | }
|
141 |
| - } |
142 |
| - // give a free pass to compilation that generated an error |
143 |
| - if(!jsonStats.errors.length && filesCount !== optionsArr.length) return done(new Error("Should have found at least one bundle file per webpack config")); |
144 |
| - if(exportedTests < filesCount) return done(new Error("No tests exported by test case")); |
145 |
| - process.nextTick(done); |
| 138 | + // give a free pass to compilation that generated an error |
| 139 | + if(!jsonStats.errors.length && filesCount !== optionsArr.length) return done(new Error("Should have found at least one bundle file per webpack config")); |
| 140 | + if(exportedTests.length < filesCount) return done(new Error("No tests exported by test case")); |
| 141 | + async.waterfall( |
| 142 | + exportedTests.map(test => (callback) => test.execute(callback, true)), |
| 143 | + done |
| 144 | + ); |
| 145 | + }); |
146 | 146 | });
|
147 | 147 | });
|
148 | 148 | });
|
|
0 commit comments