I have done something similar with javascript validation, not execution.
I use the command line tool gjslint from Google to validate javascript.
/**
*
* @test
* @dataProvider dataprovider_javascript_file
*/
public function javascript_files_are_valid ($fn){
$dn = getcwd();
$fn = "$dn/$fn";
// most of these are whitespace- file-ending- and missing-semicolon flags
$cmd1 = "gjslint --disable 131,1,2,5,10,11,300,-2 --nojsdoc --max_line_length 12000";
$cmd .= "$cmd1 $fn";
$flag = 1;
$act_string = system($cmd, $flag);
$exp_string = "1 files checked, no errors found.";
$this->assertEquals($exp_string, $act_string, print_r("cmd: $cmd", 1));
}
public function dataprovider_javascript_file() {
$dt->confArray = array("dirname1" => "", "dirname2" => "");
$files = array_filter($dt->confArray, "self::getfilenames2");
$files = array_map("self::val2array", $files);
return $files;
}
private function val2array($v){
return array($v);
}
private function getfilenames2($var){
//fetch js files, but not minified files
//return preg_match('/\.js$/', $var) ; #&& ! preg_match('/\.min\./', $var);
return preg_match('/\.js$/', $var) && ! preg_match('/\.min\./', $var);
}
Here I grab the output from the system() command and check the message string. That's a poor-man's method but it might suffice for a start.
A similar strategy would be to include the headless browser phantomjs to really execute your javascript. Then grab the output string and check that it does not contain any error messages.
I came here to look for some answers where someone might have done that, but didn't find any code so far. maybe have to write it myself.
Edit:
phantomjs-1.9.7-linux-x86_64/bin/phantomjs --web-security=false /phantomjs-1.9.7-linux-x86_64/examples/arguments.js test hello
Output:
0: /opt/smallapps/phantomjs-1.9.7-linux-x86_64/examples/arguments.js
1: test
2: a
Grab this output from the command line and check it with phpunit assertions.
example.js contains:
var system = require('system');
if (system.args.length === 1) {
console.log('Try to pass some args when invoking this script!');
} else {
system.args.forEach(function (arg, i) {
console.log(i + ': ' + arg);
});
}
phantom.exit();
Unfortunately, all this will only work with the simplest javascript files - without any dependencies to frameworks such as jQuery, or real interactions with other javascript-files that need to be embedded in a real html page, e.g. for accessing the DOM.