Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unit tests #13

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ Checkout and install the dependencies:

#### Run the tests

Start a web server in the project directory and navigate to http://localhost:3000/test/. If you makes changes to the library, be sure to run `gulp` to rebuild the library in `/dist`.
Run:
npm run test
// To debug
npm run tests:debug

### License

Expand Down
18 changes: 18 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = function(config) {
config.set({
frameworks: ['mocha', 'chai', 'sinon'],
preprocessors: {
'src/appmetrics.js': ['babel'],
'test/**/*.js': ['babel']
},
files: ['src/appmetrics.js', 'test/**/*.js'],
reporters: ['progress'],
port: 9876, // karma web server port
colors: true,
logLevel: config.LOG_INFO,
browsers: ['ChromeHeadless'],
autoWatch: false,
singleRun: true, // Karma captures browsers, runs the tests and exits
concurrency: Infinity
});
};
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
],
"scripts": {
"lint": "gulp lint",
"test": "./node_modules/mocha/bin/mocha",
"test": "karma start",
"test:debug": "karma start --browsers Chrome --single-run false",
"release": "npm version patch; gulp; npm publish"
},
"repository": {
Expand All @@ -30,7 +31,7 @@
"devDependencies": {
"babel-core": "^6.13.2",
"babel-preset-es2015": "^6.13.2",
"chai": "^3.5.0",
"chai": "^4.1.2",
"del": "^2.2.1",
"eslint-config-google": "^0.6.0",
"gulp": "^3.9.1",
Expand All @@ -41,7 +42,14 @@
"gulp-load-plugins": "^1.2.4",
"gulp-mocha": "^3.0.0",
"gulp-rename": "^1.2.2",
"mocha": "^3.0.1",
"run-sequence": "^1.2.2"
}
"karma": "^2.0.0",
"karma-babel-preprocessor": "^7.0.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.2.0",
"karma-mocha": "^1.3.0",
"karma-sinon": "^1.0.5",
"run-sequence": "^1.2.2",
"sinon": "^4.1.4"
},
"dependencies": {}
}
24 changes: 0 additions & 24 deletions test/index.html

This file was deleted.

80 changes: 18 additions & 62 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ describe('appmetrics.js', function() {

describe('init', function() {
it('constructor fails without name', function() {
assert.throws(function test() { return new Metric(); });
assert.throws(function test() {
return new Metric();
});
});
it('name is correct', function() {
assert.equal(metric.name, 'test_metric');
Expand Down Expand Up @@ -127,80 +129,34 @@ describe('appmetrics.js', function() {
});

describe('sendToAnalytics()', function() {
it('sends default request', function(done) {
const observer = new PerformanceObserver(list => {
observer.disconnect();

const entries = list.getEntries().filter(entry => {
return isAnalyticsRequest(entry) &&
entry.name.includes(Math.round(metric.duration)) &&
entry.name.includes(metric.name) &&
entry.name.includes('category_name');
});

assert.equal(entries.length, 1, 'single timing entry');
done();
});
observer.observe({entryTypes: ['resource']});

let spy;
beforeEach(() => {
spy = sinon.spy(window, 'ga');
});
afterEach(() => {
spy.restore();
});
it('sends default request', function() {
metric.sendToAnalytics('category_name');
assert(spy.calledWith('send', 'timing', 'category_name', metric.name, Math.round(metric.duration)));
});

it('can override duration and name', function(done) {
const observer = new PerformanceObserver(list => {
observer.disconnect();

const entries = list.getEntries().filter(entry => {
return isAnalyticsRequest(entry) &&
entry.name.includes('1234567890') &&
entry.name.includes('category_name') &&
entry.name.includes('metric_name');
});

assert.equal(entries.length, 1, 'one timing entry');
done();
});
observer.observe({entryTypes: ['resource']});

it('can override duration and name', function() {
metric.sendToAnalytics('category_name', 'metric_name', 1234567890);
assert(spy.calledWith('send', 'timing', 'category_name', 'metric_name', 1234567890));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are testing that the GA api call is made, not that the entries are properly recorded in the PerformanceObserver. Can we keep the tests as is? Think that means removing sinon....?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried leaving it as it was. However, when I first ran the tests they always failed. They were sending the wrong payload, metric.name was always google-analytics.com/analytics.js (or some variation of the GA script address).

The tests were previously more like integration tests and covered part of the internals of GA, which I'm not really sure is necessary. Since GA is a 'soft' peer dependency, as long as GA behaves then we shouldn't care except that we call the interface correctly.

});

it('can send a duration without measuring', function(done) {
it('can send a duration without measuring', function() {
const duration = Date.now();

const observer = new PerformanceObserver(list => {
observer.disconnect();

const entries = list.getEntries().filter(entry => {
return isAnalyticsRequest(entry) &&
entry.name.includes(duration) &&
entry.name.includes('category_name') &&
entry.name.includes('override_duration');
});

assert.equal(entries.length, 1, 'one timing entry');
done();
});
observer.observe({entryTypes: ['resource']});

const metric = new Metric('override_duration');
metric.sendToAnalytics('category_name', metric.name, duration);
assert(spy.calledWith('send', 'timing', 'category_name', metric.name, duration));
});

it('no requests are to GA before a measurement', function(done) {
// If the perf observer sees a request, the test should fail.
const observer = new PerformanceObserver(list => {
observer.disconnect();
assert.fail(
false, true, 'Google Analytics request was sent before a measurement was made.');
done();
});
observer.observe({entryTypes: ['resource']});

it('no requests are to GA before a measurement', function() {
const metric = new Metric('test_metric');
metric.sendToAnalytics('should_not_be_sent');

setTimeout(done, 500);
assert(spy.notCalled);
});
});
});
Loading