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

Warn on second pretender #178

Merged
merged 2 commits into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pretender.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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 @@ -110,7 +110,7 @@ function Pretender(/* routeMap1, routeMap2, ...*/) {
}
}

function interceptor(pretender) {
function interceptor(pretender, nativeRequest) {
function FakeRequest() {
// super()
FakeXMLHttpRequest.call(this);
Expand Down Expand Up @@ -237,6 +237,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) {
});
});