working on mocha tests with pahntomjs
This commit is contained in:
40
tests/beforeEach.js
Normal file
40
tests/beforeEach.js
Normal file
@@ -0,0 +1,40 @@
|
||||
var fs = require('fs');
|
||||
var page;
|
||||
var beforeLoadFn;
|
||||
|
||||
beforeEach(function() {
|
||||
page = require('webpage').create();
|
||||
|
||||
page.onConsoleMessage = function(msg) { console.log(msg); };
|
||||
|
||||
page.onError = function(msg, trace) {
|
||||
var msgStack = [msg];
|
||||
if (trace && trace.length) {
|
||||
msgStack.push('TRACE:');
|
||||
trace.forEach(function(t) {
|
||||
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
|
||||
});
|
||||
}
|
||||
// we need try..catch here as mocha throws error that catched by phantom.onError
|
||||
try {
|
||||
mocha.throwError(msgStack.join('\n'));
|
||||
} catch(e) { }
|
||||
};
|
||||
|
||||
page.onInitialized = function() {
|
||||
page.injectJs(node_modules + 'chai/chai.js');
|
||||
page.injectJs(node_modules + 'sinon/pkg/sinon.js');
|
||||
page.injectJs(node_modules + 'sinon-chrome/chrome.js');
|
||||
page.injectJs(node_modules + 'sinon-chrome/src/phantom-tweaks.js');
|
||||
page.injectJs(node_modules + 'require-stub/index.js');
|
||||
// call additional function defined in tests
|
||||
if (beforeLoadFn) {
|
||||
beforeLoadFn();
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
page.close();
|
||||
beforeLoadFn = null;
|
||||
});
|
||||
@@ -2,7 +2,7 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Empty HTML Document</title>
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
@@ -1,18 +1,38 @@
|
||||
var chai = require('chai');
|
||||
var chrome = require('sinon-chrome');
|
||||
var sinon = require('sinon-chai');
|
||||
var expect = chai.expect;
|
||||
// var chai = require('chai');
|
||||
// var chrome = require('sinon-chrome');
|
||||
// var sinon = require('sinon-chai');
|
||||
// var expect = chai.expect;
|
||||
|
||||
import changeExtensionTooltip from '../../assets/js/helpers/changeExtensionTooltip';
|
||||
// import changeExtensionTooltip from '../../assets/js/helpers/changeExtensionTooltip';
|
||||
|
||||
// describe('changeExtensionTooltip', function() {
|
||||
// it('should be a function', function() {
|
||||
// expect(changeExtensionTooltip).to.be.a('function');
|
||||
// });
|
||||
|
||||
// it('should change the extension tooltip', function() {
|
||||
// changeExtensionTooltip('WakaTime');
|
||||
// expect(chrome.browserAction.setTitle).toHaveBeenCalledWith({title: 'Wakatime'});
|
||||
// sinon.assert.calledWithMatch(chrome.browserAction.setTitle, {title: 'WakaTime'});
|
||||
// });
|
||||
// });
|
||||
|
||||
var babel
|
||||
|
||||
describe('changeExtensionTooltip', function() {
|
||||
it('should be a function', function() {
|
||||
expect(changeExtensionTooltip).to.be.a('function');
|
||||
});
|
||||
// sometimes it takes time to start phantomjs
|
||||
this.timeout(4000);
|
||||
|
||||
it('should change the extension tooltip', function() {
|
||||
changeExtensionTooltip('WakaTime');
|
||||
expect(chrome.browserAction.setTitle).toHaveBeenCalledWith({title: 'Wakatime'});
|
||||
sinon.assert.calledWithMatch(chrome.browserAction.setTitle, {title: 'WakaTime'});
|
||||
});
|
||||
var FILENAME = 'tests/empty.html';
|
||||
|
||||
it('should exist', function(done) {
|
||||
page.open(FILENAME, function() {
|
||||
page.injectJs('assets/js/helpers/changeExtensionTooltip.js');
|
||||
|
||||
page.evaluate(function() {
|
||||
console.log(typeof changeExtensionIcon);
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
24
tests/run.js
Normal file
24
tests/run.js
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Test Runner for Mocha tests
|
||||
* Using phantomjs to render page and execute scripts
|
||||
*/
|
||||
|
||||
var node_modules = '../node_modules/';
|
||||
phantom.injectJs(node_modules + 'mocha/mocha.js');
|
||||
phantom.injectJs(node_modules + 'sinon-chrome/src/phantom-tweaks.js');
|
||||
mocha.setup({ui: 'bdd', reporter: 'spec'});
|
||||
|
||||
// Setup
|
||||
phantom.injectJs('beforeeach.js');
|
||||
|
||||
// Tests
|
||||
phantom.injectJs('helpers/changeExtensionTooltip.spec.js');
|
||||
|
||||
// Execute
|
||||
mocha.run(function(failures) {
|
||||
// setTimeout is needed to supress "Unsafe JavaScript attempt to access..."
|
||||
// see https://github.com/ariya/phantomjs/issues/12697
|
||||
setTimeout(function() {
|
||||
phantom.exit(failures);
|
||||
}, 0);
|
||||
});
|
||||
Reference in New Issue
Block a user