-
Notifications
You must be signed in to change notification settings - Fork 50
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
Versions returned in the wrong order #26
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
'use strict'; | ||
|
||
var proxyquire = require('proxyquire'), | ||
expect = require('chai').expect, | ||
sinon = require('sinon'), | ||
semver = require('semver'); | ||
|
||
var VERBS = ['get', 'post', 'put', 'del']; | ||
|
||
describe('reggie npm server (unit)', function () { | ||
var reggieServer, | ||
req = {}, | ||
res = {}, | ||
routes = {}, | ||
restify = {}, | ||
data = function Data() {}; | ||
|
||
var versions = [ | ||
'1.0.0', | ||
'1.0.1', | ||
'1.0.2-2', | ||
'1.0.2-10' | ||
]; | ||
data.prototype.packageMeta = function () { | ||
return { | ||
versions: versions.reduce(function (verObj, currVer) { | ||
verObj[currVer] = {data: {}, time: {}}; | ||
return verObj; | ||
}, {}) | ||
}; | ||
}; | ||
data.prototype.whichVersions = function () { | ||
return versions; | ||
}; | ||
|
||
beforeEach(function () { | ||
//Store route functions in a hash | ||
VERBS.forEach(function (verb) { routes[verb] = {}; }); | ||
|
||
//mock restify | ||
restify.createServer = function() { | ||
var serverMock = { | ||
'use': function () {}, | ||
'listen': function () {} | ||
}; | ||
VERBS.forEach(function (verb) { | ||
serverMock[verb] = function (route, cb) { | ||
routes[verb][route] = cb; | ||
}; | ||
}); | ||
return serverMock; | ||
}; | ||
|
||
data.prototype.init = function () {}; | ||
|
||
reggieServer = proxyquire('../server', { | ||
'restify': restify, | ||
'./lib/data': data | ||
}); | ||
|
||
req.params = { name: 'test' }; | ||
res.json = sinon.spy(); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are already tests for npm server in I believe that integration tests are better for testing implementation of npm protocol, because we want to ensure reggie fulfils whatever npm client expects from the server. Have you considered that? What are your arguments for going with unit-tests & mocking instead? Let's discuss this first. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are basically regression tests for a specific bug. If the tests were E2E, a change unrelated to the bug could cause the test to fail even though the bug hasn't actually resurfaced. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. Now there are few things to improve in your unit-tests:
it('chooses latest by semver order', // etc
We can nest tests in (cc: @paolodm - the same problem of unit vs. integration tests applies to your pull request #22)
it('Should choose latest by semver order', function () {
givenPackageWithVersions('test-package', ['1.0.2-2', '1.0.2-10']);
req.name = 'test-package';
routes.get['/:name'](req, res);
expect(res.json.firstCall.args[1]['dist-tags'].latest).to.equal('1.0.2-10');
});
res.json.firstCall.args[1] Please extract it into a helper function. Alternatively, set up sinon to save the json result to a variable and possibly assert that it was called only once. |
||
|
||
describe('/:name', function () { | ||
beforeEach(function () { | ||
routes.get['/:name'](req, res); | ||
}); | ||
|
||
it('Should choose latest by semver order', function () { | ||
expect(res.json.firstCall.args[1]['dist-tags'].latest).to.equal('1.0.2-10'); | ||
}); | ||
|
||
it('Should "sort" by semver order', function () { | ||
// Yes, this is depending on an implementation detail of Object.keys | ||
// Yes, that violates the API contract of Object and is bad | ||
// Yes, this is how the npm client actually works | ||
var resultVersions; | ||
resultVersions = Object.keys(res.json.firstCall.args[1].versions); | ||
expect(resultVersions).to.deep.equal(versions.sort(semver.compare)); | ||
}); | ||
}); | ||
|
||
describe('/-/all', function () { | ||
beforeEach(function () { | ||
data.prototype.getAllPackageNames = function () { | ||
return ['test']; | ||
}; | ||
routes.get['/-/all'](req, res); | ||
}); | ||
|
||
it('Should choose "latest" by semver order', function () { | ||
expect(res.json.firstCall.args[1].test['dist-tags'].latest).to.equal('1.0.2-10'); | ||
}); | ||
|
||
it('Should "sort" by semver order', function () { | ||
// Yes, this is depending on an implementation detail of Object.keys | ||
// Yes, that violates the API contract of Object and is bad | ||
// Yes, this is how the npm client actually works | ||
var resultVersions; | ||
resultVersions = Object.keys(res.json.firstCall.args[1].test.versions); | ||
expect(resultVersions).to.deep.equal(versions.sort(semver.compare)); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just DRYed two birds with one stone by adding this. Hopefully that is an acceptable alternative to altering data.js.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, your solution is fine.