Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Notifications for Mocha in browser #2962

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions browser-entry.js
Expand Up @@ -167,6 +167,40 @@ mocha.run = function (fn) {
});
};

/**
* Use HTML notifications instead of Growl
*/
Mocha.prototype._growl = mocha._growl = function (runner, reporter) {
runner.on('end', function () {
var stats = reporter.stats;
if (!('Notification' in window)) return;
var notification, title, msg;
if (stats.failures) {
title = 'Failed';
msg = stats.failures + ' of ' + runner.total + ' tests failed';
} else {
title = 'Passed';
msg = stats.passes + ' tests passed in ' + stats.duration + 'ms';
}
var options = { body: msg };
if (Notification.permission === 'granted') {
notification = new Notification(title, options);
} else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
if (permission === 'granted') {
notification = new Notification(title, options);
}
});
}
if (notification) {
notification.onclick = function () {
window.focus();
this.close();
};
}
});
};

/**
* Expose the process shim.
* https://github.com/mochajs/mocha/pull/916
Expand Down
26 changes: 26 additions & 0 deletions test/browser/notification.html
@@ -0,0 +1,26 @@
<html>
<head>
<title>Mocha</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../../mocha.css" />
<script src="../../mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script>
function assert(expr, msg) {
if (!expr) throw new Error(msg || 'failed');
}
</script>
<script src="notification.spec.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
(function() {
mocha.checkLeaks();
mocha.growl()
mocha.run();
})();
</script>
</body>
</html>
7 changes: 7 additions & 0 deletions test/browser/notification.spec.js
@@ -0,0 +1,7 @@
'use strict';

describe('Notification', function () {
it('should ask for notification, or show a notification', function () {
assert(true);
});
});