Skip to content

Commit c7d5ecf

Browse files
committed
refactor(ES6): WatchTestCases.test.js
1 parent de9323f commit c7d5ecf

1 file changed

Lines changed: 67 additions & 68 deletions

File tree

‎test/WatchTestCases.test.js‎

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
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");
9+
10+
const Stats = require("../lib/Stats");
11+
const webpack = require("../lib/webpack");
1012

1113
function copyDiff(src, dest) {
1214
if(!fs.existsSync(dest))
1315
fs.mkdirSync(dest);
14-
var files = fs.readdirSync(src);
15-
files.forEach(function(filename) {
16-
var srcFile = path.join(src, filename);
17-
var destFile = path.join(dest, filename);
18-
var directory = fs.statSync(srcFile).isDirectory();
16+
const files = fs.readdirSync(src);
17+
files.forEach((filename) => {
18+
const srcFile = path.join(src, filename);
19+
const destFile = path.join(dest, filename);
20+
const directory = fs.statSync(srcFile).isDirectory();
1921
if(directory) {
2022
copyDiff(srcFile, destFile);
2123
} else {
@@ -27,10 +29,10 @@ function copyDiff(src, dest) {
2729
function remove(src) {
2830
if(!fs.existsSync(src))
2931
return;
30-
var files = fs.readdirSync(src);
31-
files.forEach(function(filename) {
32-
var srcFile = path.join(src, filename);
33-
var directory = fs.statSync(srcFile).isDirectory();
32+
const files = fs.readdirSync(src);
33+
files.forEach((filename) => {
34+
const srcFile = path.join(src, filename);
35+
const directory = fs.statSync(srcFile).isDirectory();
3436
if(directory) {
3537
remove(srcFile);
3638
} else {
@@ -39,129 +41,126 @@ function remove(src) {
3941
});
4042
}
4143

42-
describe("WatchTestCases", function() {
43-
var casesPath = path.join(__dirname, "watchCases");
44-
var categories = fs.readdirSync(casesPath);
44+
describe("WatchTestCases", () => {
45+
const casesPath = path.join(__dirname, "watchCases");
46+
let categories = fs.readdirSync(casesPath);
4547

46-
categories = categories.map(function(cat) {
48+
categories = categories.map((cat) => {
4749
return {
4850
name: cat,
49-
tests: fs.readdirSync(path.join(casesPath, cat)).filter(function(folder) {
50-
return folder.indexOf("_") < 0;
51-
}).sort()
51+
tests: fs.readdirSync(path.join(casesPath, cat)).filter((folder) => folder.indexOf("_") < 0).sort()
5252
};
5353
});
54-
before(function() {
55-
var dest = path.join(__dirname, "js");
54+
before(() => {
55+
let dest = path.join(__dirname, "js");
5656
if(!fs.existsSync(dest))
5757
fs.mkdirSync(dest);
5858
dest = path.join(__dirname, "js", "watch-src");
5959
if(!fs.existsSync(dest))
6060
fs.mkdirSync(dest);
6161
});
62-
categories.forEach(function(category) {
63-
before(function() {
64-
var dest = path.join(__dirname, "js", "watch-src", category.name);
62+
categories.forEach((category) => {
63+
before(() => {
64+
const dest = path.join(__dirname, "js", "watch-src", category.name);
6565
if(!fs.existsSync(dest))
6666
fs.mkdirSync(dest);
6767
})
68-
describe(category.name, function() {
69-
category.tests.forEach(function(testName) {
70-
describe(testName, function() {
71-
var tempDirectory = path.join(__dirname, "js", "watch-src", category.name, testName);
72-
var testDirectory = path.join(casesPath, category.name, testName);
73-
var runs = fs.readdirSync(testDirectory).sort().filter(function(name) {
68+
describe(category.name, () => {
69+
category.tests.forEach((testName) => {
70+
describe(testName, () => {
71+
const tempDirectory = path.join(__dirname, "js", "watch-src", category.name, testName);
72+
const testDirectory = path.join(casesPath, category.name, testName);
73+
const runs = fs.readdirSync(testDirectory).sort().filter((name) => {
7474
return fs.statSync(path.join(testDirectory, name)).isDirectory();
75-
}).map(function(name) {
75+
}).map((name) => {
7676
return {
7777
name: name,
78-
suite: describe(name, function() {})
79-
}
80-
});
81-
before(function() {
82-
remove(tempDirectory);
78+
suite: describe(name, () => {})
79+
};
8380
});
81+
before(() => remove(tempDirectory));
8482
it("should compile", function(done) {
8583
this.timeout(30000);
86-
var outputDirectory = path.join(__dirname, "js", "watch", category.name, testName);
84+
const outputDirectory = path.join(__dirname, "js", "watch", category.name, testName);
8785

88-
var options = {};
89-
var configPath = path.join(testDirectory, "webpack.config.js");
86+
let options = {};
87+
const configPath = path.join(testDirectory, "webpack.config.js");
9088
if(fs.existsSync(configPath))
9189
options = require(configPath);
92-
var applyConfig = function(options) {
90+
const applyConfig = (options) => {
9391
if(!options.context) options.context = tempDirectory;
9492
if(!options.entry) options.entry = "./index.js";
9593
if(!options.target) options.target = "async-node";
9694
if(!options.output) options.output = {};
9795
if(!options.output.path) options.output.path = outputDirectory;
9896
if(typeof options.output.pathinfo === "undefined") options.output.pathinfo = true;
9997
if(!options.output.filename) options.output.filename = "bundle.js";
100-
}
98+
};
10199
if(Array.isArray(options)) {
102-
options.forEach(applyConfig)
100+
options.forEach(applyConfig);
103101
} else {
104-
applyConfig(options)
102+
applyConfig(options);
105103
}
106104

107-
var state = {};
108-
var runIdx = 0;
109-
var run = runs[runIdx];
110-
var lastHash = "";
105+
const state = {};
106+
let runIdx = 0;
107+
let run = runs[runIdx];
108+
let lastHash = "";
111109
copyDiff(path.join(testDirectory, run.name), tempDirectory);
112110

113-
var compiler = webpack(options);
114-
var watching = compiler.watch({}, function(err, stats) {
111+
const compiler = webpack(options);
112+
const watching = compiler.watch({}, (err, stats) => {
115113
if(stats.hash === lastHash)
116114
return;
117115
lastHash = stats.hash;
118116
if(run.done)
119117
return done(new Error("Compilation changed but no change was issued " + lastHash + " != " + stats.hash + " (run " + runIdx + ")"));
120118
run.done = true;
121119
if(err) return done(err);
122-
var statOptions = Stats.presetToOptions("verbose");
120+
const statOptions = Stats.presetToOptions("verbose");
123121
statOptions.colors = false;
124122
fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8");
125-
var jsonStats = stats.toJson({
123+
const jsonStats = stats.toJson({
126124
errorDetails: true
127125
});
128126
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
129127
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
130-
var exportedTests = 0;
128+
let exportedTests = 0;
131129

132130
function _it(title, fn) {
133-
var test = new Test(title, fn);
131+
const test = new Test(title, fn);
134132
run.suite.addTest(test);
135133
exportedTests++;
136134
return test;
137135
}
138136

139137
function _require(currentDirectory, module) {
140138
if(Array.isArray(module) || /^\.\.?\//.test(module)) {
141-
var fn;
142-
var content;
139+
let fn;
140+
let content;
141+
let p;
143142
if(Array.isArray(module)) {
144-
var p = path.join(currentDirectory, module[0]);
145-
content = module.map(function(p) {
146-
var p = path.join(currentDirectory, p);
143+
p = path.join(currentDirectory, module[0]);
144+
content = module.map((arg) => {
145+
p = path.join(currentDirectory, arg);
147146
return fs.readFileSync(p, "utf-8");
148147
}).join("\n");
149148
} else {
150-
var p = path.join(currentDirectory, module);
149+
p = path.join(currentDirectory, module);
151150
content = fs.readFileSync(p, "utf-8");
152151
}
153152
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE) {" + content + "\n})", p);
154-
var module = {
153+
const m = {
155154
exports: {}
156155
};
157-
fn.call(module.exports, _require.bind(null, path.dirname(p)), module, module.exports, path.dirname(p), p, _it, run.name, jsonStats, state);
156+
fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, run.name, jsonStats, state);
158157
return module.exports;
159158
} else if(testConfig.modules && module in testConfig.modules) {
160159
return testConfig.modules[module];
161160
} else return require(module);
162161
}
163162

164-
var testConfig = {};
163+
let testConfig = {};
165164
try {
166165
// try to load a test file
167166
testConfig = require(path.join(testDirectory, "test.config.js"));
@@ -174,7 +173,7 @@ describe("WatchTestCases", function() {
174173
runIdx++;
175174
if(runIdx < runs.length) {
176175
run = runs[runIdx];
177-
setTimeout(function() {
176+
setTimeout(() => {
178177
copyDiff(path.join(testDirectory, run.name), tempDirectory);
179178
}, 50);
180179
} else {

0 commit comments

Comments
 (0)