working on mocha tests with pahntomjs

This commit is contained in:
Stephen Rodriguez
2015-07-19 01:38:58 -04:00
parent de94353e6c
commit 07e8ae507a
5 changed files with 100 additions and 16 deletions

View File

@@ -2,7 +2,7 @@
"scripts": {
"test": "jest --verbose --coverage && mocha --compilers js:mocha-traceur tests/**/*.spec.js",
"test-react": "jest --verbose --coverage",
"test-js": "mocha --compilers js:mocha-traceur tests/**/*.spec.js",
"test-js": "node_modules/.bin/phantomjs tests/run.js",
"start": "npm install && bower install && gulp",
"lint": "jsxhint --jsx-only .",
"postinstall": "gulp postinstall"
@@ -38,7 +38,7 @@
"react-tools": "^0.13.3",
"sinon": "^1.15.4",
"sinon-chai": "^2.8.0",
"sinon-chrome": "^0.1.2"
"sinon-chrome": "^0.2.1"
},
"dependencies": {
"bootstrap": "^3.3.4",

40
tests/beforeEach.js Normal file
View 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;
});

View File

@@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Empty HTML Document</title>
<title></title>
</head>
<body>

View File

@@ -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
View 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);
});