Skip to content

Commit

Permalink
Merge pull request #451 from subtleGradient/subtlegradient/browser-te…
Browse files Browse the repository at this point in the history
…sting

Browser testing.
  • Loading branch information
benjamn committed Nov 7, 2013
2 parents ee8bb07 + 241d57a commit b3c87ea
Show file tree
Hide file tree
Showing 22 changed files with 562 additions and 277 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ docs/js/live_editor.js
docs/js/examples
docs/downloads
examples/shared/*.js

test/the-files-to-test.generated.js
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ env:
- secure: qPvsJ46XzGrdIuPA70b55xQNGF8jcK7N1LN5CCQYYocXLa+fBrl+fTE77QvehOPhqwJXcj6kOxI+sY0KrVwV7gmq2XY2HZGWUSCxTN0SZlNIzqPA80Y7G/yOjA4PUt8LKgP+8tptyhTAY56qf+hgW8BoLiKOdztYF2p+3zXOLuA=
# SECRET_TOKEN
- secure: dkpPW+VnoqC/okhRdV90m36NcyBFhcwEKL3bNFExAwi0dXnFao8RoFlvnwiPlA23h2faROkMIetXlti6Aju08BgUFV+f9aL6vLyU7gUent4Nd3413zf2fwDtXIWIETg6uLnOpSykGKgCAT/hY3Q2oPLqOoY0OxfgnbqwxkxljrE=
- secure: eHMg4yQWJ+5zFxNcjVwpdpcbZCQMaSByqJy0wwQ2XOOMItdCvSfO/fa+mYoVND/9Orsi5kvXC9tx4sYHyth5hLrvO+bghYjiayrbxYYrxso6G/aU8Pttc7s/qk3I4zadLlua1kGy2TauByxS6nt9nUDe0FDaJKA9k1QXVlpq5Ac=
- secure: v65jUPPNBvJg1xhj4cY7wMKinCESUseHsW1d2gp68SluFHHyjWZOvXYXZdABqB/1uNmQxjbUkwkdSNAOn5k7ppjRcRnlAH2uu1xDqXrGp6cW4MfNPchrAe+FcUMtse/3THGJj8SZ9IQWIl+BHv4XASQ7FwqCR8cwlOfhsqdfch8=
29 changes: 19 additions & 10 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var exec = require('child_process').exec;
var jsxTask = require('./grunt/tasks/jsx');
var browserifyTask = require('./grunt/tasks/browserify');
var populistTask = require('./grunt/tasks/populist');
var phantomTask = require('./grunt/tasks/phantom');
var webdriverPhantomJSTask = require('./grunt/tasks/webdriver-phantomjs');
var webdriverJasmineTasks = require('./grunt/tasks/webdriver-jasmine');
var npmTask = require('./grunt/tasks/npm');
var releaseTasks = require('./grunt/tasks/release');

Expand All @@ -16,7 +17,8 @@ module.exports = function(grunt) {
jsx: require('./grunt/config/jsx/jsx'),
browserify: require('./grunt/config/browserify'),
populist: require('./grunt/config/populist'),
phantom: require('./grunt/config/phantom'),
connect: require('./grunt/config/server')(grunt),
"webdriver-jasmine": require('./grunt/config/webdriver-jasmine.js'),
npm: require('./grunt/config/npm'),
clean: ['./build', './*.gem', './docs/_site', './examples/shared/*.js'],
jshint: require('./grunt/config/jshint'),
Expand All @@ -25,11 +27,12 @@ module.exports = function(grunt) {

grunt.config.set('compress', require('./grunt/config/compress'));

grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-compare-size');
grunt.loadNpmTasks('grunt-contrib-compress');
Object.keys(grunt.file.readJSON('package.json').devDependencies)
.filter(function(npmTaskName){ return npmTaskName.indexOf('grunt-') === 0;})
.forEach(function(npmTaskName){
grunt.loadNpmTasks(npmTaskName);
})
;

// Alias 'jshint' to 'lint' to better match the workflow we know
grunt.registerTask('lint', ['jshint']);
Expand All @@ -42,7 +45,7 @@ module.exports = function(grunt) {

grunt.registerMultiTask('populist', populistTask);

grunt.registerMultiTask('phantom', phantomTask);
grunt.registerMultiTask('webdriver-jasmine', webdriverJasmineTasks);

grunt.registerMultiTask('npm', npmTask);

Expand Down Expand Up @@ -70,11 +73,17 @@ module.exports = function(grunt) {
'jsx:jasmine',
'jsx:test',
'version-check',
'populist:jasmine',
'populist:test'
]);

grunt.registerTask('test', ['build:test', 'build:basic', 'phantom:run']);
grunt.registerTask('webdriver-phantomjs', webdriverPhantomJSTask);

grunt.registerTask('test:webdriver:phantomjs', [
'connect',
'webdriver-phantomjs',
'webdriver-jasmine:local'
]);
grunt.registerTask('test', ['build:test', 'build:basic', 'test:webdriver:phantomjs']);
grunt.registerTask('npm:test', ['build', 'npm:pack']);

// Optimized build task that does all of our builds. The subtasks will be run
Expand Down
11 changes: 0 additions & 11 deletions grunt/config/phantom.js

This file was deleted.

47 changes: 47 additions & 0 deletions grunt/config/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var pxlgif = Buffer('R0lGODlhAQABAIAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==', 'base64');

module.exports = function(grunt){

function printMiddleware(req, res, next) {
if (req._parsedUrl.pathname != '/print') return next();
if (req.query.message.indexOf('ok') === 0){
grunt.log.ok(req.query.message);
} else if (req.query.message.indexOf('not ok') === 0){
grunt.log.error(req.query.message);
} else {
grunt[req.query.type || 'log'].writeln('[%s][%s]', req.headers['user-agent'], Date.now(), req.query.message);
}
res.end(pxlgif);
}
function testResultLoggerMiddleware(req, res, next) {
if (!(req.body && req.body.data)) return next();
grunt.log.writeln('[%s][%s]', req.headers['user-agent'], Date.now(), req.body.data);
res.end('Got it, thanks!');
}

return {
server: {
options: {
base: '.',
hostname: '*',
port: 9999,
middleware: function(connect, options) {
connect.logger.token('user-agent', function(req, res){ return req.headers['user-agent']; });
connect.logger.token('timestamp', function(req, res){ return Date.now(); });

return [
connect.query(),
printMiddleware,

connect.logger({format:'[:user-agent][:timestamp] :method :url', stream:grunt.verbose}),
connect.bodyParser(),
testResultLoggerMiddleware,

connect.static(options.base),
connect.directory(options.base)
];
},
}
}
}
}
18 changes: 18 additions & 0 deletions grunt/config/webdriver-jasmine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var grunt = require('grunt');


exports.local = {
webdriver: {
remote: { protocol: 'http:', hostname: '127.0.0.1', port: 9515, path: '/' }
},
url: "http://127.0.0.1:9999/test/index.html",
onComplete: function(report){
var browser = this;
if (!report.passed){
grunt.fatal("tests failed");
}
},
onError: function(error){
grunt.fatal(error);
}
}
68 changes: 0 additions & 68 deletions grunt/tasks/phantom.js

This file was deleted.

13 changes: 12 additions & 1 deletion grunt/tasks/populist.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
'use strict';

var grunt = require('grunt');
var fs = require('fs')

module.exports = function() {
var config = this.data;
var done = this.async();

var theFilesToTestScript = fs.createWriteStream(__dirname + '/../../test/the-files-to-test.generated.js');
theFilesToTestScript.write('// Generated by ');
theFilesToTestScript.write(JSON.stringify(__filename.split(/(?=grunt)/)[1]));
theFilesToTestScript.write(' at ');
theFilesToTestScript.write(JSON.stringify(new Date()));
theFilesToTestScript.write('\n\n');

// create the bundle we'll work with
var args = config.args;

Expand All @@ -15,14 +23,17 @@ module.exports = function() {
nonull: true, // Keep IDs that don't expand to anything.
cwd: config.rootDirectory
}, requires).forEach(function(name) {
args.push(name.replace(/\.js$/i, ""));
name = name.replace(/\.js$/i, "");
args.push(name);
theFilesToTestScript.write('harness.enableTest(' + JSON.stringify(name) + ');\n');
});

require("populist").buildP({
rootDirectory: config.rootDirectory,
args: args
}).then(function(output) {
grunt.file.write(config.outfile, output);
theFilesToTestScript.end();
done();
});
};
44 changes: 44 additions & 0 deletions grunt/tasks/webdriver-jasmine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var grunt = require("grunt");
var wd = require('wd');

module.exports = function(){
var config = this.data;
var taskSucceeded = this.async();
grunt.verbose.write('webdriver remote', JSON.stringify(config.webdriver.remote));
var browser = wd.promiseChainRemote(config.webdriver.remote);

browser.on('status', function(info) {
grunt.verbose.writeln(info);
});

browser.on('command', function(meth, path, data) {
grunt.verbose.writeln(' > ' + meth, path, data || '');
});

browser
.init(config.browser || {})
.get(config.url)
.then(function(){return browser;})
.then(getJSReport)
.then(config.onComplete && config.onComplete.bind(browser), config.onError && config.onError.bind(browser))
.fail(grunt.verbose.writeln.bind(grunt.verbose))
.fin(function(){
if (grunt.option('webdriver-keep-open')) return;
grunt.verbose.writeln('Closing the browser window. To keep it open, pass the --webdriver-keep-open flag to grunt.');
return browser.quit();
})
.done(
taskSucceeded.bind(null,true),
taskSucceeded.bind(null,false)
)
;
}

function getJSReport(browser){
return browser
.waitForCondition("typeof window.jasmine != 'undefined'", 500)
.waitForCondition("typeof window.jasmine.getJSReport != 'undefined'", 10e3)
.waitForCondition("window.testImageURL.running <= 0", 5e3)
.eval("jasmine.getJSReport()")
;
}
37 changes: 37 additions & 0 deletions grunt/tasks/webdriver-phantomjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var grunt = require('grunt');

module.exports = function(){
var onReadyCallback = this.async();

var phantomjs = require("phantomjs").path;
var child_process = require('child_process');
var config = this.data || {};

var args = ["--webdriver=" + (config.port || 9515)];
grunt.verbose.writeln('phantomjs START path:%s args:%s', phantomjs, args);

var child = child_process.spawn(phantomjs, args);
process.on('exit', function() {
child.kill();
});

child.on('error', function(error) {
grunt.verbose.writeln('phantomjs ERROR');
grunt.fatal(error);
});
child.on('exit', function(code) {
grunt.verbose.writeln('phantomjs END');
if (code) grunt.fatal('phantomjs FAIL');
});

function verboseWrite(chunk) {
if (onReadyCallback && chunk.toString().indexOf('running on port') != -1) {
grunt.verbose.writeln('phantomjs STARTED');
onReadyCallback();
onReadyCallback = null;
}
grunt.verbose.write(chunk);
}
child.stdout.on('data', verboseWrite);
child.stderr.on('data', verboseWrite);
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@
"grunt-contrib-copy": "~0.4.1",
"grunt-contrib-jshint": "~0.6.0",
"optimist": "~0.6.0",
"phantomjs": "~1.9.1-4",
"phantomjs": "~1.9",
"recast": "~0.4.16",
"semver": "~2.1.0",
"uglify-js": "~2.4.0",
"grunt-contrib-clean": "~0.5.0",
"grunt-compare-size": "~0.4.0",
"gzip-js": "~0.3.2",
"tmp": "~0.0.18",
"grunt-contrib-compress": "~0.5.1"
"grunt-contrib-compress": "~0.5.1",
"jasmine-tapreporter": "~0.2.2",
"grunt-contrib-connect": "~0.5.0",
"es5-shim": "~2.1.0",
"wd": "~0.2.2"
},
"engines": {
"node": ">=0.10.0"
Expand Down
4 changes: 2 additions & 2 deletions src/environment/__tests__/ReactWebWorker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
"use strict";

describe('ReactWebWorker', function() {
it('can run React in a web worker', function() {
;(typeof Worker == 'undefined' ? xit : it)('can run React in a web worker', function() {
var done = false;
var error;

var worker = new Worker('/worker.js');
var worker = new Worker(window.ReactWebWorker_URL || '/src/test/worker.js?_=' + Date.now().toString(36));
worker.addEventListener('message', function(e) {
var data = JSON.parse(e.data);
if (data.type == 'error') {
Expand Down
2 changes: 1 addition & 1 deletion src/test/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var global = {};
importScripts("phantomjs-shims.js");

try {
importScripts("react.js");
importScripts("../../build/react.js");
} catch (e) {
postMessage(JSON.stringify({
type: 'error',
Expand Down
Loading

0 comments on commit b3c87ea

Please sign in to comment.