Skip to content

Commit 3fd1632

Browse files
author
Ryan X. Charles
committed
run tests in browser with karma
Summary: - Add karma and related dependencies to make running tests in the browser easy. Also update browserify, since the old version was not producing a working build. - Running "npm test" locally runs both the node tests and browser tests (in Chrome, since that's what most people already have installed). If you want to run the node tests only, run "npm run test-node". For browser tests only, "npm run test-browser". - Make travis only run the node tests, "npm run test-node", since although it is supposed to be able to run karma tests in Firefox, it had issues with that. - Update the Oauth and BitGo Express tests not to run in the browser. The Oauth tests fail because of cross-origin policy. The BitGo Express tests fail because BitGo Express is not intended to work in the browser (at this time). - You can also run the tests in the browser without karma by running "npm run compile-test" and opening up test/index.html in your browser. - Move the BitGoJS.js build file to the browser folder, next to the tests file tests.js, so we don't clutter the main directory (the browser directory only appears if you actually build these files). Reviewers: gavin Reviewed By: gavin Subscribers: benchan, ben Differential Revision: https://phabricator.bitgo.com/D890
1 parent 1d5d154 commit 3fd1632

File tree

7 files changed

+117
-23
lines changed

7 files changed

+117
-23
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ npm-debug.log
77
node_modules
88
coverage
99
*.swp
10-
BitGoJS-min.js
11-
BitGoJS.js
10+
browser
11+
browser/BitGoJS-min.js
12+
browser/BitGoJS.js
1213
example/sandbox.js
1314
bin/server.key
1415
bin/server.crt

.karma.conf.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Karma configuration
2+
// Generated on Tue Jan 06 2015 16:30:03 GMT-0800 (PST)
3+
4+
module.exports = function(config) {
5+
config.set({
6+
7+
// base path that will be used to resolve all patterns (eg. files, exclude)
8+
basePath: '',
9+
10+
11+
// frameworks to use
12+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
13+
frameworks: ['mocha'],
14+
15+
16+
// list of files / patterns to load in the browser
17+
files: [
18+
'browser/tests.js'
19+
],
20+
21+
client: {
22+
mocha: {
23+
timeout: '10000'
24+
}
25+
},
26+
27+
// list of files to exclude
28+
exclude: [
29+
],
30+
31+
32+
// preprocess matching files before serving them to the browser
33+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
34+
preprocessors: {
35+
},
36+
37+
38+
// test results reporter to use
39+
// possible values: 'dots', 'progress'
40+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
41+
reporters: ['progress'],
42+
43+
44+
// web server port
45+
port: 9876,
46+
47+
48+
// enable / disable colors in the output (reporters and logs)
49+
colors: true,
50+
51+
52+
// level of logging
53+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
54+
logLevel: config.LOG_INFO,
55+
56+
57+
// enable / disable watching file and executing tests whenever any file changes
58+
autoWatch: false,
59+
60+
61+
// start these browsers
62+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
63+
browsers: ['Chrome'],
64+
65+
66+
// Continuous Integration mode
67+
// if true, Karma captures browsers, runs the tests and exits
68+
singleRun: false
69+
});
70+
};

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ node_js:
55
- '0.10.37'
66
env:
77
matrix:
8-
- TEST_SUITE=unit
8+
- TEST_SUITE=test-node
99
global:
1010
secure: gbQ8VqPQWU19sPvCQy0YUdgrdOK/zPPS9oBm0DQrL06mue5sd+IXGOUH/hqgSGNJxR/CbNQt7dTC6Ex0mTXHUasFlrrQUKn1Hr4Esqqqog1A2PgnuXK7AmHuXI18Cva4f8wU1h+HVMLfW9MJ+r1WtwyIg+DAdxWwDKUbIvCtIto=
11-
script: npm run-script $TEST_SUITE
11+
script: npm run $TEST_SUITE

package.json

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
"url": "https://github.com/BitGo/BitGoJS.git"
1414
},
1515
"devDependencies": {
16-
"browserify": "~4.1.5",
16+
"karma": "^0.12.31",
17+
"karma-chrome-launcher": "^0.1.7",
18+
"karma-mocha": "^0.1.10",
19+
"browserify": "~9.0.7",
20+
"envify": "^3.4.0",
1721
"coveralls": "~2.10.0",
1822
"istanbul": "0.1.30",
1923
"mocha": "1.18.2",
@@ -41,25 +45,27 @@
4145
"scripts": {
4246
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --timeout 10000 --reporter list test/*.js test/bitcoin/*.js",
4347
"coveralls": "sleep 300; ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --timeout 50000 -R spec test/*.js test/bitcoin/*.js && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
44-
"compile": "./node_modules/.bin/browserify ./src/index.js -s Bitcoin | ./node_modules/.bin/uglifyjs > BitGoJS-min.js",
45-
"compile-dbg": "./node_modules/.bin/browserify ./src/index.js -s Bitcoin | ./node_modules/.bin/uglifyjs --beautify > BitGoJS.js",
46-
"test": "npm run-script unit",
47-
"unit": "./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha -- --timeout 10000 --reporter list test/*.js"
48+
"compile": "mkdir -p browser && ./node_modules/.bin/browserify ./src/index.js -s Bitcoin | ./node_modules/.bin/uglifyjs > browser/BitGoJS-min.js",
49+
"compile-dbg": "mkdir -p browser && ./node_modules/.bin/browserify ./src/index.js -s Bitcoin | ./node_modules/.bin/uglifyjs --beautify > browser/BitGoJS.js",
50+
"compile-test": "mkdir -p browser && ./node_modules/.bin/browserify -t envify ./test/*.js > ./browser/tests.js",
51+
"test": "npm run test-node && npm run test-browser",
52+
"test-node": "./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha -- --timeout 10000 --reporter list test/*.js",
53+
"test-browser": "npm run compile-test && ./node_modules/karma/bin/karma start .karma.conf.js --log-level debug --single-run"
4854
},
4955
"dependencies": {
56+
"argparse": "^0.1.16",
57+
"bigi": "1.4.0",
5058
"bitcoinjs-lib": "1.5.4",
59+
"body-parser": "^1.10.2",
5160
"bs58": "2.0.1",
5261
"bs58check": "1.0.4",
53-
"bigi": "1.4.0",
5462
"chain-node": "0.0.17",
55-
"minimist": "0.2.0",
56-
"superagent": "0.18.0",
57-
"q": "1.1.2",
58-
"lodash": "2.4.1",
59-
"argparse": "^0.1.16",
60-
"body-parser": "^1.10.2",
6163
"express": "^4.11.1",
6264
"http-proxy": "^1.8.1",
65+
"lodash": "2.4.1",
66+
"minimist": "0.2.0",
67+
"q": "1.1.2",
68+
"superagent": "0.18.0",
6369
"underscore.string": "2.4.0"
6470
}
6571
}

test/bitgo.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,10 @@ describe('BitGo', function() {
440440

441441
var refreshToken;
442442
describe('Oauth test', function() {
443+
if (process.browser) {
444+
// Oauth tests not supported in browser due to same-origin policy
445+
return;
446+
};
443447
var bitgo;
444448

445449
before(function (done) {

test/bitgoExpress.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
if (process.browser) {
2+
// Bitgo Express tests not supported in browser
3+
return;
4+
};
5+
16
var assert = require('assert');
27
var should = require('should');
38
var request = require('supertest');

test/index.html

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<title>BitGoJs Sample Page</title>
5-
<script type="text/javascript" src="../BitGoJS-min.js"></script>
6-
</head>
7-
<body>
8-
This page intentionally left blank.
9-
</body>
3+
<head>
4+
<title>Mocha</title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
8+
</head>
9+
<body>
10+
<div id="mocha"></div>
11+
<script src="../node_modules/mocha/mocha.js"></script>
12+
<script>mocha.setup('bdd')</script>
13+
<script src="../browser/tests.js"></script>
14+
<script>
15+
mocha.run();
16+
</script>
17+
</body>
1018
</html>

0 commit comments

Comments
 (0)