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
Changes from 1 commit
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
32 changes: 15 additions & 17 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,36 @@ 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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be moved into an afterEach?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved removeAllListeners to the global afterEach. I seemed like it belongs there.

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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

t.end()
done()
})

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