New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Use Mocha DSL in test_nock_lifecycle #1809
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d94d78a
refactor: Use Mocha DSL in test_nock_lifecycle
paulmelnikow 77da2a0
Merge branch 'master' into mocha-lifecycle
paulmelnikow 61cfa8d
Restore coverage of `nock.isDone()` and move `scope.isDone()` test
paulmelnikow 1fadb1b
Merge branch 'master' into mocha-lifecycle
paulmelnikow da66cce
Merge branch 'master' into mocha-lifecycle
paulmelnikow File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably easier to see in the side-by-side diff that these two tests of
isDone()
are doing the same thing.