From e22233bd04d9fc5ed458d449e93f8f89f4a20ab6 Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Sun, 9 Feb 2020 20:26:42 -0700 Subject: [PATCH] fix(types): Scope accepts legacy Url not WHATWG (#1879) 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. --- lib/scope.js | 2 +- tests/test_scope.js | 29 +++++++++++++++++++++++++++++ types/tests.ts | 10 ++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index 9f2216d46..dca6cef4c 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -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 diff --git a/tests/test_scope.js b/tests/test_scope.js index fb4235b09..a51d702ff 100644 --- a/tests/test_scope.js +++ b/tests/test_scope.js @@ -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') @@ -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') diff --git a/types/tests.ts b/types/tests.ts index d18af7428..acefc5e30 100644 --- a/types/tests.ts +++ b/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 @@ -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')