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

fix(types): Scope accepts legacy Url not WHATWG #1879

Merged
merged 3 commits into from Feb 10, 2020
Merged
Show file tree
Hide file tree
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
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