Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: test_runner.js

Issue 29517687: Issue 5079, 5516 - Use webpack for browser tests, modules for content scripts (Closed)
Patch Set: runBrowserTests should always return a Promise Created Aug. 17, 2017, 12:59 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/browser/elemHideEmulation.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test_runner.js
diff --git a/test_runner.js b/test_runner.js
index 094c7b93296276b1d24cc9d5e73ecfecd3b248d9..da2f25f4aa9d3e5a8ff68e8c40e70ba19a7af8fc 100644
--- a/test_runner.js
+++ b/test_runner.js
@@ -21,9 +21,10 @@
const fs = require("fs");
const path = require("path");
-const url = require("url");
const nodeunit = require("nodeunit");
+const webpack = require("webpack");
+const MemoryFS = require("memory-fs");
const chromiumProcess = require("./chromium_process");
@@ -49,41 +50,84 @@ function addTestPaths(testPaths, recurse)
if (path.extname(testPath) == ".js")
{
if (testPath.split(path.sep).includes("browser"))
- browserFiles.push(testPath);
+ {
+ browserFiles.push(
+ path.relative(path.join(__dirname, "test", "browser"),
+ testPath).replace(/\.js$/, "")
+ );
+ }
else
unitFiles.push(testPath);
}
}
}
-function getFileURL(filePath)
+function webpackInMemory(bundleFilename, options)
{
- return url.format({
- protocol: "file",
- slashes: "true",
- pathname: path.resolve(process.cwd(), filePath).split(path.sep).join("/")
+ return new Promise((resolve, reject) =>
+ {
+ // Based on this example https://webpack.js.org/api/node/#custom-file-systems
+ let memoryFS = new MemoryFS();
+
+ options.output = {filename: bundleFilename, path: "/"};
+ let webpackCompiler = webpack(options);
+ webpackCompiler.outputFileSystem = memoryFS;
+
+ webpackCompiler.run((err, stats) =>
+ {
+ // Error handling is based on this example
+ // https://webpack.js.org/api/node/#error-handling
+ if (err)
+ {
+ let reason = err.stack || err;
+ if (err.details)
+ reason += "\n" + err.details;
+ reject(reason);
+ }
+ else if (stats.hasErrors())
+ reject(stats.toJson().errors);
+ else
+ {
+ let bundle = memoryFS.readFileSync("/" + bundleFilename, "utf-8");
+ memoryFS.unlinkSync("/" + bundleFilename);
+ resolve(bundle);
+ }
+ });
});
}
function runBrowserTests()
{
if (!browserFiles.length)
- return;
+ return Promise.resolve();
Wladimir Palant 2017/08/17 13:06:11 Or maybe just change the call below from runBrowse
kzar 2017/08/17 13:26:43 Done.
- // Navigate to this directory because about:blank won't be allowed to load
- // file:/// URLs.
- let initialPage = getFileURL(__dirname);
- let bootstrapPath = path.join(__dirname, "test", "browser",
- "_bootstrap.js");
- let nodeunitPath = path.join(
- path.dirname(require.resolve("nodeunit")),
- "examples", "browser", "nodeunit.js"
- );
- let args = [
- getFileURL(nodeunitPath),
- ...browserFiles.map(getFileURL)
- ];
- return chromiumProcess(initialPage, bootstrapPath, args);
+ let nodeunitPath = path.join(__dirname, "node_modules", "nodeunit",
+ "examples", "browser", "nodeunit.js");
+ let bundleFilename = "bundle.js";
+
+ return webpackInMemory(bundleFilename, {
+ entry: path.join(__dirname, "test", "browser", "_bootstrap.js"),
+ module: {
+ rules: [{
+ resource: nodeunitPath,
+ // I would have rathered used exports-loader here, to avoid treating
+ // nodeunit as a global. Unfortunately the nodeunit browser example
+ // script is quite slopily put together, if exports isn't falsey it
+ // breaks! As a workaround we need to use script-loader, which means
+ // that exports is falsey for that script as a side-effect.
+ use: ["script-loader"]
+ }]
+ },
+ resolve: {
+ alias: {
+ nodeunit$: nodeunitPath
+ },
+ modules: [path.resolve(__dirname, "lib")]
+ }
+ }).then(bundle =>
+ {
+ return chromiumProcess(bundle, bundleFilename, browserFiles);
+ });
}
if (process.argv.length > 2)
@@ -96,7 +140,7 @@ else
);
}
-Promise.resolve(runBrowserTests()).catch(error =>
+runBrowserTests().catch(error =>
{
console.error("Failed running browser tests");
console.error(error);
« no previous file with comments | « test/browser/elemHideEmulation.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld