Skip to content

Commit 67b27d3

Browse files
author
Clay Reimann
committed
Fix tests to actually fail instead of timing out
Errors thrown in the callbacks don’t bubble up to the calling test and make the tests timeout instead of fail.
1 parent 998b78f commit 67b27d3

9 files changed

Lines changed: 420 additions & 412 deletions

File tree

.gitignore

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
.DS_Store
2-
.idea
3-
node_modules/
4-
npm-debug.log
1+
dist/
52
coverage/
3+
node_modules/
4+
5+
.idea
6+
.DS_Store
67
sauce.json
8+
npm-debug.log

gulpfile.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ var stylish = require('gulp-jscs-stylish');
99
var path = require('path');
1010
var karma = require('karma');
1111

12-
console.log(Object.keys(webpack));
13-
1412
/*
1513
* Code style enforcement
1614
*/
@@ -42,7 +40,8 @@ function runTests(singleRun, isCI, done) {
4240
var files = [
4341
path.join(__dirname, 'test/vendor/*.js'), // PhantomJS 1.x polyfills
4442
path.join(__dirname, 'dist', 'github.min.js'),
45-
path.join(__dirname, 'test/test.*.js')
43+
path.join(__dirname, 'test', 'helpers.js'),
44+
path.join(__dirname, 'test', 'test.*.js')
4645
];
4746

4847
if (singleRun) {

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = function(config) {
1515

1616
autoWatch: false,
1717

18-
frameworks: ['mocha', 'chai', 'phantomjs-shim'],
18+
frameworks: ['mocha', 'chai'],
1919

2020
browsers: ['Chrome'],
2121

test/helpers.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
function callbackWithError(done, fn) {
4+
var cb = function(err, response, xhr) {
5+
try {
6+
fn(err, response, xhr);
7+
} catch(e) {
8+
done(e);
9+
}
10+
};
11+
12+
return cb;
13+
}
14+
15+
if (typeof window === 'undefined') {
16+
// Export stuff (comment here to make linter happy)
17+
module.exports = callbackWithError;
18+
}

test/test.auth.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,52 @@
11
'use strict';
22

3-
var testUser, github, user;
3+
var testUser;
44

55
if (typeof window === 'undefined') {
6-
// Module dependencies
7-
var chai = require('chai');
86
var Github = require('../');
7+
var callbackWithError = require('./helpers.js');
98

109
testUser = require('./user.json');
1110

11+
// Module dependencies
12+
var chai = require('chai');
13+
1214
// Use should flavour for Mocha
1315
var should = chai.should();
1416
}
1517

16-
describe('Github constructor', function() {
18+
describe('Authentication', function() {
1719
before(function() {
1820
if (typeof window !== 'undefined') testUser = window.__fixtures__['test/user'];
21+
});
1922

20-
github = new Github({
23+
it('should authenticate with valid credentials', function(done) {
24+
var github = new Github({
2125
username: testUser.USERNAME,
2226
password: testUser.PASSWORD,
2327
auth: 'basic'
2428
});
29+
var user = github.getUser();
2530

26-
user = github.getUser();
27-
});
28-
29-
it('should authenticate and return no errors', function(done) {
30-
user.notifications(function(err) {
31+
user.notifications(callbackWithError(done, function(err) {
3132
should.not.exist(err);
3233
done();
33-
});
34+
}));
3435
});
35-
});
36-
37-
describe('Github constructor (failing case)', function() {
38-
before(function() {
39-
if (typeof window !== 'undefined') testUser = window.__fixtures__['test/user'];
4036

41-
github = new Github({
37+
it('should fail authentication with invalid credentials', function(done) {
38+
var github = new Github({
4239
username: testUser.USERNAME,
4340
password: 'fake124',
4441
auth: 'basic'
4542
});
46-
user = github.getUser();
47-
});
43+
var user = github.getUser();
44+
45+
user.notifications(callbackWithError(done, function(err) {
46+
err.status.should.equal(401, 'Return 401 status for bad auth');
47+
err.response.data.message.should.equal('Bad credentials');
4848

49-
it('should fail authentication and return err', function(done) {
50-
user.notifications(function(err) {
51-
err.request.status.should.equal(401, 'Return 401 status for bad auth');
52-
JSON.parse(err.request.responseText).message.should.equal('Bad credentials');
5349
done();
54-
});
50+
}));
5551
});
5652
});

test/test.issue.js

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

3-
var testUser, github, issues;
3+
var testUser;
44

55
if (typeof window === 'undefined') {
6-
// Module dependencies
7-
var chai = require('chai');
86
var Github = require('../');
7+
var callbackWithError = require('./helpers.js');
98

109
testUser = require('./user.json');
1110

11+
// Module dependencies
12+
var chai = require('chai');
13+
1214
// Use should flavour for Mocha
1315
var should = chai.should();
1416
}
1517

16-
describe('Github.Issue', function() {
18+
describe('Issues', function() {
19+
var github, issues;
20+
1721
before(function() {
1822
if (typeof window !== 'undefined') testUser = window.__fixtures__['test/user'];
19-
2023
github = new Github({
2124
username: testUser.USERNAME,
2225
password: testUser.PASSWORD,
@@ -27,20 +30,21 @@ describe('Github.Issue', function() {
2730
});
2831

2932
it('should list issues', function(done) {
30-
issues.list({}, function(err, issues) {
33+
issues.list({}, callbackWithError(done, function(err, issues) {
3134
should.not.exist(err);
3235
issues.should.have.length.above(0);
3336
done();
34-
});
37+
}));
3538
});
3639

3740
it('should post issue comment', function(done) {
38-
issues.list({}, function(err, issuesList) {
39-
issues.comment(issuesList[0], 'Comment test', function(err, res) {
41+
issues.list({}, callbackWithError(done, function(err, issuesList) {
42+
should.not.exist(err);
43+
issues.comment(issuesList[0], 'Comment test', callbackWithError(done, function(err, res) {
4044
should.not.exist(err);
4145
res.body.should.equal('Comment test');
4246
done();
43-
});
44-
});
47+
}));
48+
}));
4549
});
4650
});

0 commit comments

Comments
 (0)