Skip to content

Commit

Permalink
Add new event-based system for sandbox communication
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Chen authored and steveoh committed May 22, 2019
1 parent 137f621 commit 9371fdb
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 25 deletions.
12 changes: 12 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ module.exports = function(grunt) {
}
}
},
customTemplateProcessor: {
src: 'test/fixtures/pivotal/src/**/*.js',
options: {
specs: 'test/fixtures/pivotal/spec/*Spec.js',
helpers: 'test/fixtures/pivotal/spec/*Helper.js',
template: require('./test/fixtures/customTemplateProcessor/template.js'),
junit: {
path: 'junit/customTemplate',
consolidate: true
}
}
},
customTempDir: {
src: 'test/fixtures/custom-temp-dir/src/**/*.js',
options: {
Expand Down
42 changes: 25 additions & 17 deletions tasks/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
module.exports = function(grunt) {

// node api
var fs = require('fs'),
path = require('path');
const fs = require('fs'),
path = require('path'),
events = require('events');

// npm lib
var puppeteer = require('puppeteer'),
const puppeteer = require('puppeteer'),
chalk = require('chalk'),
_ = require('lodash');

// local lib
var jasmine = require('./lib/jasmine').init(grunt);
const jasmine = require('./lib/jasmine').init(grunt);

var junitTemplate = path.join(__dirname, '/jasmine/templates/JUnit.tmpl');
const junitTemplate = path.join(__dirname, '/jasmine/templates/JUnit.tmpl');

var status = {};

Expand Down Expand Up @@ -93,14 +94,17 @@ module.exports = function(grunt) {
summary: false
});

// Setup a fresh event dispatcher to catch page events
var dispatcher = new events.EventEmitter();

if (grunt.option('debug')) {
grunt.log.debug(options);
}

var done = this.async();

// The filter returned no spec files so skip headless.
if (!(await jasmine.buildSpecrunner(this.filesSrc, options))) {
if (!(await jasmine.buildSpecrunner(this.filesSrc, options, dispatcher))) {
done(false);
return;
}
Expand All @@ -111,7 +115,7 @@ module.exports = function(grunt) {
return;
}

const err = await launchPuppeteer(options);
const err = await launchPuppeteer(options, dispatcher);
var success = !err && status.failed === 0;

if (err) {
Expand All @@ -128,7 +132,7 @@ module.exports = function(grunt) {
});
});

async function launchPuppeteer(options) {
async function launchPuppeteer(options, dispatcher) {
var file = options.outfile;

if (options.host) {
Expand All @@ -151,7 +155,7 @@ module.exports = function(grunt) {
const page = await browser.newPage();

try {
await setup(options, page);
await setup(options, dispatcher, page);
await page.goto(file, { waitUntil: 'domcontentloaded' });

await jasminePromise;
Expand All @@ -178,7 +182,7 @@ module.exports = function(grunt) {
}
}

async function setup(options, page) {
async function setup(options, dispatcher, page) {
var indentLevel = 1,
tabstop = 2,
thisRun = {},
Expand Down Expand Up @@ -206,7 +210,11 @@ module.exports = function(grunt) {
}
});

await page.exposeFunction('jasmine.jasmineStarted', function() {
await page.exposeFunction('sendMessage', function () {
dispatcher.emit.apply(dispatcher, arguments);
});

dispatcher.on('jasmine.jasmineStarted', function() {
grunt.verbose.writeln('Jasmine Runner Starting...');
thisRun.startTime = (new Date()).getTime();
thisRun.executedSpecs = 0;
Expand All @@ -216,7 +224,7 @@ module.exports = function(grunt) {
thisRun.summary = [];
});

await page.exposeFunction('jasmine.suiteStarted', function suiteStarted(suiteMetadata) {
dispatcher.on('jasmine.suiteStarted', function suiteStarted(suiteMetadata) {
grunt.verbose.writeln('jasmine.suiteStarted');
currentSuite = suiteMetadata.id;
suites[currentSuite] = {
Expand All @@ -233,7 +241,7 @@ module.exports = function(grunt) {
}
});

await page.exposeFunction('jasmine.specStarted', function(specMetaData) {
dispatcher.on('jasmine.specStarted', function(specMetaData) {
grunt.verbose.writeln('jasmine.specStarted');
thisRun.executedSpecs++;
thisRun.cleanConsole = true;
Expand All @@ -244,7 +252,7 @@ module.exports = function(grunt) {
}
});

await page.exposeFunction('jasmine.specDone', function(specMetaData) {
dispatcher.on('jasmine.specDone', function(specMetaData) {
grunt.verbose.writeln('jasmine.specDone');
var specSummary = {
assertions: 0,
Expand Down Expand Up @@ -331,7 +339,7 @@ module.exports = function(grunt) {

});

await page.exposeFunction('jasmine.suiteDone', function suiteDone(suiteMetadata) {
dispatcher.on('jasmine.suiteDone', function suiteDone(suiteMetadata) {
grunt.verbose.writeln('jasmine.suiteDone');
suites[suiteMetadata.id].time = suiteMetadata.duration / 1000;

Expand All @@ -340,7 +348,7 @@ module.exports = function(grunt) {
}
});

await page.exposeFunction('jasmine.jasmineDone', function() {
dispatcher.on('jasmine.jasmineDone', function() {
grunt.verbose.writeln('jasmine.jasmineDone');
var dur = (new Date()).getTime() - thisRun.startTime;
var specQuantity = thisRun.executedSpecs + (thisRun.executedSpecs === 1 ? ' spec ' : ' specs ');
Expand Down Expand Up @@ -372,7 +380,7 @@ module.exports = function(grunt) {
resolveJasmine();
});

await page.exposeFunction('jasmine.done_fail', function(url) {
dispatcher.on('jasmine.done_fail', function(url) {
grunt.log.error();
grunt.warn('Unable to load "' + url + '" URI.', 90);

Expand Down
4 changes: 1 addition & 3 deletions tasks/jasmine/reporters/PuppeteerReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@


(function() {
const sendMessage = (type, param) => {
window[type] && window[type](param);
};
const sendMessage = window.sendMessage;

const reporter = {
jasmineStarted: function() {
Expand Down
9 changes: 5 additions & 4 deletions tasks/lib/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

exports.init = function(grunt) {
// node api
var fs = require('fs'),
const fs = require('fs'),
path = require('path');

// npm
var rimraf = require('rimraf'),
const rimraf = require('rimraf'),
_ = require('lodash'),
pacote = require('pacote');

Expand Down Expand Up @@ -34,7 +34,7 @@ exports.init = function(grunt) {
});
};

exports.buildSpecrunner = async function(src, options) {
exports.buildSpecrunner = async function(src, options, dispatcher) {
var source = '',
tempDir = options.tempDir,
outfile = options.outfile,
Expand Down Expand Up @@ -128,7 +128,8 @@ exports.init = function(grunt) {
if (options.template.process) {
var task = {
writeTempFile: exports.writeTempFile,
copyTempFile: exports.copyTempFile
copyTempFile: exports.copyTempFile,
eventDispatcher: dispatcher
};
source = options.template.process(grunt, task, context);
grunt.file.write(specrunner, source);
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/customTemplateProcessor/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var template = __dirname + '/template.tmpl';

exports.process = function(grunt, task, context) {
var source = grunt.file.read(template);
task.eventDispatcher.on('template.notify', function (message) {
console.log(message);
});
return grunt.util._.template(source)(context);
};
28 changes: 28 additions & 0 deletions test/fixtures/customTemplateProcessor/template.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jasmine Spec Runner</title>

<% css.forEach(function(style){ %>
<link rel="stylesheet" type="text/css" href="<%= style %>">
<% }) %>

<% with (scripts) { %>
<% [].concat(polyfills, jasmine, boot, vendor, helpers, src, specs, reporters).forEach(function(script){ %>
<script type="text/javascript" src="<%= script %>"></script>
<% }) %>
<% }; %>

<script type="text/javascript">
(function () {
window.sendMessage('template.notify', {
'myMessage': 'Successful message from template!'
});
})();
</script>
</head>

<body>
</body>
</html>
2 changes: 1 addition & 1 deletion test/jasmine_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var grunt = require('grunt'),
const grunt = require('grunt'),
jasmine = require('../tasks/lib/jasmine.js').init(grunt),
_ = require('lodash');

Expand Down

0 comments on commit 9371fdb

Please sign in to comment.