Skip to content

Commit

Permalink
.requested property assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisandrews7 committed Nov 26, 2018
0 parents commit 56ebfbd
Show file tree
Hide file tree
Showing 7 changed files with 6,267 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"env": {
"node": true,
"es6": true,
"jest": true
},
"extends": "eslint:recommended"
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
65 changes: 65 additions & 0 deletions README.md
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;
});
});
```



53 changes: 53 additions & 0 deletions lib/nock-chai.js
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)
);
});
};
Loading

0 comments on commit 56ebfbd

Please sign in to comment.