Skip to content

Commit

Permalink
Made a start on writing some tests. They're currently failing though …
Browse files Browse the repository at this point in the history
…– testing for an expection when submitting with an API key – not sure what I should be checking for here...
  • Loading branch information
Oliver Farrell committed May 25, 2015
1 parent 4294f23 commit c0854be
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"webpagetest": "~0.3.3"
},
"devDependencies": {
"jshint": "^2.7.0"
"jshint": "^2.7.0",
"chai": "^2.3.0",
"mocha": "^2.2.5"
},
"scripts": {
"js": "jshint ./perfbudget.js"
Expand Down
61 changes: 61 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Command Line Tests
*/

var mocha = require('mocha'),
chai = require('chai'),
expect = chai.expect,
perfbudget = require('../perfbudget.js'),
cli = require('../lib/normalizeCli.js');

describe('perfbudget', function() {

// set timeout to account for time taken to get results
this.timeout(60000);

it('should fail if no API Key is provided', function () {
var options = {
"url": "http://www.bbc.co.uk",
"key": "",
"location": "Dulles:Chrome",
"wptInstance": "www.webpagetest.org",
"video": 1,
"runs": 1,
"pollResults": 5,
"timeout": 60,
"repeatView": false,
"budget": {
"render": "1000",
"SpeedIndex": "1000"
}
};

expect(function() {
perfbudget.runTest('123', 0);
}).to.throw();
});

// should return metrics
it('should return results for requested metrics', function (done) {
var options = {
"url": "http://www.bbc.co.uk",
"key": "",
"location": "Dulles:Chrome",
"wptInstance": "www.webpagetest.org",
"video": 1,
"runs": 1,
"pollResults": 5,
"timeout": 60,
"repeatView": false,
"budget": {
"render": "1000",
"SpeedIndex": "1000"
}
};

perfbudget.runTest(options, function (err) {
if (err) throw err;
done();
});
});
});

2 comments on commit c0854be

@stefanjudis
Copy link
Contributor

Choose a reason for hiding this comment

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

@oliverfarrell

I think this is super hard to test, because cli.js is so tightly coupled to perfbudget.js and can not think of any sane solution to test this. But it's doing one function call and some logging.

I think we can live without that. :bowtie:

But I'd write tests for normalizeCli and perfbudget totally encapsulated.

The code might need some refactoring to make it possible to mock the Webpagetest-API. We shouldn't test code that doesn't belong to us ( this is strongly opinionated ).

What do you think?

@oliverfarrell
Copy link
Owner

Choose a reason for hiding this comment

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

@stefanjudis

I must confess I'm still getting to grips with the concept of writing tests – it's quite possible I might be going about some of this a little wrong.

I'm about to push an update to reflect your comments. I'm renaming the /test/cli.js file to /test/perfbudget.js and have put the "should return results for requested metrics" test inside that which when I supply it with an API key, passes the test. As all of these tests would require an API key is this why you suggest mocking the WebPageTest-API to avoid us having to store an API key inside the tests?

Inside /test/normalizeCli.js I should then be testing that each flag works as expected, would that be correct?

Forgive me for being a bit of a newb with this :)

Please sign in to comment.