Skip to content

Commit

Permalink
travis and coveralls
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisandrews7 committed Dec 14, 2018
1 parent 7563fb2 commit 889336b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 96 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
node_js:
- "6"
script:
- npm run style
- npm run test:coverage
after_script:
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
162 changes: 67 additions & 95 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Chai Assertions for Nock

[![npm version](https://img.shields.io/npm/v/chai-nock.svg?style=flat)](https://www.npmjs.com/package/chai-nock)
[![Build Status](https://travis-ci.org/chrisandrews7/chai-nock.svg?branch=master)](https://travis-ci.org/chrisandrews7/chai-nock) [![Coverage Status](https://coveralls.io/repos/github/chrisandrews7/chai-nock/badge.svg?branch=master)](https://coveralls.io/github/chrisandrews7/chai-nock?branch=master) [![npm version](https://img.shields.io/npm/v/chai-nock.svg?style=flat)](https://www.npmjs.com/package/chai-nock)

**Nock Chai** extends [Chai](http://chaijs.com/) with a language for asserting facts about [Nock](https://www.npmjs.com/package/nock).

Expand Down Expand Up @@ -37,147 +37,119 @@ chai.use(chaiNock);

## Assertions

### `requested`
### requested

Asserts that a request has been made to the nock.

```javascript
expect(nock).to.have.been.requested;
expect(nock).not.to.have.been.requested;
```

#### Example
it('requested', () => {
const requestNock = nock('http://bbc.co.uk')
.get('/')
.reply(200);

```javascript
const { expect } = require('chai');
const nock = require('nock');
const request = require('request-promise-native');

describe('example', () => {
it('should make a request to bbc.co.uk', function() {
const requestNock = nock('http://bbc.co.uk')
.get('/')
.reply(200);

request({
uri: 'http://bbc.co.uk',
});

return expect(requestNock).to.have.been.requested;
request({
uri: 'http://bbc.co.uk',
});

return expect(requestNock).to.have.been.requested;
});
```

### `requestedWith(body)`
### requestedWith(body)

Asserts that a request has been made to the nock with a body that exactly matches the object provided.

```javascript
expect(nock).to.have.been.requestedWith(body);
expect(nock).not.to.have.been.requestedWith(body);
```

#### Example

```javascript
const { expect } = require('chai');
const nock = require('nock');
const request = require('request-promise-native');

describe('example', () => {
it('should make a request to bbc.co.uk', function() {
const requestNock = nock('http://bbc.co.uk')
.get('/')
.reply(200);

request({
json: true,
uri: 'http://bbc.co.uk',
body: {
hello: 'world'
}
});

return expect(requestNock).to.have.been.requestedWith({ hello: 'world' });
it('requestedWith', () => {
const requestNock = nock('http://bbc.co.uk')
.get('/')
.reply(200);

request({
json: true,
uri: 'http://bbc.co.uk',
body: {
hello: 'world'
}
});

return expect(requestNock).to.have.been.requestedWith({ hello: 'world' });
});
```

### `requestedWithHeaders(headers)`
### requestedWithHeaders(headers)

Asserts that a request has been made to the nock with headers that exactly match the object provided.

```javascript
expect(nock).to.have.been.requestedWithHeaders(headers);
expect(nock).not.to.have.been.requestedWithHeaders(headers);
```

#### Example

```javascript
const { expect } = require('chai');
const nock = require('nock');
const request = require('request-promise-native');

describe('example', () => {
it('should make a request to bbc.co.uk with exactly correct headers', function() {
const requestNock = nock('http://bbc.co.uk')
.get('/')
.reply(200);

request({
json: true,
uri: 'http://bbc.co.uk',
headers: {
myHeader: 'myHeaderValue'
}
});

return expect(requestNock).to.have.been.requestedWithHeaders({
host: 'bbc.co.uk',
accept: 'application/json',
it('requestedWithHeaders', () => {
const requestNock = nock('http://bbc.co.uk')
.get('/')
.reply(200);

request({
json: true,
uri: 'http://bbc.co.uk',
headers: {
myHeader: 'myHeaderValue'
});
}
});

return expect(requestNock).to.have.been.requestedWithHeaders({
host: 'bbc.co.uk',
accept: 'application/json',
myHeader: 'myHeaderValue'
});
});
```

_Note: request-promise-native library adds `host` and `accept` headers from the `uri` and `json` options provided so we need to include them in our exact headers object_

### `requestedWithHeadersMatch(headers)`
### requestedWithHeadersMatch(partialHeaders)

Asserts that a request has been made to the nock with headers that contain the key/value pairs in the object provided.

```javascript
expect(nock).to.have.been.requestedWithHeadersMatch(headers);
expect(nock).not.to.have.been.requestedWithHeadersMatch(headers);
it('requestedWithHeadersMatch', () => {
const requestNock = nock('http://bbc.co.uk')
.get('/')
.reply(200);

request({
json: true,
uri: 'http://bbc.co.uk',
headers: {
myHeader: 'myHeaderValue',
otherHeader: 'otherHeaderValue'
}
});

return expect(requestNock).to.have.been.requestedWithHeadersMatch({
myHeader: 'myHeaderValue'
});
});
```

#### Example
## Usage

```javascript
const { expect } = require('chai');
const nock = require('nock');
const request = require('request-promise-native');

describe('example', () => {
it('should make a request to bbc.co.uk the correct myHeader value in the headers', function() {
it('test', () => {
const requestNock = nock('http://bbc.co.uk')
.get('/')
.reply(200);
.get('/')
.reply(200);

request({
json: true,
uri: 'http://bbc.co.uk',
headers: {
myHeader: 'myHeaderValue',
otherHeader: 'otherHeaderValue'
body: {
hello: 'world'
}
});

return expect(requestNock).to.have.been.requestedWithHeadersMatch({
myHeader: 'myHeaderValue'
});
return expect(requestNock).to.have.been.requestedWith({ hello: 'world' });
});
});
```
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "jest test",
"test:watch": "jest test --watch",
"test:coverage": "npm test -- --coverage",
"style": "eslint ."
},
"keywords": [
Expand All @@ -28,6 +28,7 @@
},
"homepage": "https://github.com/chrisandrews7/chai-nock#readme",
"devDependencies": {
"coveralls": "^3.0.2",
"eslint": "^5.9.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^3.3.0",
Expand Down

0 comments on commit 889336b

Please sign in to comment.