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(Suite/Test): untitled suite/test-case #1632 #1809

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/_mocha
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var images = {
program
.version(JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8')).version)
.usage('[debug] [options] [files]')
.option('-A, --async-only', "force all tests to take a callback (async)")
.option('-A, --async-only', "force all tests to take a callback (async) or return a promise")
.option('-c, --colors', 'force enabling of colors')
.option('-C, --no-colors', 'force disabling of colors')
.option('-G, --growl', 'enable growl notification support')
Expand Down
18 changes: 13 additions & 5 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ Runnable.prototype.timeout = function(ms) {
if (ms === 0) {
this._enableTimeouts = false;
}
if (ms > Math.pow(2, 31)) {
throw new RangeError('Timeout too large, must be less than 2^31');
}
if (typeof ms === 'string') {
ms = milliseconds(ms);
}
Expand Down Expand Up @@ -214,6 +217,7 @@ Runnable.prototype.run = function(fn) {
var ctx = this.ctx;
var finished;
var emitted;
var result;

// Sometimes the ctx exists, but it is not runnable
if (ctx && ctx.runnable) {
Expand Down Expand Up @@ -256,7 +260,7 @@ Runnable.prototype.run = function(fn) {
this.resetTimeout();

try {
this.fn.call(ctx, function(err) {
result = this.fn.call(ctx, function(err) {
if (err instanceof Error || toString.call(err) === '[object Error]') {
return done(err);
}
Expand All @@ -268,16 +272,16 @@ Runnable.prototype.run = function(fn) {
}
done();
});

if (result && typeof result.then === 'function') {
return done(new Error('Asynchronous resolution method is overspecified. Specify a callback *or* return a Promise, not both.'));
}
} catch (err) {
done(utils.getError(err));
}
return;
}

if (this.asyncOnly) {
return done(new Error('--async-only option in use without declaring `done()`'));
}

// sync or promise-returning
try {
if (this.pending) {
Expand All @@ -301,6 +305,10 @@ Runnable.prototype.run = function(fn) {
done(reason || new Error('Promise rejected with no or falsy reason'));
});
} else {
if (self.asyncOnly) {
return done(new Error('--async-only option in use without declaring `done()` or returning a promise'));
}

done();
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ exports.create = function(parent, title) {
* @param {Context} parentContext
*/
function Suite(title, parentContext) {
if (!utils.isString(title)) {
throw new Error('Suite `title` should be a "string" but "' + typeof title + '" was given instead.');
}
this.title = title;
function Context() {}
Context.prototype = parentContext;
Expand Down
4 changes: 4 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var Runnable = require('./runnable');
var create = require('lodash.create');
var isString = require('./utils').isString;

/**
* Expose `Test`.
Expand All @@ -19,6 +20,9 @@ module.exports = Test;
* @param {Function} fn
*/
function Test(title, fn) {
if (!isString(title)) {
throw new Error('Test `title` should be a "string" but "' + typeof title + '" was given instead.');
}
Runnable.call(this, title, fn);
this.pending = !fn;
this.type = 'test';
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,10 @@
"_mocha": "./bin/_mocha"
},
"engines": {
"node": ">= 0.8.x"
"node": ">= 0.8.x",
"npm": ">=1.4.3"
},
"engineStrict": true,
"scripts": {
"test": "make test-all"
},
Expand All @@ -278,7 +280,7 @@
"debug": "2.0.0",
"diff": "1.4.0",
"escape-string-regexp": "1.0.2",
"glob": "3.2.3",
"glob": "4.0.6",
"growl": "1.8.1",
"jade": "0.26.3",
"lodash.create": "^3.1.1",
Expand Down
28 changes: 28 additions & 0 deletions test/acceptance/misc/asyncOnly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
describe('asyncOnly', function(){
it('should display an error', function(){

})

it('should pass', function(done){
done();
})

it('should ignore pending tests')

it('should fail when test throws an error', function(){
// the async warning only applies if the test would have otherwise passed
throw Error('you should see this error');
})

describe('with a function that returns a promise', function() {
it('should pass', function(){
var fulfilledPromise = {
then: function (fulfilled, rejected) {
process.nextTick(fulfilled);
}
};

return fulfilledPromise;
})
})
})
8 changes: 8 additions & 0 deletions test/acceptance/overspecified-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe('overspecified asynchronous resolution method', function() {
it('should fail when multiple methods are used', function(done) {
setTimeout(done, 0);

// uncomment
// return { then: function() {} };
});
});
2 changes: 1 addition & 1 deletion test/acceptance/throw.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('a test that throws', function () {
var suite, runner;

beforeEach(function(){
suite = new Suite(null, 'root');
suite = new Suite('Suite', 'root');
runner = new Runner(suite);
})

Expand Down
7 changes: 7 additions & 0 deletions test/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ describe('Runnable(title, fn)', function(){
})
})

describe('#timeout(ms) when ms>2^31', function(){
it('should throw an error', function () {
var run = new Runnable;
run.timeout.bind(run, 1e10).should.throw(/Timeout too large/);
});
});

describe('#enableTimeouts(enabled)', function(){
it('should set enabled', function(){
var run = new Runnable;
Expand Down
2 changes: 1 addition & 1 deletion test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Runner', function(){
var suite, runner;

beforeEach(function(){
suite = new Suite(null, 'root');
suite = new Suite('Suite', 'root');
runner = new Runner(suite);
})

Expand Down
38 changes: 38 additions & 0 deletions test/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,42 @@ describe('Suite', function(){
});

});

describe('initialization', function() {
it('should throw an error if the title isn\'t a string', function() {
(function() {
new Suite(undefined, 'root');
}).should.throw();

(function() {
new Suite(function(){}, 'root');
}).should.throw();
Copy link
Contributor

Choose a reason for hiding this comment

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

Could these assert the error message?

Copy link
Contributor

Choose a reason for hiding this comment

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

And with these I mean every added .should.throw in the PR.

});

it('should not throw if the title is a string', function() {
(function() {
new Suite('Bdd suite', 'root');
}).should.not.throw();
});
});
});

describe('Test', function() {
describe('initialization', function() {
it('should throw an error if the title isn\'t a string', function() {
(function() {
new Test(function(){});
}).should.throw();

(function() {
new Test(undefined, function(){});
}).should.throw();
});

it('should not throw if the title is a string', function() {
(function() {
new Test('test-case', function(){});
}).should.not.throw();
});
});
});