webpack/test/ConfigTestCases.test.js

290 lines
8.3 KiB
JavaScript
Raw Normal View History

2017-01-18 17:08:00 +08:00
"use strict";
/* globals describe expect it beforeAll */
2017-01-18 17:08:00 +08:00
const path = require("path");
const fs = require("fs");
const vm = require("vm");
const mkdirp = require("mkdirp");
2018-05-17 23:29:07 +08:00
const rimraf = require("rimraf");
2017-01-18 17:08:00 +08:00
const checkArrayExpectation = require("./checkArrayExpectation");
2014-06-18 04:34:30 +08:00
2017-01-18 17:08:00 +08:00
const Stats = require("../lib/Stats");
const webpack = require("../lib/webpack");
2018-04-15 03:46:33 +08:00
const prepareOptions = require("./helpers/prepareOptions");
2014-06-18 04:34:30 +08:00
2017-01-18 17:08:00 +08:00
describe("ConfigTestCases", () => {
const casesPath = path.join(__dirname, "configCases");
let categories = fs.readdirSync(casesPath);
2018-02-25 18:46:17 +08:00
categories = categories.map(cat => {
2014-06-18 04:34:30 +08:00
return {
name: cat,
2018-02-25 18:46:17 +08:00
tests: fs
.readdirSync(path.join(casesPath, cat))
.filter(folder => {
return folder.indexOf("_") < 0;
})
.sort()
.filter(testName => {
const testDirectory = path.join(casesPath, cat, testName);
const filterPath = path.join(testDirectory, "test.filter.js");
if (fs.existsSync(filterPath) && !require(filterPath)()) {
describe.skip(testName, () => it("filtered"));
return false;
}
return true;
})
};
});
2018-02-25 18:46:17 +08:00
categories.forEach(category => {
2017-01-18 17:08:00 +08:00
describe(category.name, () => {
2018-02-25 18:46:17 +08:00
category.tests.forEach(testName => {
2018-03-06 05:48:18 +08:00
describe(testName, function() {
const testDirectory = path.join(casesPath, category.name, testName);
2018-02-25 18:46:17 +08:00
const outputDirectory = path.join(
__dirname,
"js",
"config",
category.name,
testName
);
const exportedTests = [];
const exportedBeforeEach = [];
const exportedAfterEach = [];
2018-03-06 05:48:18 +08:00
it(
testName + " should compile",
2018-02-25 18:46:17 +08:00
() =>
new Promise((resolve, reject) => {
const done = err => {
if (err) return reject(err);
resolve();
2018-01-27 05:26:41 +08:00
};
2018-05-17 23:29:07 +08:00
rimraf.sync(outputDirectory);
mkdirp.sync(outputDirectory);
2018-02-25 18:46:17 +08:00
const options = prepareOptions(
require(path.join(testDirectory, "webpack.config.js")),
{ testPath: outputDirectory }
);
const optionsArr = [].concat(options);
optionsArr.forEach((options, idx) => {
if (!options.context) options.context = testDirectory;
if (!options.mode) options.mode = "production";
if (!options.optimization) options.optimization = {};
if (options.optimization.minimize === undefined)
options.optimization.minimize = false;
if (!options.entry) options.entry = "./index.js";
if (!options.target) options.target = "async-node";
if (!options.output) options.output = {};
if (!options.output.path)
options.output.path = outputDirectory;
if (typeof options.output.pathinfo === "undefined")
options.output.pathinfo = true;
if (!options.output.filename)
options.output.filename = "bundle" + idx + ".js";
});
let testConfig = {
findBundle: function(i, options) {
if (
fs.existsSync(
path.join(options.output.path, "bundle" + i + ".js")
)
) {
return "./bundle" + i + ".js";
}
},
timeout: 30000
};
try {
// try to load a test file
testConfig = Object.assign(
testConfig,
require(path.join(testDirectory, "test.config.js"))
);
2018-03-06 05:48:18 +08:00
} catch (e) {
// ignored
}
2015-08-09 18:42:43 +08:00
2018-02-25 18:46:17 +08:00
webpack(options, (err, stats) => {
if (err) {
const fakeStats = {
errors: [err.stack]
};
if (
checkArrayExpectation(
testDirectory,
fakeStats,
"error",
"Error",
done
)
)
return;
// Wait for uncaught errors to occur
return setTimeout(done, 200);
2018-01-27 05:26:41 +08:00
}
2018-02-25 18:46:17 +08:00
const statOptions = Stats.presetToOptions("verbose");
statOptions.colors = false;
mkdirp.sync(outputDirectory);
fs.writeFileSync(
path.join(outputDirectory, "stats.txt"),
stats.toString(statOptions),
"utf-8"
);
const jsonStats = stats.toJson({
errorDetails: true
});
if (
checkArrayExpectation(
testDirectory,
jsonStats,
"error",
"Error",
done
)
)
return;
if (
checkArrayExpectation(
testDirectory,
jsonStats,
"warning",
"Warning",
done
)
)
return;
function _it(title, fn) {
exportedTests.push({
title,
fn,
timeout: testConfig.timeout
});
2018-01-27 05:26:41 +08:00
}
2018-02-25 18:46:17 +08:00
function _beforeEach(fn) {
return exportedBeforeEach.push(fn);
}
2015-08-09 18:42:43 +08:00
function _afterEach(fn) {
return exportedAfterEach.push(fn);
}
2018-02-25 18:46:17 +08:00
const globalContext = {
console: console,
expect: expect,
setTimeout: setTimeout,
clearTimeout: clearTimeout
2018-01-27 05:26:41 +08:00
};
2016-06-16 05:25:40 +08:00
2018-02-25 18:46:17 +08:00
function _require(currentDirectory, module) {
if (Array.isArray(module) || /^\.\.?\//.test(module)) {
let fn;
let content;
let p;
if (Array.isArray(module)) {
p = path.join(currentDirectory, module[0]);
content = module
.map(arg => {
p = path.join(currentDirectory, arg);
return fs.readFileSync(p, "utf-8");
})
.join("\n");
} else {
p = path.join(currentDirectory, module);
content = fs.readFileSync(p, "utf-8");
}
if (
options.target === "web" ||
options.target === "webworker"
) {
fn = vm.runInNewContext(
2018-05-07 21:26:04 +08:00
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach, expect, jest, window) {" +
2018-02-25 18:46:17 +08:00
content +
"\n})",
globalContext,
p
);
} else {
fn = vm.runInThisContext(
2018-05-07 21:26:04 +08:00
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach, expect, jest) {" +
"global.expect = expect; " +
2018-02-25 18:46:17 +08:00
content +
"\n})",
p
);
}
const m = {
exports: {}
};
fn.call(
m.exports,
_require.bind(null, path.dirname(p)),
m,
m.exports,
path.dirname(p),
p,
_it,
_beforeEach,
_afterEach,
2018-02-25 18:46:17 +08:00
expect,
2018-05-07 21:26:04 +08:00
jest,
2018-02-25 18:46:17 +08:00
globalContext
);
return m.exports;
} else if (
testConfig.modules &&
module in testConfig.modules
) {
return testConfig.modules[module];
} else return require(module);
}
let filesCount = 0;
if (testConfig.noTests) return process.nextTick(done);
for (let i = 0; i < optionsArr.length; i++) {
const bundlePath = testConfig.findBundle(i, optionsArr[i]);
if (bundlePath) {
filesCount++;
_require(outputDirectory, bundlePath);
}
}
// give a free pass to compilation that generated an error
if (
!jsonStats.errors.length &&
filesCount !== optionsArr.length
)
return done(
new Error(
"Should have found at least one bundle file per webpack config"
)
);
if (exportedTests.length < filesCount)
return done(new Error("No tests exported by test case"));
if (testConfig.afterExecute) testConfig.afterExecute();
2018-04-11 04:52:22 +08:00
const asyncSuite = describe("exported tests", () => {
exportedBeforeEach.forEach(beforeEach);
exportedAfterEach.forEach(afterEach);
exportedTests.forEach(
({ title, fn, timeout }) =>
2018-04-11 04:52:22 +08:00
fn
? fit(title, fn, timeout)
: fit(title, () => {}).pend("Skipped")
2018-03-06 05:48:18 +08:00
);
});
// workaround for jest running clearSpies on the wrong suite (invoked by clearResourcesForRunnable)
asyncSuite.disabled = true;
2018-04-11 04:52:22 +08:00
jasmine
.getEnv()
.execute([asyncSuite.id], asyncSuite)
.then(done, done);
2018-02-25 18:46:17 +08:00
});
})
);
2014-06-18 04:34:30 +08:00
});
});
});
});
2014-08-22 19:51:24 +08:00
});