Skip to content

Commit

Permalink
Merge pull request #178 from pretenderjs/warn-on-second-pretender
Browse files Browse the repository at this point in the history
Warn on second pretender
  • Loading branch information
trek authored Oct 7, 2016
2 parents 0ab71bd + 87dcbab commit 5b95cd7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pretender.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function Pretender(/* routeMap1, routeMap2, ...*/) {

// capture xhr requests, channeling them into
// the route map.
self.XMLHttpRequest = interceptor(this);
self.XMLHttpRequest = interceptor(this, this._nativeXMLHttpRequest);

// 'start' the server
this.running = true;
Expand All @@ -132,7 +132,7 @@ function Pretender(/* routeMap1, routeMap2, ...*/) {
}
}

function interceptor(pretender) {
function interceptor(pretender, nativeRequest) {
function FakeRequest() {
// super()
FakeXMLHttpRequest.call(this);
Expand Down Expand Up @@ -259,6 +259,12 @@ function interceptor(pretender) {
};

FakeRequest.prototype = proto;

if (nativeRequest.prototype._passthroughCheck) {
throw new Error('You created a second Pretender instance while there was already one running. ' +
'Running two Pretender servers at once will lead to unexpected results!' +
'Please call .shutdown() on your instances when you no longer need them to respond.');
}
return FakeRequest;
}

Expand Down
21 changes: 21 additions & 0 deletions test/creation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ var pretender;
var describe = QUnit.module;
var it = QUnit.test;

describe('pretender creation - without shutdown', function(config) {
var secondPretender;

config.beforeEach(function() {
pretender = new Pretender();
});

config.afterEach(function() {
pretender.shutdown();
});

test('an error is thrown when you start a new pretender while another one is running', function(assert) {
var message = 'You created a second Pretender instance while there ' +
'already one running. Running two Pretender servers at once will lead to unexpected results!';
assert.throws(function() {
new Pretender();
}, message);
});
});

describe('pretender creation', function(config) {
config.afterEach(function() {
if (pretender) {
Expand Down Expand Up @@ -55,3 +75,4 @@ describe('pretender creation', function(config) {
});
});


0 comments on commit 5b95cd7

Please sign in to comment.