Skip to content

Commit

Permalink
allow running JS tests for a single plugin in tests:run-js (#20816)
Browse files Browse the repository at this point in the history
  • Loading branch information
diosmosis committed Jun 6, 2023
1 parent 742cbe1 commit 00215b0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
8 changes: 8 additions & 0 deletions plugins/TestRunner/Commands/TestsRunJS.php
Expand Up @@ -16,25 +16,33 @@ protected function configure()
$this->setName('tests:run-js');
$this->setDescription('Run javascript tests');
$this->addRequiredValueOption('matomo-url', null, 'Custom matomo url. Defaults to http://localhost');
$this->addOptionalValueOption('plugin', null, 'The plugin to run tests for. If not supplied, all tests are run.');
}

protected function doExecute(): int
{
$input = $this->getInput();
$output = $this->getOutput();
$matomoUrl = $input->getOption('matomo-url') ?? 'http://localhost';
$plugin = $input->getOption('plugin');

$screenshotTestingDir = PIWIK_INCLUDE_PATH . "/tests/lib/screenshot-testing";
$javascriptTestingDir = PIWIK_INCLUDE_PATH . "/tests/javascript";

$cmdNode = "cd '$javascriptTestingDir' && NODE_PATH='$screenshotTestingDir/node_modules' node testrunnerNode.js '$matomoUrl/tests/javascript/'";
if (!empty($plugin)) {
$cmdNode .= ' --plugin=' . escapeshellarg($plugin);
}

$output->writeln('Executing command: <info>' . $cmdNode . '</info>');
$output->writeln('');

passthru($cmdNode, $returnCodeNode);

$cmdPhantom = "phantomjs $javascriptTestingDir/testrunnerPhantom.js '$matomoUrl/tests/javascript/'";
if (!empty($plugin)) {
$cmdPhantom .= ' --plugin=' . escapeshellarg($plugin);
}

$output->writeln('');
$output->writeln('');
Expand Down
12 changes: 10 additions & 2 deletions tests/javascript/testrunnerNode.js
Expand Up @@ -9,7 +9,10 @@
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; // ignore ssl errors

const puppeteer = require('puppeteer');
const url = process.argv[2] || 'http://localhost/tests/javascript/';
const baseUrl = process.argv[2] || 'http://localhost/tests/javascript/';

const pluginArg = process.argv.find((arg) => /--plugin=(.*?)/.test(arg));
const plugin = pluginArg ?pluginArg.split('=', 2)[1] : null;

main();

Expand All @@ -22,6 +25,11 @@ async function main() {
console.log("[" + consoleMessage.type() + "] " + consoleMessage.text());
});

let url = baseUrl;
if (plugin) {
url += `?module=${encodeURIComponent(plugin)}`;
}

await page.goto(url);
await page.waitForFunction(() => window.QUnit);

Expand Down Expand Up @@ -64,4 +72,4 @@ async function main() {
});

process.exit(success ? 0 : 1);
}
}
20 changes: 18 additions & 2 deletions tests/javascript/testrunnerPhantom.js
Expand Up @@ -23,7 +23,23 @@

var fs = require("fs");
var system = require("system");
var url = system.args[1] || 'http://localhost/tests/javascript/';
var baseUrl = system.args[1] || 'http://localhost/tests/javascript/';

function getPluginArg() {
for (var i = 0; i < system.args.length; ++i) {
if (/--plugin=(.*?)/.test(system.args[i])) {
return system.args[i].split('=', 2)[1];
}
}
return null;
}

var plugin = getPluginArg();

var url = baseUrl;
if (plugin) {
url += '?module=' + encodeURIComponent(plugin);
}

function printError(message) {
console.error(message + "\n");
Expand Down Expand Up @@ -95,4 +111,4 @@ page.open(url, function(success) {
printError("Failure opening " + url);
phantom.exit(1);
}
});
});

0 comments on commit 00215b0

Please sign in to comment.