Skip to content

Commit

Permalink
Rename package
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisandrews7 committed Nov 27, 2018

Verified

This commit was signed with the committer’s verified signature.
woodruffw William Woodruff
1 parent 0ffdd33 commit d08ccde
Showing 5 changed files with 70 additions and 43 deletions.
29 changes: 16 additions & 13 deletions lib/nock-chai.js → index.js
Original file line number Diff line number Diff line change
@@ -7,19 +7,25 @@ module.exports = (chai) => {

function 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.once('replied', (req, interceptor) => {
clearTimeout(timeout);
nock.once('request', (req, interceptor, reqBody) => {
try {
resolve(JSON.parse(interceptor.body));
body = JSON.parse(reqBody);
} catch (err) {
resolve(interceptor.body);
body = reqBody;
}
});

nock.once('replied', () => {
clearTimeout(timeout);
resolve(body);
});

nock.once('error', err => {
clearTimeout(timeout);
reject(err);
@@ -54,14 +60,11 @@ module.exports = (chai) => {
isNock(this._obj);

return promisfyNockInterceptor(this._obj)
.then(
(requestBody) => {
if (equal(requestBody, arg)) {
return this.assert(true, null, 'expected Nock to have not been requested with #{exp}', arg);
}
return this.assert(false, 'expected Nock to have been requested with #{exp}, but was requested with #{act}', 'expected Nock to have not been requested with #{exp}', arg, requestBody);
},
() => this.assert(false, 'expected Nock to have been requested, but it was never called')
);
.then((nockRequest) => {
if (equal(nockRequest, arg)) {
return this.assert(true, null, 'expected Nock to have not been requested with #{exp}', arg);
}
return this.assert(false, 'expected Nock to have been requested with #{exp}, but was requested with #{act}', 'expected Nock to have not been requested with #{exp}', arg, nockRequest);
},() => this.assert(false, 'expected Nock to have been requested, but it was never called'));
});
};
2 changes: 1 addition & 1 deletion package-lock.json

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

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "nock-chai",
"name": "chai-nock",
"version": "0.2.0",
"description": "Extends Chai with assertions for the Nock Http mocking framework",
"main": "lib/index.js",
"main": "index.js",
"scripts": {
"test": "jest test",
"style": "eslint ."
@@ -18,14 +18,14 @@
],
"repository": {
"type": "git",
"url": "git+https://github.com/ComparetheMarket/nock-chai.git"
"url": "git+https://github.com/ComparetheMarket/chai-nock.git"
},
"author": "Chris Andrews, Ben Vaughan-Jones",
"license": "MIT",
"bugs": {
"url": "https://github.com/ComparetheMarket/nock-chai/issues"
"url": "https://github.com/ComparetheMarket/chai-nock/issues"
},
"homepage": "https://github.com/ComparetheMarket/nock-chai#readme",
"homepage": "https://github.com/ComparetheMarket/chai-nock#readme",
"devDependencies": {
"eslint": "^5.9.0",
"jest": "^23.6.0",
6 changes: 3 additions & 3 deletions test/requested.test.js
Original file line number Diff line number Diff line change
@@ -2,10 +2,10 @@ const { expect, use} = require('chai');
const nock = require('nock');
const request = require('request-promise-native');

const nockChai = require('../lib/nock-chai');
use(nockChai);
const chaiNock = require('../');
use(chaiNock);

describe('Requested assertions', () => {
describe('requested assertions', () => {
const TEST_URL = 'http://someurl.com';

afterEach(() => {
66 changes: 45 additions & 21 deletions test/requestedWith.test.js
Original file line number Diff line number Diff line change
@@ -2,15 +2,11 @@ const { expect, use} = require('chai');
const nock = require('nock');
const request = require('request-promise-native');

const nockChai = require('../lib/nock-chai');
use(nockChai);
const chaiNock = require('../');
use(chaiNock);

describe('requestedWith() assertions', () => {
const TEST_URL = 'http://someurl.com';
const mockArgument = {
test: 12345,
value: 6789,
};

afterEach(() => {
nock.cleanAll();
@@ -36,34 +32,50 @@ describe('requestedWith() assertions', () => {
describe('when a request to the nock has been made with the correct argument', () => {
describe('with a simple argument', () => {
it('passes', () => {
const requestNock = nock(TEST_URL).get('/').reply(200, 'test');
request(TEST_URL);
const requestNock = nock(TEST_URL).get('/').reply(200);
request({
json: true,
uri: TEST_URL,
body: 'test'
});

return expect(requestNock).to.have.been.requestedWith('test');
});
});

describe('with an Object as an argument', () => {
it('passes', () => {
const requestNock = nock(TEST_URL).get('/').reply(200, mockArgument);
request(TEST_URL);
const requestNock = nock(TEST_URL).get('/').reply(200);
request({
json: true,
uri: TEST_URL,
body: {
test: 123,
}
});

return expect(requestNock).to.have.been.requestedWith(mockArgument);
return expect(requestNock).to.have.been.requestedWith({
test: 123,
});
});
});
});

describe('when a request to the nock has been made but with incorrect arguments', () => {
it('throws', (done) => {
const requestNock = nock(TEST_URL).get('/').reply(200, { test: 1 });
request(TEST_URL);
const requestNock = nock(TEST_URL).get('/').reply(200);
request({
json: true,
uri: TEST_URL,
body: { test: 1 }
});

const assertion = expect(requestNock).to.have.been.requestedWith(mockArgument);
const assertion = expect(requestNock).to.have.been.requestedWith({ test: 2 });

return assertion
.then(() => done.fail('Should have thrown an error'))
.catch((err) => {
expect(err.message).to.equal('expected Nock to have been requested with { test: 12345, value: 6789 }, but was requested with { test: 1 }');
expect(err.message).to.equal('expected Nock to have been requested with { test: 2 }, but was requested with { test: 1 }');
done();
});
});
@@ -73,7 +85,7 @@ describe('requestedWith() assertions', () => {
it('throws', (done) => {
const requestNock = nock(TEST_URL).get('/').reply(200);

const assertion = expect(requestNock).to.have.been.requestedWith(mockArgument);
const assertion = expect(requestNock).to.have.been.requestedWith({ test: 123 });

return assertion
.then(() => done.fail('Should have thrown an error'))
@@ -88,8 +100,12 @@ describe('requestedWith() assertions', () => {
describe('.not.requestedWith()', () => {
describe('when a request to the nock has been made with the incorrect arguments', () => {
it('passes', () => {
const requestNock = nock(TEST_URL).get('/').reply(200, mockArgument);
request(TEST_URL);
const requestNock = nock(TEST_URL).get('/').reply(200);
request({
json: true,
uri: TEST_URL,
body: { test: 123 }
});

return expect(requestNock).not.to.have.been.requestedWith('different_value');
});
@@ -105,15 +121,23 @@ describe('requestedWith() assertions', () => {

describe('when a request to the nock has been made with matching arguments', () => {
it('throws', (done) => {
const requestNock = nock(TEST_URL).get('/').reply(200, mockArgument);
request(TEST_URL);
const mockArgument = {
test: 12345,
};

const requestNock = nock(TEST_URL).get('/').reply(200);
request({
json: true,
uri: TEST_URL,
body: mockArgument
});

const assertion = expect(requestNock).not.to.have.been.requestedWith(mockArgument);

return assertion
.then(() => done.fail('Should have thrown an error'))
.catch((err) => {
expect(err.message).to.equal('expected Nock to have not been requested with { test: 12345, value: 6789 }');
expect(err.message).to.equal('expected Nock to have not been requested with { test: 12345 }');
done();
});
});

0 comments on commit d08ccde

Please sign in to comment.