-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
77 lines (62 loc) · 2.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict'
module.exports = tapeNockFactory
const nock = require('nock')
const sanitizeFilename = require('sanitize-filename')
const path = require('path')
function tapeNockFactory (tapeTest, nockOpts) {
const nockBack = nock.back
nockOpts = nockOpts || {}
nockBack.fixtures = nockOpts.fixtures || path.join(path.dirname(module.parent.filename), 'fixtures')
if (nockOpts.mode) nockBack.setMode(nockOpts.mode)
const defaultTestOptions = nockOpts.defaultTestOptions || {}
const testnames = []
function testTestWithNock (fn) {
fn = fn || tapeTest
return function testWithNock (_name, _opts, _cb) {
const args = getTestArgs(_name, _opts, _cb)
let sanitized = sanitizeFilename(args.name)
if (sanitized.length < 1) sanitized = 'fixtures'
const filename = sanitized + '_.json'
if (testnames.indexOf(filename) > -1) {
const mustBeUnique = 'tape-nock: Duplicate test filename: "' + filename + '". All test descriptions must be unique.'
throw new Error(mustBeUnique)
}
testnames.push(filename)
const emitter = fn(args.name, args.opts, args.cb)
emitter.once('prerun', function () {
nockBack(filename, Object.assign({}, defaultTestOptions, args.opts), function (nockDone) {
emitter.once('end', function () {
nockDone()
})
})
})
return emitter
}
}
const testWithNock = testTestWithNock()
Object.keys(tapeTest).forEach(function (key) {
if (typeof tapeTest[key] !== 'function') return
testWithNock[key] = tapeTest[key]
})
testWithNock.nock = nock
testWithNock.only = testTestWithNock(tapeTest.only)
return testWithNock
}
tapeNockFactory.nock = nock
const getTestArgs = function (name_, opts_, cb_) {
let name = '(anonymous)'
let opts = {}
let cb
for (let i = 0; i < arguments.length; i++) {
const arg = arguments[i]
const t = typeof arg
if (t === 'string') {
name = arg
} else if (t === 'object') {
opts = arg || opts
} else if (t === 'function') {
cb = arg
}
}
return { name, opts, cb }
}