Socket
Socket
Sign inDemoInstall

stubs

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.2 to 2.0.0

5

index.js

@@ -23,5 +23,6 @@ 'use strict';

var args = [].slice.call(arguments)
var returnVal
if (cfg.callthrough)
cached.apply(obj, args)
returnVal = cached.apply(obj, args)

@@ -32,3 +33,5 @@ stub.apply(obj, args)

obj[method] = cached
return returnVal
}
}

7

package.json
{
"name": "stubs",
"version": "1.1.2",
"version": "2.0.0",
"description": "Easy method stubber.",

@@ -21,3 +21,6 @@ "main": "index.js",

},
"homepage": "https://github.com/stephenplusplus/stubs"
"homepage": "https://github.com/stephenplusplus/stubs",
"devDependencies": {
"tess": "^1.0.0"
}
}

@@ -5,144 +5,122 @@ 'use strict';

var stubs = require('./')
var tess = require('tess')
var tests = []
tess('init', function(it) {
it('is a function', function() {
assert.equal(typeof stubs, 'function')
})
test('is a function', function() {
assert.equal(typeof stubs, 'function')
it('throws without obj || method', function() {
assert.throws(function() {
stubs()
}, /must provide/)
assert.throws(function() {
stubs({})
}, /must provide/)
})
})
test('throws without obj || method', function() {
assert.throws(function() {
stubs()
}, /must provide/)
tess('stubs', function(it) {
it('stubs a method with a noop', function() {
var originalCalled = false
assert.throws(function() {
stubs({})
}, /must provide/)
})
var obj = {}
obj.method = function() {
originalCalled = true
}
test('stubs a method with a noop', function() {
var originalCalled = false
stubs(obj, 'method')
var obj = {}
obj.method = function() {
originalCalled = true
}
obj.method()
stubs(obj, 'method')
assert(!originalCalled)
})
obj.method()
it('accepts an override', function() {
var originalCalled = false
assert(!originalCalled)
})
var obj = {}
obj.method = function() {
originalCalled = true
}
test('accepts an override', function() {
var originalCalled = false
var replacementCalled = false
stubs(obj, 'method', function() {
replacementCalled = true
})
var obj = {}
obj.method = function() {
originalCalled = true
}
var replacementCalled = false
stubs(obj, 'method', function() {
replacementCalled = true
obj.method()
assert(!originalCalled)
assert(replacementCalled)
})
obj.method()
assert(!originalCalled)
assert(replacementCalled)
})
it('calls through to original method', function() {
var originalCalled = false
test('calls through to original method', function() {
var originalCalled = false
var obj = {}
obj.method = function() {
originalCalled = true
}
var obj = {}
obj.method = function() {
originalCalled = true
}
var replacementCalled = false
stubs(obj, 'method', { callthrough: true }, function() {
replacementCalled = true
})
var replacementCalled = false
stubs(obj, 'method', { callthrough: true }, function() {
replacementCalled = true
obj.method()
assert(originalCalled)
assert(replacementCalled)
})
obj.method()
assert(originalCalled)
assert(replacementCalled)
})
it('returns value of original method call', function() {
var uniqueVal = Date.now()
test('stops calling stub after n calls', function() {
var timesToCall = 5
var timesCalled = 0
var obj = {}
obj.method = function() {
return uniqueVal
}
var obj = {}
obj.method = function() {
assert.equal(timesCalled, timesToCall)
}
stubs(obj, 'method', { callthrough: true }, function() {})
stubs(obj, 'method', { calls: timesToCall }, function() {
timesCalled++
assert.equal(obj.method(), uniqueVal)
})
obj.method() // 1 (stub)
obj.method() // 2 (stub)
obj.method() // 3 (stub)
obj.method() // 4 (stub)
obj.method() // 5 (stub)
obj.method() // 6 (original)
})
it('stops calling stub after n calls', function() {
var timesToCall = 5
var timesCalled = 0
test('calls stub in original context of obj', function() {
var secret = 'brownies'
var obj = {}
obj.method = function() {
assert.equal(timesCalled, timesToCall)
}
function Class() {
this.method = function() {}
this.secret = secret
}
stubs(obj, 'method', { calls: timesToCall }, function() {
timesCalled++
})
var cl = new Class()
stubs(cl, 'method', function() {
assert.equal(this.secret, secret)
obj.method() // 1 (stub)
obj.method() // 2 (stub)
obj.method() // 3 (stub)
obj.method() // 4 (stub)
obj.method() // 5 (stub)
obj.method() // 6 (original)
})
cl.method()
})
it('calls stub in original context of obj', function() {
var secret = 'brownies'
function test(message, fn) {
try {
fn()
tests.push({ success: true, fail: false, message: message })
} catch(e) {
tests.push({ success: false, fail: true, message: message, error: e })
}
}
function Class() {
this.method = function() {}
this.secret = secret
}
tests.forEach(function(test, index) {
function black(message) {
return '\u001b[30m' + message + '\u001b[39m'
}
function greenBg(message) {
return '\u001b[42m' + message + '\u001b[49m'
}
function redBg(message) {
return '\u001b[41m' + message + '\u001b[49m'
}
function bold(message) {
return '\u001b[1m' + message + '\u001b[22m'
}
var cl = new Class()
var icon, message
if (test.success) {
icon = '✔︎'
message = greenBg(black(' ' + icon + ' ' + test.message + ' '))
} else {
icon = '✖'
message = redBg(bold(' ' + icon + ' ' + test.message + ' '))
}
stubs(cl, 'method', function() {
assert.equal(this.secret, secret)
})
console.log((index > 0 ? '\n' : '') + message)
if (test.error) {
console.log(' ' + test.error.stack)
}
})
cl.method()
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc