Index: test/browser.js |
=================================================================== |
rename from test_runner.js |
rename to test/browser.js |
--- a/test_runner.js |
+++ b/test/browser.js |
@@ -14,30 +14,29 @@ |
* You should have received a copy of the GNU General Public License |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
/* eslint-env node */ |
"use strict"; |
-const fs = require("fs"); |
+const assert = require("assert"); |
const path = require("path"); |
const MemoryFS = require("memory-fs"); |
-const nodeunit = require("nodeunit"); |
const webpack = require("webpack"); |
-const chromiumRemoteProcess = require("./test/runners/chromium_remote_process"); |
-const chromiumProcess = require("./test/runners/chromium_process"); |
-const edgeProcess = require("./test/runners/edge_process"); |
-const firefoxProcess = require("./test/runners/firefox_process"); |
+const chromiumRemoteProcess = require("./runners/chromium_remote_process"); |
+const chromiumProcess = require("./runners/chromium_process"); |
+const edgeProcess = require("./runners/edge_process"); |
+const firefoxProcess = require("./runners/firefox_process"); |
-let unitFiles = []; |
-let browserFiles = []; |
+// Path are to be from the top-level directory |
+let browserFiles = ["./test/browser/elemHideEmulation.js", "./test/browser/snippets.js"]; |
let runnerDefinitions = { |
// Chromium with chrome-remote-interface |
chromium_remote: chromiumRemoteProcess, |
// Chromium with WebDriver (requires Chromium >= 63.0.3239) |
chromium: chromiumProcess, |
edge: edgeProcess, |
firefox: firefoxProcess |
@@ -57,42 +56,16 @@ |
return ["chromium_remote", "firefox"]; |
} |
return runners.filter(runner => runnerDefinitions.hasOwnProperty(runner)); |
} |
let runnerProcesses = configureRunners(); |
-function addTestPaths(testPaths, recurse) |
-{ |
- for (let testPath of testPaths) |
- { |
- let stat = fs.statSync(testPath); |
- if (stat.isDirectory()) |
- { |
- if (recurse) |
- { |
- addTestPaths(fs.readdirSync(testPath).map( |
- file => path.join(testPath, file))); |
- } |
- continue; |
- } |
- if (path.basename(testPath).startsWith("_")) |
- continue; |
- if (path.extname(testPath) == ".js") |
- { |
- if (testPath.split(path.sep).includes("browser")) |
- browserFiles.push(testPath); |
- else |
- unitFiles.push(testPath); |
- } |
- } |
-} |
- |
function webpackInMemory(bundleFilename, options) |
{ |
return new Promise((resolve, reject) => |
{ |
// Based on this example |
// https://webpack.js.org/api/node/#custom-file-systems |
let memoryFS = new MemoryFS(); |
@@ -119,77 +92,58 @@ |
let bundle = memoryFS.readFileSync("/" + bundleFilename, "utf-8"); |
memoryFS.unlinkSync("/" + bundleFilename); |
resolve(bundle); |
} |
}); |
}); |
} |
-function runBrowserTests(processes) |
+describe("Browser tests", function() |
{ |
- if (!browserFiles.length) |
- return Promise.resolve(); |
+ assert.ok(browserFiles.length); |
- let nodeunitPath = path.join(__dirname, "node_modules", "nodeunit", |
- "examples", "browser", "nodeunit.js"); |
+ this.timeout(0); |
+ |
let bundleFilename = "bundle.js"; |
- return webpackInMemory(bundleFilename, { |
- entry: path.join(__dirname, "test", "browser", "_bootstrap.js"), |
- module: { |
- rules: [{ |
- resource: nodeunitPath, |
- // I would have rather 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 |
+ let bundle; |
+ let mochaPath = path.join(__dirname, "..", "node_modules", "mocha", |
+ "mocha.js"); |
+ before(async() => |
+ { |
+ bundle = await webpackInMemory(bundleFilename, { |
+ entry: path.join(__dirname, "browser", "_bootstrap.js"), |
+ module: { |
+ rules: [{ |
+ // we use the browser version of mocha |
+ resource: mochaPath, |
+ use: ["script-loader"] |
+ }] |
}, |
- modules: [path.resolve(__dirname, "lib")] |
- } |
- }).then(bundle => |
- Promise.all( |
- processes.map(currentProcess => |
- runnerDefinitions[currentProcess]( |
- bundle, bundleFilename, |
- browserFiles.map( |
- file => path.relative(path.join(__dirname, "test", "browser"), |
- file).replace(/\.js$/, "") |
- ) |
- ) |
- // We need to convert rejected promise to a resolved one |
- // or the test will not let close the webdriver. |
- .catch(e => e) |
- )).then(results => |
- { |
- let errors = results.filter(e => typeof e != "undefined"); |
- if (errors.length) |
- throw `Browser unit test failed: ${errors.join(", ")}`; |
- }) |
- ); |
-} |
+ resolve: { |
+ alias: { |
+ mocha$: mochaPath |
+ }, |
+ modules: [path.resolve(__dirname, "..", "lib")] |
+ } |
+ }); |
+ }); |
-if (process.argv.length > 2) |
- addTestPaths(process.argv.slice(2), true); |
-else |
-{ |
- addTestPaths( |
- [path.join(__dirname, "test"), path.join(__dirname, "test", "browser")], |
- true |
- ); |
-} |
+ it("Runner prerequisites", () => |
+ { |
+ assert.ok(bundle); |
+ assert.notEqual(runnerProcesses.length, 0, "No runners was found"); |
+ }); |
-runBrowserTests(runnerProcesses).then(() => |
-{ |
- if (unitFiles.length) |
- nodeunit.reporters.default.run(unitFiles); |
-}).catch(error => |
-{ |
- console.error(error); |
- process.exit(1); |
+ runnerProcesses.map(currentProcess => |
+ { |
+ it(currentProcess, () => |
+ runnerDefinitions[currentProcess]( |
+ bundle, bundleFilename, |
+ browserFiles.map( |
+ file => path.relative(path.join(__dirname, "browser"), |
+ file).replace(/\.js$/, "") |
+ ) |
+ ) |
+ ); |
+ }); |
}); |