Skip to content

Commit

Permalink
Close #557 PR: improve test coverage for calling classes without the …
Browse files Browse the repository at this point in the history
…`new` keyword. Fixes #161
  • Loading branch information
Carnubak authored and sindresorhus committed Feb 16, 2016
1 parent ef371bd commit e50f3de
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/concurrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var BAIL_ERROR = new Error();
module.exports = Concurrent;

function Concurrent(tests, bail) {
if (!this instanceof Concurrent) {
if (!(this instanceof Concurrent)) {
throw new TypeError('Class constructor Concurrent cannot be invoked without \'new\'');
}

Expand Down
2 changes: 1 addition & 1 deletion lib/sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var isPromise = require('is-promise');
module.exports = Sequence;

function Sequence(tests, bail) {
if (!this instanceof Sequence) {
if (!(this instanceof Sequence)) {
throw new TypeError('Class constructor Sequence cannot be invoked without \'new\'');
}

Expand Down
8 changes: 8 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ var fs = require('fs');
var test = require('tap').test;
var Api = require('../api');

test('must be called with new', function (t) {
t.throws(function () {
var api = Api;
api([path.join(__dirname, 'fixture/es2015.js')]);
}, {message: 'Class constructor Api cannot be invoked without \'new\''});
t.end();
});

test('ES2015 support', function (t) {
t.plan(1);

Expand Down
11 changes: 11 additions & 0 deletions test/ava-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
var test = require('tap').test;
var AvaError = require('../lib/ava-error');

test('must be called with new', function (t) {
t.throws(function () {
var avaError = AvaError;
avaError('test message');
}, {message: 'Class constructor AvaError cannot be invoked without \'new\''});
t.end();
});
8 changes: 4 additions & 4 deletions test/caching-precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var test = require('tap').test;
var uniqueTempDir = require('unique-temp-dir');

var CachingPrecompiler = require('../lib/caching-precompiler');
var createCachingPrecompiler = CachingPrecompiler;

function fixture(name) {
return path.join(__dirname, 'fixture', name);
Expand All @@ -26,10 +25,11 @@ test('creation with new', function (t) {
t.end();
});

test('creation without new throws', function (t) {
test('must be called with new', function (t) {
t.throws(function () {
createCachingPrecompiler(uniqueTempDir());
});
var cachingPrecompiler = CachingPrecompiler;
cachingPrecompiler(uniqueTempDir());
}, {message: 'Class constructor CachingPrecompiler cannot be invoked without \'new\''});
t.end();
});

Expand Down
6 changes: 3 additions & 3 deletions test/concurrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,9 @@ test('rejections are just passed through - bail', function (t) {

test('must be called with new', function (t) {
t.throws(function () {
var c = Concurrent;
c([pass('a')]);
});
var concurrent = Concurrent;
concurrent([pass('a')]);
}, {message: 'Class constructor Concurrent cannot be invoked without \'new\''});
t.end();
});

Expand Down
11 changes: 11 additions & 0 deletions test/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
var test = require('tap').test;
var Logger = require('../lib/logger');

test('must be called with new', function (t) {
t.throws(function () {
var logger = Logger;
logger();
}, {message: 'Class constructor Logger cannot be invoked without \'new\''});
t.end();
});
8 changes: 8 additions & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ var Runner = require('../lib/runner');

var noop = function () {};

test('must be called with new', function (t) {
t.throws(function () {
var runner = Runner;
runner();
}, {message: 'Class constructor Runner cannot be invoked without \'new\''});
t.end();
});

test('runner emits a "test" event', function (t) {
var runner = new Runner();

Expand Down
9 changes: 8 additions & 1 deletion test/sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,14 @@ test('must be called with new', function (t) {
t.throws(function () {
var sequence = Sequence;
sequence([pass('a')]);
});
}, {message: 'Class constructor Sequence cannot be invoked without \'new\''});
t.end();
});

test('needs at least one sequence item', function (t) {
t.throws(function () {
new Sequence().run();
}, {message: 'Sequence items can\'t be undefined'});
t.end();
});

Expand Down
8 changes: 4 additions & 4 deletions test/test-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ function serialize(collection) {
return ret;
}

test('requires new', function (t) {
var withoutNew = TestCollection;
test('must be called with new', function (t) {
var testCollection = TestCollection;
t.throws(function () {
withoutNew();
});
testCollection();
}, {message: 'Class constructor TestCollection cannot be invoked without \'new\''});
t.end();
});

Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ ava.cb = function (title, fn, contextRef, report) {
return t;
};

test('must be called with new', function (t) {
t.throws(function () {
var test = Test;
test();
}, {message: 'Class constructor Test cannot be invoked without \'new\''});
t.end();
});

test('run test', function (t) {
var result = ava('foo', function (a) {
a.fail();
Expand Down

0 comments on commit e50f3de

Please sign in to comment.