Skip to content

Commit c1befdd

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

1 file changed

Lines changed: 62 additions & 63 deletions

File tree

‎test/Stats.test.js‎

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1+
"use strict";
2+
13
/*globals describe it */
2-
var should = require("should");
3-
var path = require("path");
4-
var fs = require("fs");
5-
var mkdirp = require("mkdirp");
4+
const should = require("should");
5+
const path = require("path");
6+
const fs = require("fs");
67

7-
var webpack = require("../lib/webpack");
8+
const webpack = require("../lib/webpack");
89

9-
var base = path.join(__dirname, "statsCases");
10-
var outputBase = path.join(__dirname, "js", "stats");
11-
var tests = fs.readdirSync(base).filter(function(testName) {
12-
return fs.existsSync(path.join(base, testName, "index.js")) ||
13-
fs.existsSync(path.join(base, testName, "webpack.config.js"))
10+
const base = path.join(__dirname, "statsCases");
11+
const outputBase = path.join(__dirname, "js", "stats");
12+
const tests = fs.readdirSync(base).filter(testName => {
13+
fs.existsSync(path.join(base, testName, "index.js")) ||
14+
fs.existsSync(path.join(base, testName, "webpack.config.js"));
1415
});
15-
var Stats = require("../lib/Stats");
16+
const Stats = require("../lib/Stats");
1617

17-
describe("Stats", function() {
18-
tests.forEach(function(testName) {
19-
it("should print correct stats for " + testName, function(done) {
20-
var options = {
18+
describe("Stats", () => {
19+
tests.forEach(testName => {
20+
it("should print correct stats for " + testName, (done) => {
21+
let options = {
2122
entry: "./index",
2223
output: {
2324
filename: "bundle.js"
@@ -26,27 +27,27 @@ describe("Stats", function() {
2627
if(fs.existsSync(path.join(base, testName, "webpack.config.js"))) {
2728
options = require(path.join(base, testName, "webpack.config.js"));
2829
}
29-
(Array.isArray(options) ? options : [options]).forEach(function(options) {
30+
(Array.isArray(options) ? options : [options]).forEach((options) => {
3031
if(!options.context) options.context = path.join(base, testName);
3132
if(!options.output) options.output = options.output || {};
3233
if(!options.output.path) options.output.path = path.join(outputBase, testName);
3334
});
34-
var c = webpack(options);
35-
var compilers = c.compilers ? c.compilers : [c];
36-
compilers.forEach(function(c) {
37-
var ifs = c.inputFileSystem;
35+
const c = webpack(options);
36+
const compilers = c.compilers ? c.compilers : [c];
37+
compilers.forEach((c) => {
38+
const ifs = c.inputFileSystem;
3839
c.inputFileSystem = Object.create(ifs);
39-
c.inputFileSystem.readFile = function() {
40-
var args = Array.prototype.slice.call(arguments);
41-
var callback = args.pop();
42-
ifs.readFile.apply(ifs, args.concat([function(err, result) {
40+
c.inputFileSystem.readFile = () => {
41+
const args = Array.prototype.slice.call(arguments);
42+
const callback = args.pop();
43+
ifs.readFile.apply(ifs, args.concat([(err, result) => {
4344
if(err) return callback(err);
4445
callback(null, result.toString("utf-8").replace(/\r/g, ""));
4546
}]));
4647
};
4748
c.apply(new webpack.optimize.OccurrenceOrderPlugin());
4849
});
49-
c.run(function(err, stats) {
50+
c.run((err, stats) => {
5051
if(err) return done(err);
5152

5253
if(/error$/.test(testName)) {
@@ -55,10 +56,10 @@ describe("Stats", function() {
5556
done(new Error(stats.toJson().errors.join("\n\n")));
5657
}
5758

58-
var toStringOptions = {
59+
let toStringOptions = {
5960
colors: false
6061
};
61-
var hasColorSetting = false;
62+
let hasColorSetting = false;
6263
if(typeof options.stats !== "undefined") {
6364
toStringOptions = options.stats;
6465

@@ -68,7 +69,7 @@ describe("Stats", function() {
6869
toStringOptions.children = options.map(o => o.stats);
6970
}
7071

71-
var actual = stats.toString(toStringOptions);
72+
let actual = stats.toString(toStringOptions);
7273
(typeof actual).should.be.eql("string");
7374
if(!hasColorSetting) {
7475
actual = actual
@@ -88,7 +89,7 @@ describe("Stats", function() {
8889
.replace(/[\t ]*Version:.+\n/g, "")
8990
.replace(path.join(base, testName), "Xdir/" + testName)
9091
.replace(/ dependencies:Xms/g, "");
91-
var expected = fs.readFileSync(path.join(base, testName, "expected.txt"), "utf-8").replace(/\r/g, "");
92+
const expected = fs.readFileSync(path.join(base, testName, "expected.txt"), "utf-8").replace(/\r/g, "");
9293
if(actual !== expected) {
9394
fs.writeFileSync(path.join(base, testName, "actual.txt"), actual, "utf-8");
9495
} else if(fs.existsSync(path.join(base, testName, "actual.txt"))) {
@@ -99,62 +100,60 @@ describe("Stats", function() {
99100
});
100101
});
101102
});
102-
describe("Error Handling", function() {
103-
describe("does have", function() {
104-
it("hasErrors", function() {
105-
var mockStats = new Stats({
106-
errors: ['firstError'],
107-
hash: '1234'
103+
describe("Error Handling", () => {
104+
describe("does have", () => {
105+
it("hasErrors", () => {
106+
const mockStats = new Stats({
107+
errors: ["firstError"],
108+
hash: "1234"
108109
});
109110
mockStats.hasErrors().should.be.ok();
110111
});
111-
it("hasWarnings", function() {
112-
var mockStats = new Stats({
113-
warnings: ['firstError'],
114-
hash: '1234'
112+
it("hasWarnings", () => {
113+
const mockStats = new Stats({
114+
warnings: ["firstError"],
115+
hash: "1234"
115116
});
116117
mockStats.hasWarnings().should.be.ok();
117118
});
118119
});
119-
describe("does not have", function() {
120-
it("hasErrors", function() {
121-
var mockStats = new Stats({
120+
describe("does not have", () => {
121+
it("hasErrors", () => {
122+
const mockStats = new Stats({
122123
errors: [],
123-
hash: '1234'
124+
hash: "1234"
124125
});
125126
mockStats.hasErrors().should.not.be.ok();
126127
});
127-
it("hasWarnings", function() {
128-
var mockStats = new Stats({
128+
it("hasWarnings", () => {
129+
const mockStats = new Stats({
129130
warnings: [],
130-
hash: '1234'
131+
hash: "1234"
131132
});
132133
mockStats.hasWarnings().should.not.be.ok();
133134
});
134135
});
135-
it("formatError handles string errors", function() {
136-
var mockStats = new Stats({
137-
errors: ['firstError'],
136+
it("formatError handles string errors", () => {
137+
const mockStats = new Stats({
138+
errors: ["firstError"],
138139
warnings: [],
139140
assets: [],
140141
entrypoints: {},
141142
chunks: [],
142143
modules: [],
143144
children: [],
144-
hash: '1234',
145+
hash: "1234",
145146
mainTemplate: {
146-
getPublicPath: function() {
147-
return 'path';
148-
}
147+
getPublicPath: () => "path"
149148
}
150149
});
151-
var obj = mockStats.toJson();
152-
obj.errors[0].should.be.equal('firstError');
150+
const obj = mockStats.toJson();
151+
obj.errors[0].should.be.equal("firstError");
153152
});
154153
});
155-
describe("Presets", function() {
156-
describe("presetToOptions", function() {
157-
it("returns correct object with 'Normal'", function() {
154+
describe("Presets", () => {
155+
describe("presetToOptions", () => {
156+
it("returns correct object with 'Normal'", () => {
158157
Stats.presetToOptions("Normal").should.eql({
159158
assets: false,
160159
version: false,
@@ -172,16 +171,16 @@ describe("Stats", function() {
172171
performance: true
173172
});
174173
});
175-
it("truthy values behave as 'normal'", function() {
176-
var normalOpts = Stats.presetToOptions('normal');
174+
it("truthy values behave as 'normal'", () => {
175+
const normalOpts = Stats.presetToOptions("normal");
177176
Stats.presetToOptions("pizza").should.eql(normalOpts);
178177
Stats.presetToOptions(true).should.eql(normalOpts);
179178
Stats.presetToOptions(1).should.eql(normalOpts);
180179

181180
Stats.presetToOptions("verbose").should.not.eql(normalOpts);
182181
Stats.presetToOptions(false).should.not.eql(normalOpts);
183182
});
184-
it("returns correct object with 'none'", function() {
183+
it("returns correct object with 'none'", () => {
185184
Stats.presetToOptions("none").should.eql({
186185
hash: false,
187186
version: false,
@@ -204,8 +203,8 @@ describe("Stats", function() {
204203
performance: false
205204
});
206205
});
207-
it("falsy values behave as 'none'", function() {
208-
var noneOpts = Stats.presetToOptions('none');
206+
it("falsy values behave as 'none'", () => {
207+
const noneOpts = Stats.presetToOptions("none");
209208
Stats.presetToOptions("").should.eql(noneOpts);
210209
Stats.presetToOptions(null).should.eql(noneOpts);
211210
Stats.presetToOptions().should.eql(noneOpts);

0 commit comments

Comments
 (0)