working on mocha tests with pahntomjs
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "jest --verbose --coverage && mocha --compilers js:mocha-traceur tests/**/*.spec.js",
|
"test": "jest --verbose --coverage && mocha --compilers js:mocha-traceur tests/**/*.spec.js",
|
||||||
"test-react": "jest --verbose --coverage",
|
"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",
|
"start": "npm install && bower install && gulp",
|
||||||
"lint": "jsxhint --jsx-only .",
|
"lint": "jsxhint --jsx-only .",
|
||||||
"postinstall": "gulp postinstall"
|
"postinstall": "gulp postinstall"
|
||||||
@@ -38,7 +38,7 @@
|
|||||||
"react-tools": "^0.13.3",
|
"react-tools": "^0.13.3",
|
||||||
"sinon": "^1.15.4",
|
"sinon": "^1.15.4",
|
||||||
"sinon-chai": "^2.8.0",
|
"sinon-chai": "^2.8.0",
|
||||||
"sinon-chrome": "^0.1.2"
|
"sinon-chrome": "^0.2.1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bootstrap": "^3.3.4",
|
"bootstrap": "^3.3.4",
|
||||||
|
|||||||
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">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Empty HTML Document</title>
|
<title></title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,38 @@
|
|||||||
var chai = require('chai');
|
// var chai = require('chai');
|
||||||
var chrome = require('sinon-chrome');
|
// var chrome = require('sinon-chrome');
|
||||||
var sinon = require('sinon-chai');
|
// var sinon = require('sinon-chai');
|
||||||
var expect = chai.expect;
|
// 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() {
|
describe('changeExtensionTooltip', function() {
|
||||||
it('should be a function', function() {
|
// sometimes it takes time to start phantomjs
|
||||||
expect(changeExtensionTooltip).to.be.a('function');
|
this.timeout(4000);
|
||||||
});
|
|
||||||
|
|
||||||
it('should change the extension tooltip', function() {
|
var FILENAME = 'tests/empty.html';
|
||||||
changeExtensionTooltip('WakaTime');
|
|
||||||
expect(chrome.browserAction.setTitle).toHaveBeenCalledWith({title: 'Wakatime'});
|
it('should exist', function(done) {
|
||||||
sinon.assert.calledWithMatch(chrome.browserAction.setTitle, {title: 'WakaTime'});
|
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