Skip to content

Commit 67a3d4b

Browse files
committed
using karma to run test
1 parent 4970d26 commit 67a3d4b

File tree

3 files changed

+123
-9
lines changed

3 files changed

+123
-9
lines changed

karma.conf.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Karma configuration
2+
// Generated on Mon Jun 13 2016 13:04:33 GMT+0200 (CEST)
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: ['browserify', 'mocha'],
14+
frameworks: ['mocha'],
15+
//'requirejs'
16+
17+
// list of files / patterns to load in the browser
18+
files: [
19+
'node_modules/mocha/mocha.js',
20+
'node_modules/chai/chai.js',
21+
'node_modules/should/should.js',
22+
'dist/MqttWorker.js',
23+
{pattern: 'mqttWorker.js', included: false},
24+
'dist/MqttBundle.js',
25+
{pattern: 'test/*.spec.js', included: true}
26+
],
27+
28+
29+
// list of files to exclude
30+
exclude: [],
31+
32+
33+
// preprocess matching files before serving them to the browser
34+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
35+
// 'lib/*.js': ['coverage'],
36+
preprocessors: {
37+
// source files for coverage
38+
'mqttWorker.js': ['coverage']
39+
},
40+
41+
// optionally, configure the reporter
42+
coverageReporter: {
43+
type : 'lcov',
44+
dir : 'coverage/'
45+
},
46+
47+
48+
// test results reporter to use
49+
// possible values: 'dots', 'progress'
50+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
51+
reporters: ['progress', 'coverage'],
52+
53+
54+
// web server port
55+
port: 9876,
56+
57+
58+
// enable / disable colors in the output (reporters and logs)
59+
colors: true,
60+
61+
62+
// level of logging
63+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
64+
logLevel: config.LOG_INFO,
65+
66+
67+
// enable / disable watching file and executing tests whenever any file changes
68+
autoWatch: true,
69+
70+
71+
// start these browsers
72+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
73+
browsers: ['Chrome'],
74+
75+
76+
// Continuous Integration mode
77+
// if true, Karma captures browsers, runs the tests and exits
78+
singleRun: false,
79+
80+
// Concurrency level
81+
// how many browser should be started simultaneous
82+
concurrency: Infinity
83+
})
84+
};

mqttWorker.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
importScripts('dist/MqttBundle.js');
4-
//importScripts('dist/MqttBundle.min.js');
54

65
// detect if we are a Worker or SharedWorker
76
var worker = typeof self.postMessage === 'function' || false;
@@ -25,14 +24,39 @@ var util = {
2524
// the proper URL is supplied via client.connect as the last argument
2625
var document = {};
2726

28-
var serviceWorker = {
27+
var mqttWorker = {
2928
// map for the clients eg. portN => clientN
3029
clients: {},
3130
// a scope wrapping function to generate different postMessages to the worker client
3231
generateMessageCallback: function (port, type) {
3332
return function () {
3433
// type is something like 'publish', 'subscribe', 'message' or 'callback'
35-
port.postMessage({type: type, args: arguments});
34+
// posting arguments can fail to du cloning the object. We might have to reduce the arguments we send back
35+
// to the browser
36+
37+
switch (arguments.length) {
38+
case 0:
39+
port.postMessage({type: type});
40+
break;
41+
case 1:
42+
port.postMessage({type: type, args: {0: arguments[0]}});
43+
break;
44+
case 2:
45+
port.postMessage({type: type, args: {0: arguments[0],1: arguments[1]}});
46+
break;
47+
case 3:
48+
//port.postMessage({type: type, args: {0: arguments[0],1: arguments[1]}});
49+
port.postMessage({type: type, args: {0: arguments[0],1: arguments[1],2: arguments[2]}});
50+
break;
51+
default:
52+
port.postMessage({type: type, args: {0: arguments[0]}});
53+
//port.postMessage({type: type, args: arguments});
54+
}
55+
56+
//if(type === 'close') {
57+
// port.postMessage({type: type});
58+
//} else {
59+
//}
3660
}
3761
},
3862

@@ -45,7 +69,6 @@ var serviceWorker = {
4569
createClient: function (args, port) {
4670
var generateMessageCallback = this.generateMessageCallback;
4771

48-
4972
function addCallbacks(mqtt) {
5073

5174
['close', 'connect', 'error', 'message', 'offline', 'reconnect']
@@ -61,7 +84,7 @@ var serviceWorker = {
6184

6285
/**
6386
* A callback method for the 'connect' event. Connect events are only fired when used with a
64-
* SharedServiceWorker.
87+
* SharedWebWorker.
6588
* @param {Object} event that triggered the connect
6689
*/
6790
connect: function (event) {
@@ -100,6 +123,7 @@ var serviceWorker = {
100123
} else {
101124
port = event.srcElement;
102125
}
126+
103127
var callback;
104128
var client = this.clients[port];
105129

@@ -110,8 +134,9 @@ var serviceWorker = {
110134
try {
111135
this.clients[port] = this.createClient.apply(this, [arr, port]);
112136
} catch (err) {
113-
console.error(err);
137+
//console.error(err);
114138
}
139+
// sending back the options set on the client
115140
port.postMessage({type: 'options', args: this.clients[port].options});
116141
break;
117142

@@ -155,5 +180,5 @@ if(worker) {
155180
event = 'message';
156181
}
157182
// binding to the 'message'/'connect' event at the serverWorker
158-
self.addEventListener(event, serviceWorker[event].bind(serviceWorker));
183+
self.addEventListener(event, mqttWorker[event].bind(mqttWorker));
159184

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "mqttWorker.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "karma start"
88
},
99
"author": "",
1010
"license": "MIT",
@@ -19,8 +19,13 @@
1919
"grunt-browserify": "^5.0.0",
2020
"grunt-contrib-uglify": "^1.0.1",
2121
"grunt-contrib-watch": "^1.0.0",
22+
"karma": "^0.13.22",
23+
"karma-browserify": "^5.0.5",
24+
"karma-chrome-launcher": "^1.0.1",
25+
"karma-coverage": "^1.0.0",
26+
"karma-mocha": "^1.0.1",
2227
"load-grunt-tasks": "^3.4.1",
23-
"mocha": "^2.4.5",
28+
"mocha": "^2.5.3",
2429
"should": "^8.2.2"
2530
}
2631
}

0 commit comments

Comments
 (0)