Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use Mocha DSL in test_nock_lifecycle (#1809)
- Loading branch information
1 parent
eac299b
commit c209c6b
Showing
2 changed files
with
174 additions
and
160 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 |
---|---|---|
@@ -1,215 +1,213 @@ | ||
'use strict' | ||
|
||
const http = require('http') | ||
const { test } = require('tap') | ||
const { expect } = require('chai') | ||
const nock = require('..') | ||
const sinon = require('sinon') | ||
const assertRejects = require('assert-rejects') | ||
const got = require('./got_client') | ||
|
||
require('./cleanup_after_each')() | ||
require('./setup') | ||
|
||
test('double activation throws exception', t => { | ||
nock.restore() | ||
expect(nock.isActive()).to.be.false() | ||
describe('Nock lifecycle functions', () => { | ||
describe('`activate()`', () => { | ||
it('double activation throws exception', () => { | ||
nock.restore() | ||
expect(nock.isActive()).to.be.false() | ||
|
||
nock.activate() | ||
expect(nock.isActive()).to.be.true() | ||
nock.activate() | ||
expect(nock.isActive()).to.be.true() | ||
|
||
expect(() => nock.activate()).to.throw(Error, 'Nock already active') | ||
expect(() => nock.activate()).to.throw(Error, 'Nock already active') | ||
|
||
expect(nock.isActive()).to.be.true() | ||
|
||
t.end() | ||
}) | ||
|
||
test('(re-)activate after restore', async t => { | ||
const onResponse = sinon.spy() | ||
|
||
const server = http.createServer((request, response) => { | ||
onResponse() | ||
|
||
if (request.url === '/') { | ||
response.writeHead(200) | ||
response.write('server served a response') | ||
} | ||
expect(nock.isActive()).to.be.true() | ||
}) | ||
|
||
response.end() | ||
}) | ||
it('(re-)activate after restore', async () => { | ||
const onResponse = sinon.spy() | ||
|
||
t.once('end', () => server.close()) | ||
await new Promise(resolve => server.listen(resolve)) | ||
const url = `http://localhost:${server.address().port}` | ||
const server = http.createServer((request, response) => { | ||
onResponse() | ||
|
||
const scope = nock(url) | ||
.get('/') | ||
.reply(304, 'served from our mock') | ||
if (request.url === '/') { | ||
response.writeHead(200) | ||
response.write('server served a response') | ||
} | ||
|
||
nock.restore() | ||
expect(nock.isActive()).to.be.false() | ||
response.end() | ||
}) | ||
|
||
expect(await got(url)).to.include({ statusCode: 200 }) | ||
await new Promise(resolve => server.listen(resolve)) | ||
const url = `http://localhost:${server.address().port}` | ||
|
||
expect(scope.isDone()).to.be.false() | ||
const scope = nock(url) | ||
.get('/') | ||
.reply(304, 'served from our mock') | ||
|
||
nock.activate() | ||
expect(nock.isActive()).to.be.true() | ||
nock.restore() | ||
expect(nock.isActive()).to.be.false() | ||
|
||
expect(await got(url)).to.include({ statusCode: 304 }) | ||
expect(await got(url)).to.include({ statusCode: 200 }) | ||
|
||
expect(scope.isDone()).to.be.true() | ||
expect(scope.isDone()).to.be.false() | ||
|
||
expect(onResponse).to.have.been.calledOnce() | ||
}) | ||
nock.activate() | ||
expect(nock.isActive()).to.be.true() | ||
|
||
test('clean all works', async t => { | ||
nock('http://example.test') | ||
.get('/') | ||
.twice() | ||
.reply() | ||
expect(await got(url)).to.include({ statusCode: 304 }) | ||
|
||
await got('http://example.test/') | ||
expect(scope.isDone()).to.be.true() | ||
|
||
nock.cleanAll() | ||
expect(onResponse).to.have.been.calledOnce() | ||
|
||
await assertRejects(got('http://example.test/'), err => { | ||
expect(err).to.include({ name: 'RequestError', code: 'ENOTFOUND' }) | ||
return true | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
|
||
test('cleanAll should remove pending mocks from all scopes', t => { | ||
const scope1 = nock('http://example.test') | ||
.get('/somepath') | ||
.reply(200, 'hey') | ||
expect(scope1.pendingMocks()).to.deep.equal([ | ||
'GET http://example.test:80/somepath', | ||
]) | ||
const scope2 = nock('http://example.test') | ||
.get('/somepath') | ||
.reply(200, 'hey') | ||
expect(scope2.pendingMocks()).to.deep.equal([ | ||
'GET http://example.test:80/somepath', | ||
]) | ||
|
||
nock.cleanAll() | ||
|
||
expect(scope1.pendingMocks()).to.be.empty() | ||
expect(scope2.pendingMocks()).to.be.empty() | ||
t.end() | ||
}) | ||
describe('`cleanAll()`', () => { | ||
it('removes a half-consumed mock', async () => { | ||
nock('http://example.test') | ||
.get('/') | ||
.twice() | ||
.reply() | ||
|
||
test('cleanAll removes persistent mocks', async t => { | ||
nock('http://example.test') | ||
.persist() | ||
.get('/') | ||
.reply() | ||
await got('http://example.test/') | ||
|
||
nock.cleanAll() | ||
nock.cleanAll() | ||
|
||
await assertRejects(got('http://example.test/'), err => { | ||
expect(err).to.include({ | ||
name: 'RequestError', | ||
code: 'ENOTFOUND', | ||
await assertRejects(got('http://example.test/'), err => { | ||
expect(err).to.include({ name: 'RequestError', code: 'ENOTFOUND' }) | ||
return true | ||
}) | ||
}) | ||
return true | ||
}) | ||
}) | ||
|
||
test('is done works', async t => { | ||
nock('http://example.test') | ||
.get('/') | ||
.reply(200) | ||
|
||
expect(nock.isDone()).to.be.false() | ||
|
||
await got('http://example.test/') | ||
|
||
expect(nock.isDone()).to.be.true() | ||
}) | ||
it('removes pending mocks from all scopes', () => { | ||
const scope1 = nock('http://example.test') | ||
.get('/somepath') | ||
.reply(200, 'hey') | ||
expect(scope1.pendingMocks()).to.deep.equal([ | ||
'GET http://example.test:80/somepath', | ||
]) | ||
const scope2 = nock('http://example.test') | ||
.get('/somepath') | ||
.reply(200, 'hey') | ||
expect(scope2.pendingMocks()).to.deep.equal([ | ||
'GET http://example.test:80/somepath', | ||
]) | ||
|
||
nock.cleanAll() | ||
|
||
expect(scope1.pendingMocks()).to.be.empty() | ||
expect(scope2.pendingMocks()).to.be.empty() | ||
}) | ||
|
||
test('isDone', async t => { | ||
const scope = nock('http://example.test') | ||
.get('/') | ||
.reply() | ||
it('removes persistent mocks', async () => { | ||
nock('http://example.test') | ||
.persist() | ||
.get('/') | ||
.reply() | ||
|
||
nock.cleanAll() | ||
|
||
await assertRejects(got('http://example.test/'), err => { | ||
expect(err).to.include({ | ||
name: 'RequestError', | ||
code: 'ENOTFOUND', | ||
}) | ||
return true | ||
}) | ||
}) | ||
}) | ||
|
||
expect(scope.isDone()).to.be.false() | ||
describe('`isDone()`', () => { | ||
it('returns false while a mock is pending, and true after it is consumed', async () => { | ||
const scope = nock('http://example.test') | ||
.get('/') | ||
.reply() | ||
|
||
await got('http://example.test/') | ||
expect(nock.isDone()).to.be.false() | ||
|
||
expect(scope.isDone()).to.be.true() | ||
await got('http://example.test/') | ||
|
||
scope.done() | ||
}) | ||
expect(nock.isDone()).to.be.true() | ||
|
||
test('pending mocks works', async t => { | ||
nock('http://example.test') | ||
.get('/') | ||
.reply() | ||
scope.done() | ||
}) | ||
}) | ||
|
||
expect(nock.pendingMocks()).to.deep.equal(['GET http://example.test:80/']) | ||
describe('`pendingMocks()`', () => { | ||
it('returns the expected array while a mock is pending, and an empty array after it is consumed', async () => { | ||
nock('http://example.test') | ||
.get('/') | ||
.reply() | ||
|
||
await got('http://example.test/') | ||
expect(nock.pendingMocks()).to.deep.equal(['GET http://example.test:80/']) | ||
|
||
expect(nock.pendingMocks()).to.be.empty() | ||
}) | ||
await got('http://example.test/') | ||
|
||
test('activeMocks returns incomplete mocks', t => { | ||
nock('http://example.test') | ||
.get('/') | ||
.reply(200) | ||
expect(nock.activeMocks()).to.deep.equal(['GET http://example.test:80/']) | ||
t.end() | ||
}) | ||
expect(nock.pendingMocks()).to.be.empty() | ||
}) | ||
}) | ||
|
||
test("activeMocks doesn't return completed mocks", async t => { | ||
nock('http://example.test') | ||
.get('/') | ||
.reply() | ||
describe('`activeMocks()`', () => { | ||
it('returns the expected array for incomplete mocks', () => { | ||
nock('http://example.test') | ||
.get('/') | ||
.reply(200) | ||
expect(nock.activeMocks()).to.deep.equal(['GET http://example.test:80/']) | ||
}) | ||
|
||
await got('http://example.test/') | ||
expect(nock.activeMocks()).to.be.empty() | ||
t.end() | ||
}) | ||
it("activeMocks doesn't return completed mocks", async () => { | ||
nock('http://example.test') | ||
.get('/') | ||
.reply() | ||
|
||
test('resetting nock catastrophically while a request is in progress is handled gracefully', async t => { | ||
// While invoking cleanAll() from a nock request handler isn't very | ||
// realistic, it's possible that user code under test could crash, causing | ||
// before or after hooks to fire, which invoke `nock.cleanAll()`. A little | ||
// extreme, though if this does happen, we may as well be graceful about it. | ||
function somethingBad() { | ||
nock.cleanAll() | ||
} | ||
|
||
const responseBody = 'hi' | ||
const scope = nock('http://example.test') | ||
.get('/somepath') | ||
.reply(200, (uri, requestBody) => { | ||
somethingBad() | ||
return responseBody | ||
await got('http://example.test/') | ||
expect(nock.activeMocks()).to.be.empty() | ||
}) | ||
}) | ||
|
||
const { body } = await got('http://example.test/somepath') | ||
|
||
expect(body).to.equal(responseBody) | ||
scope.done() | ||
}) | ||
describe('resetting nock catastrophically while a request is in progress', () => { | ||
it('is handled gracefully', async () => { | ||
// While invoking cleanAll() from a nock request handler isn't very | ||
// realistic, it's possible that user code under test could crash, causing | ||
// before or after hooks to fire, which invoke `nock.cleanAll()`. A little | ||
// extreme, though if this does happen, we may as well be graceful about it. | ||
function somethingBad() { | ||
nock.cleanAll() | ||
} | ||
|
||
const responseBody = 'hi' | ||
const scope = nock('http://example.test') | ||
.get('/somepath') | ||
.reply(200, (uri, requestBody) => { | ||
somethingBad() | ||
return responseBody | ||
}) | ||
|
||
const { body } = await got('http://example.test/somepath') | ||
|
||
expect(body).to.equal(responseBody) | ||
scope.done() | ||
}) | ||
}) | ||
|
||
test('abort pending request when abortPendingRequests is called', t => { | ||
const onRequest = sinon.spy() | ||
describe('`abortPendingRequests()`', () => { | ||
it('prevents the request from completing', done => { | ||
const onRequest = sinon.spy() | ||
|
||
nock('http://example.test') | ||
.get('/') | ||
.delayConnection(100) | ||
.reply(200, 'OK') | ||
nock('http://example.test') | ||
.get('/') | ||
.delayConnection(100) | ||
.reply(200, 'OK') | ||
|
||
http.get('http://example.test', onRequest) | ||
http.get('http://example.test', onRequest) | ||
|
||
setTimeout(() => { | ||
expect(onRequest).not.to.have.been.called() | ||
t.end() | ||
}, 200) | ||
process.nextTick(nock.abortPendingRequests) | ||
setTimeout(() => { | ||
expect(onRequest).not.to.have.been.called() | ||
done() | ||
}, 200) | ||
process.nextTick(nock.abortPendingRequests) | ||
}) | ||
}) | ||
}) |
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