Skip to content

Commit dad0b6f

Browse files
committed
refactor(ES6): TestCases.test.js
1 parent 860b053 commit dad0b6f

1 file changed

Lines changed: 41 additions & 45 deletions

File tree

test/TestCases.test.js

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
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");
1+
"use strict";
72

8-
var Stats = require("../lib/Stats");
9-
var webpack = require("../lib/webpack");
3+
const should = require("should");
4+
const path = require("path");
5+
const fs = require("fs");
6+
const vm = require("vm");
7+
const Test = require("mocha/lib/test");
8+
const checkArrayExpectation = require("./checkArrayExpectation");
109

11-
describe("TestCases", function() {
12-
var casesPath = path.join(__dirname, "cases");
13-
var categories = fs.readdirSync(casesPath);
14-
categories = categories.map(function(cat) {
10+
const Stats = require("../lib/Stats");
11+
const webpack = require("../lib/webpack");
12+
13+
describe("TestCases", () => {
14+
const casesPath = path.join(__dirname, "cases");
15+
let categories = fs.readdirSync(casesPath);
16+
categories = categories.map((cat) => {
1517
return {
1618
name: cat,
17-
tests: fs.readdirSync(path.join(casesPath, cat)).filter(function(folder) {
18-
return folder.indexOf("_") < 0;
19-
})
19+
tests: fs.readdirSync(path.join(casesPath, cat)).filter((folder) => folder.indexOf("_") < 0)
2020
};
2121
});
2222
[{
@@ -94,27 +94,25 @@ describe("TestCases", function() {
9494
new webpack.optimize.UglifyJsPlugin(),
9595
new webpack.NamedModulesPlugin()
9696
]
97-
}].forEach(function(config) {
98-
describe(config.name, function() {
99-
categories.forEach(function(category) {
97+
}].forEach((config) => {
98+
describe(config.name, () => {
99+
categories.forEach((category) => {
100100
describe(category.name, function() {
101101
this.timeout(30000);
102-
category.tests.filter(function(test) {
103-
var testDirectory = path.join(casesPath, category.name, test);
104-
var filterPath = path.join(testDirectory, "test.filter.js");
102+
category.tests.filter((test) => {
103+
const testDirectory = path.join(casesPath, category.name, test);
104+
const filterPath = path.join(testDirectory, "test.filter.js");
105105
if(fs.existsSync(filterPath) && !require(filterPath)(config)) {
106-
describe.skip(test, function() {
107-
it('filtered');
108-
});
106+
describe.skip(test, () => it("filtered"));
109107
return false;
110108
}
111109
return true;
112-
}).forEach(function(testName) {
113-
var suite = describe(testName, function() {});
114-
it(testName + " should compile", function(done) {
115-
var testDirectory = path.join(casesPath, category.name, testName);
116-
var outputDirectory = path.join(__dirname, "js", config.name, category.name, testName);
117-
var options = {
110+
}).forEach((testName) => {
111+
const suite = describe(testName, () => {});
112+
it(testName + " should compile", (done) => {
113+
const testDirectory = path.join(casesPath, category.name, testName);
114+
const outputDirectory = path.join(__dirname, "js", config.name, category.name, testName);
115+
const options = {
118116
context: casesPath,
119117
entry: "./" + category.name + "/" + testName + "/index",
120118
target: "async-node",
@@ -145,43 +143,41 @@ describe("TestCases", function() {
145143
}]
146144
},
147145
plugins: (config.plugins || []).concat(function() {
148-
this.plugin("compilation", function(compilation) {
149-
["optimize", "optimize-modules-basic", "optimize-chunks-basic", "after-optimize-tree", "after-optimize-assets"].forEach(function(hook) {
150-
compilation.plugin(hook, function() {
151-
compilation.checkConstraints();
152-
});
146+
this.plugin("compilation", (compilation) => {
147+
["optimize", "optimize-modules-basic", "optimize-chunks-basic", "after-optimize-tree", "after-optimize-assets"].forEach((hook) => {
148+
compilation.plugin(hook, () => compilation.checkConstraints());
153149
});
154150
});
155151
})
156152
};
157-
webpack(options, function(err, stats) {
153+
webpack(options, (err, stats) => {
158154
if(err) return done(err);
159-
var statOptions = Stats.presetToOptions("verbose");
155+
const statOptions = Stats.presetToOptions("verbose");
160156
statOptions.colors = false;
161157
fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8");
162-
var jsonStats = stats.toJson({
158+
const jsonStats = stats.toJson({
163159
errorDetails: true
164160
});
165161
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
166162
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
167-
var exportedTest = 0;
163+
let exportedTest = 0;
168164

169165
function _it(title, fn) {
170-
var test = new Test(title, fn);
166+
const test = new Test(title, fn);
171167
suite.addTest(test);
172168
exportedTest++;
173169
return test;
174170
}
175171

176172
function _require(module) {
177173
if(module.substr(0, 2) === "./") {
178-
var p = path.join(outputDirectory, module);
179-
var fn = vm.runInThisContext("(function(require, module, exports, __dirname, it) {" + fs.readFileSync(p, "utf-8") + "\n})", p);
180-
var module = {
174+
const p = path.join(outputDirectory, module);
175+
const fn = vm.runInThisContext("(function(require, module, exports, __dirname, it) {" + fs.readFileSync(p, "utf-8") + "\n})", p);
176+
const m = {
181177
exports: {}
182178
};
183-
fn.call(module.exports, _require, module, module.exports, outputDirectory, _it);
184-
return module.exports;
179+
fn.call(m.exports, _require, m, m.exports, outputDirectory, _it);
180+
return m.exports;
185181
} else return require(module);
186182
}
187183
_require("./bundle.js");

0 commit comments

Comments
 (0)