Skip to content
Permalink
Browse files
CB-9429 Enables jsdom/browser tests for browserify.
This closes #123.
  • Loading branch information
Vladimir Kotikov committed Jul 31, 2015
1 parent 64e371e commit 2c29e187e4206a6a77fba940ef6f77aef5c7eb8c
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 99 deletions.
@@ -40,7 +40,7 @@ module.exports = function(grunt) {
"blackberry10": {},
"ios": {},
"osx": {},
//"test": {},
"test": {},
"windows": { useWindowsLineEndings: true },
"wp8": { useWindowsLineEndings: true },
"firefoxos": {},
@@ -67,7 +67,9 @@ module.exports = function(grunt) {
// defaults
grunt.registerTask('default', ['build', 'test']);
grunt.registerTask('build', ['compile', 'jshint', 'whitespace-check']);
grunt.registerTask('test', ['compile', 'jshint', '_test']);
grunt.registerTask('btest', ['compile', '_btest']);
grunt.registerTask('test', ['compile:test', 'jshint', '_test']);
grunt.registerTask('btest', ['compile:test', 'jshint', '_btest']);
grunt.registerTask('cover', ['compile', '_cover']);
grunt.registerTask('test-browserify', ['compile-browserify:test', 'jshint', '_test:skip-tests']);
grunt.registerTask('btest-browserify', ['compile-browserify:test', 'jshint', '_btest:skip-tests']);
};
@@ -62,13 +62,15 @@
"devDependencies": {
"connect": "1.8.5",
"grunt": "0.4.1",
"grunt-cli": "0.1.13",
"grunt-contrib-clean": "0.6.0",
"grunt-contrib-jshint": "0.10.0",
"istanbul": "^0.3.4",
"jasmine-node": "1.14.5",
"node-jsdom": "~3.x",
"mkdirp": "^0.5.0",
"grunt-cli": "0.1.13"
"node-jsdom": "~3.x",
"open": "0.0.5",
"through2": "^2.0.0"
},
"dependencies": {
"browserify": "10.1.3",
File renamed without changes.
@@ -22,7 +22,7 @@
module.exports = {
id: 'test platform',
bootstrap: function() {
var propertyreplacer = require('cordova/propertyreplacer');
var propertyreplacer = require('cordova/test/propertyreplacer');

require('cordova/builder').replaceHookForTesting = function(obj, key) {
// This doesn't clean up non-clobbering assignments, nor does it work for
File renamed without changes.
@@ -21,7 +21,7 @@

/*global spyOn:false */

var propertyreplacer = require('cordova/propertyreplacer');
var propertyreplacer = require('cordova/test/propertyreplacer');

exports.replace = function(moduleName, newValue) {
propertyreplacer.stub(define.moduleMap, moduleName, null);
File renamed without changes.
File renamed without changes.
@@ -20,8 +20,10 @@ var fs = require('fs');
var path = require('path');
var browserify = require('browserify');
var require_tr = require('./require-tr');
var root = path.join(__dirname, '..', '..')

var root = path.join(__dirname, '..', '..');
var pkgJson = require('../../package.json');
var collectFiles = require('./collect-files');
var copyProps = require('./copy-props');

module.exports = function bundle(platform, debug, commitId, platformVersion) {
require_tr.platform = platform;
@@ -43,6 +45,26 @@ module.exports = function bundle(platform, debug, commitId, platformVersion) {
b.add(path.join(root, 'src', 'legacy-exec', platform, 'platform.js'));
}

if (platform === 'test') {
// Add tests to bundle
// TODO: Also need to include android/ios tests
fs.readdirSync('test').forEach(function (item) {
var itemPath = path.resolve('test', item);
if (fs.statSync(itemPath).isFile()) b.add(itemPath);
});

// Add rest of modules from cordova-js-src/legacy-exec directory
// TODO: this probably should be done for all platforms?
fs.readdirSync(path.join(root, 'src', 'legacy-exec', platform, platform)).forEach(function (item) {
var itemPath = path.resolve(root, 'src', 'legacy-exec', platform, platform, item);
if (fs.statSync(itemPath).isFile()) b.add(itemPath);
});

// Ignore fake modules from tests, otherwise browserify fails to generate bundle
['your mom', 'dino', 'a', 'ModuleA', 'ModuleB', 'ModuleC']
.forEach(b.ignore.bind(b));
}

b.add(path.join(root, 'src', 'scripts', 'bootstrap.js'));

return b;
@@ -35,14 +35,9 @@ module.exports = function bundle(platform, debug, commitId, platformVersion, pla
if(fs.existsSync(platformPath) && fs.existsSync(path.join(platformPath, 'cordova-js-src'))) {
copyProps(modules, collectFiles(path.join(platformPath, 'cordova-js-src')));
} else {
if(platform !== 'test') {
//for platforms that don't have a release with cordova-js-src yet
copyProps(modules, collectFiles(path.join('src', 'legacy-exec', platform)));
} else {
//platform === test
copyProps(modules, collectFiles(path.join('src', platform)));
}

// for platforms that don't have a release with cordova-js-src yet
// or if platform === test
copyProps(modules, collectFiles(path.join('src', 'legacy-exec', platform)));
}
//test doesn't support custom paths
if (platform === 'test') {
@@ -68,7 +63,7 @@ module.exports = function bundle(platform, debug, commitId, platformVersion, pla
}

var output = [];

output.push("// Platform: " + platform);
output.push("// " + commitId);

@@ -82,16 +77,16 @@ module.exports = function bundle(platform, debug, commitId, platformVersion, pla
if (!scripts['require']) {
throw new Error("didn't find a script for 'require'")
}

writeScript(output, scripts['require'], debug)

// write modules
var moduleIds = Object.keys(modules)
moduleIds.sort()

for (var i=0; i<moduleIds.length; i++) {
var moduleId = moduleIds[i]

writeModule(output, modules[moduleId], moduleId, debug)
}

@@ -101,9 +96,9 @@ module.exports = function bundle(platform, debug, commitId, platformVersion, pla
if (!scripts['bootstrap']) {
throw new Error("didn't find a script for 'bootstrap'")
}

writeScript(output, scripts['bootstrap'], debug)

var bootstrapPlatform = 'bootstrap-' + platform
if (scripts[bootstrapPlatform]) {
writeScript(output, scripts[bootstrapPlatform], debug)
@@ -19,6 +19,8 @@
var fs = require('fs');
var path = require('path');
var util = require('util');
var through = require('through2');
var requireTr = require('./require-tr');
var bundle = require('./bundle-browserify');
var computeCommitId = require('./compute-commit-id');
var writeLicenseHeader = require('./write-license-header');
@@ -43,7 +45,20 @@ module.exports = function generate(platform, useWindowsLineEndings, platformVers

outReleaseFile = path.join('pkg', 'cordova.' + platform + '.js');
outReleaseFileStream = fs.createWriteStream(outReleaseFile);


var symbolList = null;
var addSymbolList = through.obj(function(row, enc, next) {
if(symbolList === null) {
symbolList = requireTr.getModules(platform);
this.push(util.format('var symbolList = %s;\n%s\n', JSON.stringify(symbolList, null, 4), row));
} else {
this.push(row);
}
next();
});

libraryRelease.pipeline.get('wrap').push(addSymbolList);

// write license header
writeLicenseHeader(outReleaseFileStream, platform, commitId, platformVersion);

@@ -149,7 +149,10 @@ function _updateRequires(code, platform) {
module.indexOf("cordova") === 0 && module.indexOf("cordova-") !== 0) {

var scriptPath;
var cordovajssrc = path.join(process.cwd(), "platforms", platform, "platform_www", "cordova-js-src")
var cordovajssrc = path.join(process.cwd(), "platforms", platform, "platform_www", "cordova-js-src");
// Amazon uses "android" directory
var platformName = platform === "amazon-fireos" ? "android" : platform;
var platformModuleRe = new RegExp('cordova\/(' + platformName + ')\/(.+)');

// require('cordova') -> cordova.js
if(module === "cordova") {
@@ -158,17 +161,19 @@ function _updateRequires(code, platform) {
} else if(module.match(/cordova\/init/)) {
scriptPath = node.args[0].value = module.replace(/cordova\/init/,
path.join(root, "src", "common", "init_b"));
// android and amazon-fireos have some special require's
} else if(module.match(/cordova\/(android|amazon-fireos)\/(.+)/)) {
// handle platform's special special requires
} else if(module.match(platformModuleRe)) {
if(fs.existsSync(cordovajssrc)) {
//cordova cli project with cordova-js-src in platform
scriptPath = node.args[0].value = module.replace(/cordova\/(android|amazon-fireos)\/(.+)/, path.join(cordovajssrc, "android", "$2"));
scriptPath = node.args[0].value = module.replace(platformModuleRe,
path.join(cordovajssrc, platformName, "$2"));
} else {
//non cli or no cordova-js-src directory
scriptPath = node.args[0].value = module.replace(/cordova\/(android|amazon-fireos)\/(.+)/, path.join(root, "src", "legacy-exec", "$1", "android", "$2"));
scriptPath = node.args[0].value = module.replace(platformModuleRe,
path.join(root, "src", "legacy-exec", "$1", platformName, "$2"));
}
// require('cordova/exec') and require('cordova/platform') -> platform's exec/platform
} else if(module.match(/cordova\/(platform|exec)$/)) {
} else if(module.match(/cordova\/(platform|exec)$/)) {
if(fs.existsSync(cordovajssrc)) {
//cordova cli project with cordova-js-src in platform
scriptPath = node.args[0].value = module.replace(/cordova\/(platform|exec)/, path.join(cordovajssrc, "$1"));
@@ -23,15 +23,20 @@
var fs = require('fs');
var path = require('path');
var connect = require('connect');
var exec = require('child_process').exec;
var bundle = require('./bundle');
var collect = require('./collect');
var start = require('open');

var testLibName = path.join(__dirname, '..', '..', 'pkg', 'cordova.test.js');
var testLib = fs.readFileSync(testLibName, 'utf8');

var pathToTemplate = path.join(__dirname, '..', 'templates', 'suite.html');
var pathToVendor = path.join(__dirname, '..', 'vendor');
var pathToJasmine = path.join(__dirname, '..', '..', 'node_modules', 'jasmine-node', 'lib', 'jasmine-node');
var pathToTests = path.join(__dirname, '..', '..', 'test');

var SKIP_TESTS = false;

var template = fs.readFileSync(pathToTemplate, "utf-8");

// middlewar for GET '/cordova.test.js'
@@ -40,7 +45,7 @@ function cordovajs(req, res) {
"Cache-Control": "no-cache",
"Content-Type": "text/javascript"
});
res.end(bundle('test'));
res.end(testLib);
}

// middleware for GET '/'
@@ -50,39 +55,45 @@ function root(req, res) {
"Content-Type": "text/html"
});

//FIXME in place collect thing is atrocious
//create the script tags to include
var tests = [];
collect(path.join(__dirname, '..', '..', 'test'), tests);
var specs = tests.map(function (file, path) {
return '<script src="' + file.replace(/^.*\/test\//, "/") +
'" type="text/javascript" charset="utf-8"></script>';
}).join('');

//inject in the test script includes and write the document
res.end(template.replace(/<!-- ##TESTS## -->/g, specs));
// When we testing browserify bundle, we don't need to include
// tests since they're already bundled with cordova.
if (!SKIP_TESTS) {
//FIXME in place collect thing is atrocious
//create the script tags to include
var tests = [];
collect(path.join(__dirname, '..', '..', 'test'), tests);
var specs = tests.map(function (file, path) {
return '<script src="' + file.replace(/\\/g, '/').replace(/^.*\/test\//, "/") +
'" type="text/javascript" charset="utf-8"></script>';
}).join('\n');

template = template.replace(/<!-- ##TESTS## -->/g, specs);
}

// write the document
res.end(template);
}

// connect router defn
function routes(app) {
app.get('/cordova.test.js', cordovajs);
app.get('/', root)
app.get('/', root);
}

module.exports = function() {

console.log('starting browser-based tests')
module.exports = function(skipTests) {
SKIP_TESTS = skipTests;
console.log('starting browser-based tests');

var vendor = connect.static(pathToVendor);
var jasmine = connect.static(pathToJasmine);
var tests = connect.static(pathToTests);
var router = connect.router(routes);

connect(vendor, jasmine, tests, router).listen(3000);

console.log("Test Server running on:\n");
console.log("http://127.0.0.1:3000\n");

exec('open http://127.0.0.1:3000');
start('http://127.0.0.1:3000');
};

@@ -30,7 +30,7 @@ var jsdom = require("node-jsdom").jsdom;
var document = jsdom(undefined, { url: 'file:///jsdomtest.info/a?b#c' });
var window = document.parentWindow;

module.exports = function(callback) {
module.exports = function(callback, skipTests) {

console.log('starting node-based tests');

@@ -54,12 +54,16 @@ module.exports = function(callback) {
// hijack require
require = window.cordova.require;
define = window.cordova.define;
// Set up dummy navigator object
navigator = window.navigator || {};

// load in our tests
var tests = [];
collect(path.join(__dirname, '..', '..', 'test'), tests);
for (var x in tests) {
eval(fs.readFileSync(tests[x], "utf-8"));
if (!skipTests) {
// load in our tests
var tests = [];
collect(path.join(__dirname, '..', '..', 'test'), tests);
for (var x in tests) {
eval(fs.readFileSync(tests[x], "utf-8"));
}
}

var env = jasmine.getEnv();
@@ -43,10 +43,6 @@

window.global = window;

window.cordova = {
platform: ""
};

window.addEventListener("load", function () {
jasmineEnv.execute();
});
@@ -62,7 +58,7 @@
</script>

<!-- ##TESTS## -->

<!-- Note: if you want to run individual tests, remove the tests placeholder above and include the individual tests yourself -->
<!-- <script src="test.accelerometer.js" type="text/javascript" charset="utf-8"></script> -->
</head>
@@ -26,11 +26,11 @@ try {
module.exports = function(grunt) {
grunt.registerTask('_test', 'Runs test in node', function() {
var done = this.async();
require('./lib/test-jsdom')(done);
require('./lib/test-jsdom')(done, arguments[0] === ('skip-tests'));
});

grunt.registerTask('_btest', 'Runs tests in the browser', function() {
require('./lib/test-browser')();
require('./lib/test-browser')(arguments[0] === ('skip-tests'));
this.async(); // never finish.
});
};

0 comments on commit 2c29e18

Please sign in to comment.