Skip to content

Commit

Permalink
Add before callback to support plugins (#28)
Browse files Browse the repository at this point in the history
- Restore global setup to avoid a failure in the new tests
  • Loading branch information
paulmelnikow committed Apr 13, 2017
1 parent 579c9b2 commit 4ce0b40
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 22 deletions.
26 changes: 26 additions & 0 deletions lib/icedfrisby.js
Expand Up @@ -170,6 +170,7 @@ function Frisby(msg) {
it: null,
isNot: false, // test negation
inspections: [], // array of inspections to perform before the expectations
before: [],
expects: [], // array expectations to perform
after: [],
retry: clone.retry || 0,
Expand Down Expand Up @@ -941,6 +942,18 @@ Frisby.prototype.waits = function(ms) {
return this;
};

/**
* @param {afterCallback} cb - callback
* @return {object}
* @desc callback function to run before the request is made
*/
Frisby.prototype.before = function (cb) {
this.current.before.push(function() {
cb.call(this);
});
return this;
};

/**
* @param {afterCallback} cb - callback
* @return {object}
Expand Down Expand Up @@ -1075,6 +1088,19 @@ Frisby.prototype.toss = function (retry) {
let retries = (self.current.outgoing.method.toUpperCase() ==
"POST") ? 0 : self.current.retry;

for(let i=0; i < self.current.before.length; i++) {
const fn = self.current.before[i];
if(false !== self._exceptionHandler) {
try {
fn.call(self);
} catch(e) {
self._exceptionHandler(e);
}
} else {
fn.call(self);
}
}

// wait optinally, launch request
if (self.current.waits > 0) {
setTimeout(makeRequest, self.current.waits);
Expand Down
98 changes: 76 additions & 22 deletions test/frisby_mock_request.js
Expand Up @@ -162,6 +162,54 @@ describe('Frisby matchers', function() {
restoreGlobalSetup();
});

describe('before callbacks', function () {
it('should be invoked in sequence before the request', function() {
const sequence = [];

const mockFn = mockRequest.mock()
.get('/test-object')
.respond({
statusCode: 200,
body: fixtures.singleObject
})
.run();
const requestFn = function () {
sequence.push('request');
return mockFn.apply(this, arguments);
};

frisby.create(this.test.title)
.before(() => { sequence.push('before-one'); })
.before(() => { sequence.push('before-two'); })
.get('http://mock-request/test-object', {mock: requestFn})
.after(() => {
const expectedSequence = ['before-one', 'before-two', 'request'];
expect(sequence).to.deep.equal(expectedSequence);
})
.toss();
});

it('should respect the exception handler', function() {
const mockFn = mockRequest.mock()
.get('/test-object')
.respond({
statusCode: 200,
body: fixtures.singleObject
})
.run();

const message = 'this is the error';

frisby.create(this.test.title)
.before(() => { throw new Error(message); })
.get('http://mock-request/test-object', {mock: mockFn})
.exceptionHandler(err => {
expect(err.message).to.equal(message);
})
.toss();
});
});

it('expectJSON should test EQUALITY for a SINGLE object', function() {
// Mock API
var mockFn = mockRequest.mock()
Expand Down Expand Up @@ -822,30 +870,36 @@ describe('Frisby matchers', function() {
.toss();
});

it('globalSetup should be able to set baseURI', function() {
nock('http://httpbin.org', { allowUnmocked: true })
.post('/test')
.once()
.reply(200, function(uri, requestBody) {
return requestBody;
});

frisby.globalSetup({
request: {
baseUri: 'http://httpbin.org'
}
describe('globalSetup', function () {
afterEach(function () {
frisby.globalSetup();
});

frisby.create(this.test.title)
.post('/test', {}, {
body: 'some body here'
})
.expectStatus(200)
.expectBodyContains('some body here')
.after(function() {
expect(this.current.outgoing.uri).to.equal('http://httpbin.org/test');
})
.toss();
it('should be able to set baseURI', function() {
nock('http://httpbin.org', { allowUnmocked: true })
.post('/test')
.once()
.reply(200, function(uri, requestBody) {
return requestBody;
});

frisby.globalSetup({
request: {
baseUri: 'http://httpbin.org'
}
});

frisby.create(this.test.title)
.post('/test', {}, {
body: 'some body here'
})
.expectStatus(200)
.expectBodyContains('some body here')
.after(function() {
expect(this.current.outgoing.uri).to.equal('http://httpbin.org/test');
})
.toss();
});
});

it('baseUri should be able to override global setup', function() {
Expand Down

0 comments on commit 4ce0b40

Please sign in to comment.