Skip to content

Commit

Permalink
Merge pull request #572 from tabone/new-keyword
Browse files Browse the repository at this point in the history
Return new instance if new keyword is missing.
  • Loading branch information
3rd-Eden committed Sep 9, 2015
2 parents 626976c + 7812a55 commit 7debd82
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/WebSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var closeTimeout = 30 * 1000; // Allow 30 seconds to terminate the connection cl
*/
function WebSocket(address, protocols, options) {
if (this instanceof WebSocket === false) {
throw new TypeError("Classes can't be function-called");
return new WebSocket(address, protocols, options);
}

EventEmitter.call(this);
Expand Down
2 changes: 1 addition & 1 deletion lib/WebSocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var util = require('util')

function WebSocketServer(options, callback) {
if (this instanceof WebSocketServer === false) {
throw new TypeError("Classes can't be function-called");
return new WebSocketServer(options, callback);
}

events.EventEmitter.call(this);
Expand Down
13 changes: 5 additions & 8 deletions test/WebSocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,11 @@ describe('WebSocket', function() {
done();
}
});
it('throws TypeError when called without new', function(done) {
try {
var ws = WebSocket('ws://localhost:' + port);
}
catch (e) {
e.should.be.instanceof(TypeError);
done();
}

it('should return a new instance if called without new', function(done) {
var ws = WebSocket('ws://localhost:' + port);
ws.should.be.an.instanceOf(WebSocket);
done();
});
});

Expand Down
14 changes: 5 additions & 9 deletions test/WebSocketServer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ function areArraysEqual(x, y) {

describe('WebSocketServer', function() {
describe('#ctor', function() {
it('throws TypeError when called without new', function(done) {
try {
var ws = WebSocketServer({noServer: true});
}
catch (e) {
e.should.be.instanceof(TypeError);
done();
}
it('should return a new instance if called without new', function(done) {
var ws = WebSocketServer({noServer: true});
ws.should.be.an.instanceOf(WebSocketServer);
done();
});

it('throws an error if no option object is passed', function() {
var gotException = false;
try {
Expand Down

0 comments on commit 7debd82

Please sign in to comment.