Skip to content

Commit

Permalink
fix(types): Scope accepts legacy Url not WHATWG (#1879)
Browse files Browse the repository at this point in the history
Updates the types to properly reflect that a Scope can be created with
the output of `url.parse`. Previously, it denoted that it accepted a
`url.URL` instance. To clarify, tests were also added proving that one
works while the other throws an error.
  • Loading branch information
mastermatt committed Feb 10, 2020
1 parent 061e922 commit e22233b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/scope.js
Expand Up @@ -22,7 +22,7 @@ try {
}

/**
* @param {string|RegExp} basePath
* @param {string|RegExp|url.url} basePath
* @param {Object} options
* @param {boolean} options.allowUnmocked
* @param {string[]} options.badheaders
Expand Down
29 changes: 29 additions & 0 deletions tests/test_scope.js
Expand Up @@ -4,6 +4,7 @@ const path = require('path')
const { expect } = require('chai')
const sinon = require('sinon')
const proxyquire = require('proxyquire').preserveCache()
const url = require('url')
const Interceptor = require('../lib/interceptor')
const nock = require('..')
const got = require('./got_client')
Expand All @@ -24,6 +25,34 @@ it('scope exposes interceptors', () => {
})
})

describe('`Scope#constructor`', () => {
it('accepts the output of url.parse', async () => {
const scope = nock(url.parse('http://example.test'))
.get('/')
.reply()

const { statusCode } = await got('http://example.test')
expect(statusCode).to.equal(200)
scope.done()
})

it.skip('accepts a WHATWG URL instance', async () => {
const scope = nock(new url.URL('http://example.test'))
.get('/')
.reply()

const { statusCode } = await got('http://example.test')
expect(statusCode).to.equal(200)
scope.done()
})

it('fails when provided a WHATWG URL instance', () => {
// This test just proves the lack of current support. When this feature is added,
// this test should be removed and the test above un-skipped.
expect(() => nock(new url.URL('http://example.test'))).to.throw()
})
})

describe('`Scope#remove()`', () => {
it('removes an active mock', () => {
const scope = nock('http://example.test')
Expand Down
10 changes: 8 additions & 2 deletions types/tests.ts
@@ -1,6 +1,6 @@
import nock from 'nock'
import * as fs from 'fs'
import { URL, URLSearchParams } from 'url'
import url, { URLSearchParams } from 'url'

let scope: nock.Scope = nock('http://example.test')
let inst: nock.Interceptor
Expand Down Expand Up @@ -159,7 +159,13 @@ nock('http://example.test')
})

// Using URL as input
scope = nock(new URL('https://example.test/'))
// Not supported yet
// scope = nock(new URL('https://example.test/'))
// .get('/resource')
// .reply(200, 'url matched')

// specifying URL from url.parse output
scope = nock(url.parse('https://example.test/'))
.get('/resource')
.reply(200, 'url matched')

Expand Down

0 comments on commit e22233b

Please sign in to comment.