Skip to content
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(test): Mocha DSL for event tests #1928

Merged
merged 3 commits into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ afterEach(function() {
nock.restore()
nock.cleanAll()
nock.enableNetConnect()
nock.emitter.removeAllListeners()
// It's important that Sinon is restored before Nock is reactivated for tests that stub/spy built-in methods,
// otherwise Sinon loses track of the stubs and can't restore them.
sinon.restore()
Expand Down
34 changes: 15 additions & 19 deletions tests/test_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
const { expect } = require('chai')
const http = require('http')
const sinon = require('sinon')
const { test } = require('tap')

const nock = require('..')
const got = require('./got_client')

require('./cleanup_after_each')()
require('./setup')

function ignore() {}

test('emits request and replied events when request has no body', async () => {
it('emits request and replied events when request has no body', async () => {
const scope = nock('http://example.test')
.get('/')
.reply()
Expand All @@ -31,7 +29,7 @@ test('emits request and replied events when request has no body', async () => {
expect(onReplied).to.have.been.calledOnce()
})

test('emits request and request body', async () => {
it('emits request and request body', async () => {
const data = 'example=123'

const scope = nock('http://example.test')
Expand Down Expand Up @@ -68,36 +66,34 @@ test('emits request and request body', async () => {
expect(onReplied).to.have.been.calledOnce()
})

test('emits no match when no match and no mock', function(t) {
nock.emitter.once('no match', function() {
t.end()
it('emits no match when no match and no mock', done => {
nock.emitter.once('no match', () => {
done()
})

const req = http.get('http://example.test/abc')
req.once('error', ignore)
http.get('http://example.test/abc').once('error', ignore)
})

test('emits no match when no match and mocked', function(t) {
it('emits no match when no match and mocked', done => {
nock('http://example.test')
.get('/')
.reply(418)

const assertion = function(req) {
nock.emitter.on('no match', req => {
expect(req.path).to.equal('/definitelymaybe')
nock.emitter.removeAllListeners('no match')
t.end()
}
nock.emitter.on('no match', assertion)
done()
})

http.get('http://example.test/definitelymaybe').once('error', ignore)
})

test('emits no match when netConnect is disabled', function(t) {
it('emits no match when netConnect is disabled', done => {
nock.disableNetConnect()
nock.emitter.on('no match', function(req) {

nock.emitter.on('no match', req => {
expect(req.hostname).to.equal('example.test')
nock.emitter.removeAllListeners('no match')
t.end()
done()
})

http.get('http://example.test').once('error', ignore)
})