From 9375dcddeb9ff2b70fd20f89c2a045b800e813ac Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Sat, 22 Feb 2020 10:09:46 -0800 Subject: [PATCH] refactor(test): Mocha DSL for event tests (#1928) --- tests/setup.js | 1 + tests/test_events.js | 34 +++++++++++++++------------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/tests/setup.js b/tests/setup.js index 5064aef01..ff48fc265 100644 --- a/tests/setup.js +++ b/tests/setup.js @@ -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() diff --git a/tests/test_events.js b/tests/test_events.js index b3d93e2da..d8f81ae8e 100644 --- a/tests/test_events.js +++ b/tests/test_events.js @@ -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() @@ -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') @@ -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) })