Skip to content

Commit b7ce843

Browse files
authored
Merge pull request webpack#6396 from niieani/jest
Convert tests to Jest
2 parents 7a70020 + 3d23bbc commit b7ce843

File tree

309 files changed

+1634
-1645
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

309 files changed

+1634
-1645
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
"publish-patch": "npm run lint && npm run beautify-lint && mocha && npm version patch && git push && git push --tags && npm publish"
118118
},
119119
"jest": {
120-
"testEnvironment": "node"
120+
"testEnvironment": "node",
121+
"setupTestFrameworkScriptFile": "<rootDir>/test/setupTestFramework.js",
122+
"testMatch": [
123+
"<rootDir>/test/*.test.js",
124+
"<rootDir>/test/*.unittest.js"
125+
]
121126
}
122127
}

test/BenchmarkTestCases.benchmark.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"use strict";
22

3-
require("should");
43
const path = require("path");
54
const fs = require("fs");
65
const asyncLib = require("async");
7-
var Test = require("mocha/lib/test");
86

97
const Benchmark = require("benchmark");
108

@@ -24,8 +22,7 @@ describe("BenchmarkTestCases", function() {
2422
fs.mkdirSync(baselinesPath);
2523
} catch(e) {}
2624

27-
before(function(done) {
28-
this.timeout(270000);
25+
beforeAll(function(done) {
2926
const git = require("simple-git");
3027
const rootPath = path.join(__dirname, "..");
3128
getBaselineRevs(rootPath, (err, baselineRevisions) => {
@@ -65,7 +62,7 @@ describe("BenchmarkTestCases", function() {
6562
}
6663
}, done);
6764
});
68-
});
65+
}, 270000);
6966

7067
function getBaselineRevs(rootPath, callback) {
7168
const git = require("simple-git")(rootPath);
@@ -169,17 +166,10 @@ describe("BenchmarkTestCases", function() {
169166
tests.forEach(testName => {
170167
const testDirectory = path.join(casesPath, testName);
171168
let headStats = null;
172-
const suite = describe(testName, function() {});
173-
it(`${testName} create benchmarks`, function() {
169+
describe(`${testName} create benchmarks`, function() {
174170
baselines.forEach(baseline => {
175171
let baselineStats = null;
176-
177-
function it(title, fn) {
178-
const test = new Test(title, fn);
179-
suite.addTest(test);
180-
}
181172
it(`should benchmark ${baseline.name} (${baseline.rev})`, function(done) {
182-
this.timeout(180000);
183173
const outputDirectory = path.join(__dirname, "js", "benchmark", `baseline-${baseline.name}`, testName);
184174
const config = Object.create(require(path.join(testDirectory, "webpack.config.js")));
185175
config.output = Object.create(config.output || {});
@@ -194,7 +184,7 @@ describe("BenchmarkTestCases", function() {
194184
baselineStats = stats;
195185
done();
196186
});
197-
});
187+
}, 180000);
198188

199189
if(baseline.name !== "HEAD") {
200190
it(`HEAD should not be slower than ${baseline.name} (${baseline.rev})`, function() {

test/ConfigTestCases.test.js

Lines changed: 101 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"use strict";
22

3-
/* globals describe it */
4-
require("should");
3+
/* globals describe expect it fit */
54
const path = require("path");
65
const fs = require("fs");
76
const vm = require("vm");
87
const mkdirp = require("mkdirp");
9-
const Test = require("mocha/lib/test");
108
const checkArrayExpectation = require("./checkArrayExpectation");
9+
const async = require("async");
1110

1211
const Stats = require("../lib/Stats");
1312
const webpack = require("../lib/webpack");
@@ -36,113 +35,113 @@ describe("ConfigTestCases", () => {
3635
categories.forEach((category) => {
3736
describe(category.name, () => {
3837
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+
webpack(options, (err, stats) => {
70+
if(err) {
71+
const fakeStats = {
72+
errors: [err.stack]
73+
};
74+
if(checkArrayExpectation(testDirectory, fakeStats, "error", "Error", done)) return;
75+
// Wait for uncatched errors to occur
76+
return setTimeout(done, 200);
6177
}
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) {}
78+
const statOptions = Stats.presetToOptions("verbose");
79+
statOptions.colors = false;
80+
mkdirp.sync(outputDirectory);
81+
fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8");
82+
const jsonStats = stats.toJson({
83+
errorDetails: true
84+
});
85+
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
86+
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
87+
let exportedTests = [];
6988

70-
this.timeout(testConfig.timeout);
89+
function _it(title, fn) {
90+
exportedTests.push(fit(title, fn, testConfig.timeout));
91+
}
7192

72-
webpack(options, (err, stats) => {
73-
if(err) {
74-
const fakeStats = {
75-
errors: [err.stack]
93+
const globalContext = {
94+
console: console,
95+
expect: expect
7696
};
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;
9197

92-
function _it(title, fn) {
93-
const test = new Test(title, fn);
94-
suite.addTest(test);
95-
exportedTests++;
96-
return test;
97-
}
98-
99-
const globalContext = {
100-
console: console
101-
};
98+
function _require(currentDirectory, module) {
99+
if(Array.isArray(module) || /^\.\.?\//.test(module)) {
100+
let fn;
101+
let content;
102+
let p;
103+
if(Array.isArray(module)) {
104+
p = path.join(currentDirectory, module[0]);
105+
content = module.map((arg) => {
106+
p = path.join(currentDirectory, arg);
107+
return fs.readFileSync(p, "utf-8");
108+
}).join("\n");
109+
} else {
110+
p = path.join(currentDirectory, module);
111+
content = fs.readFileSync(p, "utf-8");
112+
}
113+
if(options.target === "web" || options.target === "webworker") {
114+
fn = vm.runInNewContext("(function(require, module, exports, __dirname, __filename, it, expect, window) {" + content + "\n})", globalContext, p);
115+
} else {
116+
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, expect) {" + content + "\n})", p);
117+
}
118+
const m = {
119+
exports: {}
120+
};
121+
fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, expect, globalContext);
122+
return m.exports;
123+
} else if(testConfig.modules && module in testConfig.modules) {
124+
return testConfig.modules[module];
125+
} else return require(module);
126+
}
127+
let filesCount = 0;
102128

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");
117-
}
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);
129+
if(testConfig.noTests) return process.nextTick(done);
130+
for(let i = 0; i < optionsArr.length; i++) {
131+
const bundlePath = testConfig.findBundle(i, optionsArr[i]);
132+
if(bundlePath) {
133+
filesCount++;
134+
_require(outputDirectory, bundlePath);
122135
}
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);
140136
}
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);
137+
// give a free pass to compilation that generated an error
138+
if(!jsonStats.errors.length && filesCount !== optionsArr.length) return done(new Error("Should have found at least one bundle file per webpack config"));
139+
if(exportedTests.length < filesCount) return done(new Error("No tests exported by test case"));
140+
async.waterfall(
141+
exportedTests.map(test => (callback) => test.execute(callback, true)),
142+
done
143+
);
144+
});
146145
});
147146
});
148147
});

test/ExternalModule.unittest.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe("ExternalModule", () => {
6060
expect(externalModule.getSource.callCount).toBe(1);
6161
expect(externalModule.getSourceString.callCount).toBe(1);
6262
expect(externalModule.getSource.args[0][0]).toBe(expectedString);
63-
expect(result).toBe(expectedSource);
63+
expect(result).toEqual(expectedSource);
6464
});
6565
});
6666

@@ -109,7 +109,7 @@ describe("ExternalModule", () => {
109109
const result = externalModule.getSourceForGlobalVariableExternal(varName, type);
110110

111111
// check
112-
expect(result).toBe(expected);
112+
expect(result).toEqual(expected);
113113
});
114114
});
115115
describe("given an single variable name", () => {
@@ -123,7 +123,7 @@ describe("ExternalModule", () => {
123123
const result = externalModule.getSourceForGlobalVariableExternal(varName, type);
124124

125125
// check
126-
expect(result).toBe(expected);
126+
expect(result).toEqual(expected);
127127
});
128128
});
129129
});
@@ -139,7 +139,7 @@ describe("ExternalModule", () => {
139139
const result = externalModule.getSourceForCommonJsExternal(varName, type);
140140

141141
// check
142-
expect(result).toBe(expected);
142+
expect(result).toEqual(expected);
143143
});
144144
});
145145
describe("given an single variable name", () => {
@@ -153,7 +153,7 @@ describe("ExternalModule", () => {
153153
const result = externalModule.getSourceForCommonJsExternal(varName, type);
154154

155155
// check
156-
expect(result).toBe(expected);
156+
expect(result).toEqual(expected);
157157
});
158158
});
159159
});
@@ -170,7 +170,7 @@ describe("ExternalModule", () => {
170170
const result = externalModule.checkExternalVariable(variableToCheck, request);
171171

172172
// check
173-
expect(result).toBe(expected);
173+
expect(result).toEqual(expected);
174174
});
175175
});
176176

@@ -185,7 +185,7 @@ describe("ExternalModule", () => {
185185
const result = externalModule.getSourceForAmdOrUmdExternal(id, optional, request);
186186

187187
// check
188-
expect(result).toBe(expected);
188+
expect(result).toEqual(expected);
189189
});
190190
describe("given an optinal check is set", () => {
191191
it("ads a check for the existance of the variable before looking it up", () => {
@@ -199,7 +199,7 @@ module.exports = __WEBPACK_EXTERNAL_MODULE_someId__;`;
199199
const result = externalModule.getSourceForAmdOrUmdExternal(id, optional, request);
200200

201201
// check
202-
expect(result).toBe(expected);
202+
expect(result).toEqual(expected);
203203
});
204204
});
205205
});
@@ -214,7 +214,7 @@ module.exports = __WEBPACK_EXTERNAL_MODULE_someId__;`;
214214
const result = externalModule.getSourceForDefaultCase(optional, request);
215215

216216
// check
217-
expect(result).toBe(expected);
217+
expect(result).toEqual(expected);
218218
});
219219
describe("given an optinal check is requested", () => {
220220
it("checks for the existance of the request setting it", () => {
@@ -227,7 +227,7 @@ module.exports = some/request;`;
227227
const result = externalModule.getSourceForDefaultCase(optional, request);
228228

229229
// check
230-
expect(result).toBe(expected);
230+
expect(result).toEqual(expected);
231231
});
232232
});
233233
});

0 commit comments

Comments
 (0)