-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 56ebfbd
Showing
7 changed files
with
6,267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"env": { | ||
"node": true, | ||
"es6": true, | ||
"jest": true | ||
}, | ||
"extends": "eslint:recommended" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Chai Assertions for Nock | ||
|
||
**Nock Chai** extends [Chai](http://chaijs.com/) with an language for asserting facts about [Nock](https://www.npmjs.com/package/nock). | ||
|
||
Instead of manually wiring up your expectations to intercepting a nocked request: | ||
|
||
```javascript | ||
const nockedRequest = nock('http://some-url'); | ||
|
||
nockedRequest.on('request', function(req, interceptor, body) { | ||
expect(body).to.equal('foo'); | ||
}); | ||
``` | ||
|
||
you can write code that expresses what you really mean: | ||
|
||
```javascript | ||
return expect(nock('http://some-url')).to.have.been.requested; | ||
``` | ||
|
||
|
||
## Installation | ||
```npm install chai-nock``` | ||
|
||
Then add to your test setup: | ||
|
||
```javascript | ||
const chai = require('chai'); | ||
const chaiNock = require('chai-nock'); | ||
|
||
chai.use(chaiNock); | ||
``` | ||
|
||
## Assertions | ||
|
||
#### requested | ||
```javascript | ||
expect(nock).to.have.been.requested; | ||
expect(nock).not.to.have.been.requested; | ||
``` | ||
|
||
## Examples | ||
|
||
Using Chai's `expect`: | ||
|
||
```javascript | ||
const { expect } = require('chai'); | ||
const nock = require('nock'); | ||
const request = require('request-promise-native'); | ||
|
||
describe('bbc.co.uk', () => { | ||
it('should make a request to the bbc.co.uk', function() { | ||
const requestNock = nock('http://bbc.co.uk') | ||
.get('/') | ||
.reply(200); | ||
|
||
request('http://bbc.co.uk'); | ||
|
||
return expect(requestNock).to.have.been.requested; | ||
}); | ||
}); | ||
``` | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"use strict"; | ||
|
||
module.exports = (chai) => { | ||
const Assertion = chai.Assertion; | ||
const MAX_TIMEOUT = 1000; | ||
|
||
const promisfyNockInterceptor = (nock) => { | ||
return new Promise((resolve, reject) => { | ||
let body; | ||
const timeout = setTimeout(() => { | ||
reject(new Error('The request has not been recieved by Nock')); | ||
}, MAX_TIMEOUT); | ||
|
||
nock.on('request', (req, interceptor, reqBody) => { | ||
body = reqBody; | ||
}); | ||
|
||
nock.on('replied', () => { | ||
clearTimeout(timeout); | ||
resolve(body); | ||
}); | ||
|
||
nock.on('error', err => { | ||
clearTimeout(timeout); | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
const isNock = (obj) => { | ||
if ( | ||
typeof(obj) !== 'object' || | ||
!obj.interceptors || | ||
!obj.interceptors.length) | ||
{ | ||
throw new TypeError('You must provide a valid Nock'); | ||
} | ||
} | ||
|
||
function assert(truthy) { | ||
this.assert(truthy, 'expected Nock to have been requested', 'expected Nock to have not been requested'); | ||
} | ||
|
||
Assertion.addProperty('requested', function () { | ||
isNock(this._obj); | ||
|
||
return promisfyNockInterceptor(this._obj) | ||
.then( | ||
() => assert.call(this, true), | ||
() => assert.call(this, false) | ||
); | ||
}); | ||
}; |
Oops, something went wrong.