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

Commit

Permalink
feat: add nock back in
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed May 31, 2018
1 parent f0c363c commit d3ed0ff
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 13 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,13 @@ Automatically calls `done()` to ensure the calls were made and `cleanAll()` to r

```js
const fancy = require('fancy-test')
.register('nock', require('@fancy-test/nock'))

describe('nock tests', () => {
fancy
.nock('https://api.github.com', nock => {
nock
.nock('https://api.github.com', api => api
.get('/me')
.reply(200, {name: 'jdxcode'})
})
)
.it('mocks http call to github', async () => {
const {body: user} = await HTTP.get('https://api.github.com/me')
expect(user).to.have.property('name', 'jdxcode')
Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/jdxcode/fancy-test/issues",
"dependencies": {
"lodash": "^4.17.10",
"stdout-stderr": "^0.1.9"
},
"devDependencies": {
"@oclif/tslint": "^1.1.2",
"@types/chai": "^4.1.3",
"@types/lodash": "^4.14.109",
"@types/mocha": "^5.2.0",
"@types/nock": "^9.1.3",
"@types/node": "^10.3.0",
"@types/sinon": "^5.0.1",
"lodash": "^4.17.10",
"stdout-stderr": "^0.1.9"
},
"devDependencies": {
"@oclif/tslint": "^1.1.2",
"chai": "^4.1.2",
"chalk": "^2.4.1",
"http-call": "^5.1.4",
"mocha": "^5.2.0",
"nock": "^9.3.0",
"sinon": "^5.0.10",
"ts-node": "^6.0.5",
"tslib": "^1.9.2",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import base from './base'
import _catch from './catch'
import {expect} from './chai'
import env from './env'
import * as Nock from './nock'
import {stderr, stdout} from './stdmock'
import stub from './stub'

Expand All @@ -13,6 +14,7 @@ export const fancy = base
.register('stub', stub)
.register('stderr', stderr)
.register('stdout', stdout)
.register('nock', Nock.nock)

export type Fancy = typeof fancy

Expand Down
31 changes: 31 additions & 0 deletions src/nock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Nock from 'nock'

export function nock(host?: string, options?: nock.Callback | nock.Options, cb?: nock.Callback) {
if (host === undefined) throw new Error('host is undefined')
if (typeof options === 'function') {
cb = options
options = {}
}
if (cb === undefined) throw new Error('callback is undefined')

const nock: typeof Nock = require('nock')
const intercepter = nock(host, options)
return {
async run(ctx: {nock: number}) {
ctx.nock = ctx.nock || 0
await cb!(intercepter)
ctx.nock++
},
finally(ctx: {error?: Error, nock: number}) {
if (!ctx.error) intercepter.done()
ctx.nock--
if (ctx.nock === 0) nock.cleanAll()
},
}
}

export namespace nock {
export interface Scope extends Nock.Scope {}
export interface Options extends Nock.Options {}
export type Callback = (nock: Scope) => any
}
29 changes: 29 additions & 0 deletions test/nock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import HTTP from 'http-call'

import {expect, fancy} from '../src'

describe('nock', () => {
// from readme
fancy
.nock('https://api.github.com', nock => {
nock
.get('/me')
.reply(200, {name: 'jdxcode'})
})
.end('mocks http call to github', async () => {
const {body: user} = await HTTP.get('https://api.github.com/me')
expect(user).to.have.property('name', 'jdxcode')
})
// from readme

fancy
.nock('https://api.github.com', {reqheaders: {foo: 'bar'}}, nock => {
nock
.get('/me')
.reply(200, {name: 'jdxcode'})
})
.end('passes options', async () => {
const {body: user} = await HTTP.get('https://api.github.com/me', {headers: {foo: 'bar'}})
expect(user).to.have.property('name', 'jdxcode')
})
})
76 changes: 72 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@
version "5.2.0"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.0.tgz#b3c8e69f038835db1a7fdc0b3d879fc50506e29e"

"@types/node@^10.3.0":
"@types/nock@^9.1.3":
version "9.1.3"
resolved "https://registry.yarnpkg.com/@types/nock/-/nock-9.1.3.tgz#1d445679375b9e25afd449dc56585f81729454e8"
dependencies:
"@types/node" "*"

"@types/node@*", "@types/node@^10.3.0":
version "10.3.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.3.0.tgz#078516315a84d56216b5d4fed8f75d59d3b16cac"

Expand Down Expand Up @@ -168,6 +174,10 @@ [email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"

content-type@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.4.tgz#e138cc75e040c727b1966fe5e5f8c9aee256fe3b"

[email protected], debug@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
Expand All @@ -180,6 +190,10 @@ deep-eql@^3.0.0:
dependencies:
type-detect "^4.0.0"

deep-equal@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5"

[email protected], diff@^3.1.0, diff@^3.2.0, diff@^3.5.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
Expand Down Expand Up @@ -248,6 +262,16 @@ [email protected]:
version "1.1.1"
resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"

http-call@^5.1.4:
version "5.1.4"
resolved "https://registry.yarnpkg.com/http-call/-/http-call-5.1.4.tgz#fc37a1f6638ece82aba3d12eb6030f0889da850a"
dependencies:
content-type "^1.0.4"
debug "^3.1.0"
is-retry-allowed "^1.1.0"
is-stream "^1.1.0"
tunnel-agent "^0.6.0"

inflight@^1.0.4:
version "1.0.6"
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
Expand All @@ -263,6 +287,14 @@ inversify@^4.10.0:
version "4.13.0"
resolved "https://registry.yarnpkg.com/inversify/-/inversify-4.13.0.tgz#0ab40570bfa4474b04d5b919bbab3a4f682a72f5"

is-retry-allowed@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-1.1.0.tgz#11a060568b67339444033d0125a61a20d564fb34"

is-stream@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
Expand All @@ -278,6 +310,10 @@ js-yaml@^3.7.0:
argparse "^1.0.7"
esprima "^4.0.0"

json-stringify-safe@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"

just-extend@^1.1.27:
version "1.1.27"
resolved "https://registry.yarnpkg.com/just-extend/-/just-extend-1.1.27.tgz#ec6e79410ff914e472652abfa0e603c03d60e905"
Expand All @@ -286,7 +322,7 @@ lodash.get@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"

lodash@^4.17.10:
lodash@^4.17.10, lodash@^4.17.5:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

Expand Down Expand Up @@ -316,7 +352,7 @@ minimist@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"

[email protected], mkdirp@^0.5.1:
[email protected], mkdirp@^0.5.0, mkdirp@^0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
dependencies:
Expand Down Expand Up @@ -352,6 +388,20 @@ nise@^1.3.3:
path-to-regexp "^1.7.0"
text-encoding "^0.6.4"

nock@^9.3.0:
version "9.3.0"
resolved "https://registry.yarnpkg.com/nock/-/nock-9.3.0.tgz#4dcfdd37bd249836754d05bbac5a1f05e12e0f16"
dependencies:
chai "^4.1.2"
debug "^3.1.0"
deep-equal "^1.0.0"
json-stringify-safe "^5.0.1"
lodash "^4.17.5"
mkdirp "^0.5.0"
propagate "^1.0.0"
qs "^6.5.1"
semver "^5.5.0"

once@^1.3.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
Expand All @@ -376,6 +426,14 @@ pathval@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0"

propagate@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/propagate/-/propagate-1.0.0.tgz#00c2daeedda20e87e3782b344adba1cddd6ad709"

qs@^6.5.1:
version "6.5.2"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"

reflect-metadata@^0.1.12:
version "0.1.12"
resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.12.tgz#311bf0c6b63cd782f228a81abe146a2bfa9c56f2"
Expand All @@ -386,11 +444,15 @@ resolve@^1.3.2:
dependencies:
path-parse "^1.0.5"

safe-buffer@^5.0.1:
version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"

[email protected]:
version "1.3.0"
resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.3.0.tgz#8d1d9350e25622da30de3e44ba692b5221ab7c50"

semver@^5.3.0:
semver@^5.3.0, semver@^5.5.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab"

Expand Down Expand Up @@ -533,6 +595,12 @@ tsutils@^2.12.1, tsutils@^2.24.0:
dependencies:
tslib "^1.8.1"

tunnel-agent@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd"
dependencies:
safe-buffer "^5.0.1"

type-detect@^4.0.0, type-detect@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
Expand Down

0 comments on commit d3ed0ff

Please sign in to comment.