Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
fix(nock): added nock.cleanAll()
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 25, 2018
1 parent 8198686 commit 5c40945
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +20,6 @@ export {
Next,
CatchOptions,
EnvOptions,
NockHosts,
NockScope,
StdmockOptions,
}
Expand Down
18 changes: 10 additions & 8 deletions src/nock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 5c40945

Please sign in to comment.