diff --git a/src/index.ts b/src/index.ts index 85a24a5..d32ff6e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import base, {Base, Fancy, Next, Plugin} from './base' import _catch, {CatchOptions} from './catch' import env, {EnvOptions} from './env' import mock from './mock' -import nock, {NockHosts, NockScope} from './nock' +import nock, {NockScope} from './nock' import {stderr, StdmockOptions, stdout} from './stdmock' export const fancy = base @@ -20,7 +20,6 @@ export { Next, CatchOptions, EnvOptions, - NockHosts, NockScope, StdmockOptions, } diff --git a/src/nock.ts b/src/nock.ts index ce07c24..e29b5b5 100644 --- a/src/nock.ts +++ b/src/nock.ts @@ -2,17 +2,19 @@ import * as Nock from 'nock' import {Next} from './base' -export interface NockHosts {[host: string]: Nock.Scope} - export type NockCallback = (nock: Nock.Scope) => any -export default async (next: Next<{nock: NockHosts}>, ctx: any, host: string, cb: NockCallback) => { - const hosts: NockHosts = ctx.nock || {} +export default async (next: Next<{nock: number}>, ctx: any, host: string, cb: NockCallback) => { + const count = (ctx.nock as number || 0) + 1 const nock: typeof Nock = require('nock') - const api = hosts[host] || nock(host) - await cb(api) - await next({nock: hosts}) - api.done() + const intercepter = nock(host) + await cb(intercepter) + try { + await next({nock: count}) + intercepter.done() + } finally { + if (count === 1) nock.cleanAll() + } } export {Scope as NockScope} from 'nock'