Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
simple-mock
Advanced tools
Super simple stubs and spies with 1-step sandbox restore.
For Node:
$ npm install simple-mock
For Browser:
$ bower install simple-mock
Not sure when to use a mock, stub, or spy? Just use simple.mock
.
Examples:
var simple = require('simple-mock')
simple.mock(obj, 'example', 'value') // Replace with this value
simple.mock(obj, 'example') // Spy on underlying method *or* stub
simple.mock(obj, 'example').callFn(function () {}) // Stub
simple.mock(obj, 'example').callbackWith(null, 'etc') // Stub
simple.mock(obj, 'example').returnWith('etc') // Stub
simple.mock(obj, 'example').throwWith(new Error()) // Stub
simple.mock(obj, 'example').resolveWith('etc') // Stub
simple.mock(obj, 'example').rejectWith(new Error()) // Stub
simple.mock(obj, 'example').callOriginal() // Unstubbed call
Then, to make sure all objects are back to the state the were in before your mocks:
simple.restore() // Ideally called in an afterEach() block
callbackWith
, returnWith
and throwWith
can be chained for queued behaviour, e.g.
simple.mock(Something.prototype, 'example')
.callbackWith(null, 'etc')
.callbackWith(new Error())
callbackWith
, returnWith
and throwWith
configurations are stored on a simple array fn.actions
You define your expectations with your own choice of assertion library.
assert(fn.called)
assert.equals(fn.callCount, 3)
assert.equals(fn.lastCall.arg, error) // First parameter of the last call
assert.equals(fn.lastCall.args[1], 'etc') // Second parameter of the last call
assert.equals(fn.calls[0].returned, 'etc')
assert.equals(fn.calls[1].threw, error)
If you need to create a standalone stub (stubs are also spies):
var fn = simple.stub().returnWith('etc')
var returned = fn()
assert.equals(returned, 'etc')
Or spy on a standalone function:
var fn = simple.spy(function () {})
assert.equals(fn.callCount, 0)
assert.equals(fn.calls, [])
See examples for more common usage patterns.
For var simple = require('simple-mock')
:
Sets the value on this object. E.g. mock(config, 'title', 'test')
is the same as config.title = 'test'
, but restorable with all mocks.
Wraps fn
in a spy and sets this on the obj
, restorable with all mocks.
If obj
has already has this function, it is wrapped in a spy. The resulting spy can be turned into a stub by further configuration. Restores with all mocks.
Restores all current mocks.
Use this if you need to restore only a single mock value or function on an object.
Wraps fn
in a spy.
Returns a stub function that is also a spy.
Boolean
Number of times the function was called.
An array of calls, each having these properties:
this
) of the callThe last call object, with properties as above. (This is often also the first and only call.)
Resets all counts and properties to the original state.
Configures this stub to call this function, returning its return value. Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
Configures this stub to call the original, unstubbed function, returning its return value. Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
Configures this stub to return with this value. Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
Configures this stub to throw this error. Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
Configures this stub to call back with the arguments passed. It will use either the last argument as callback, or the argument at cbArgumentIndex
. Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
Configures the last configured function or callback to be called in this context, i.e. this
will be obj
.
Configures the stub to return a Promise (where available] resolving to this value. Same as stub.returnWith(Promise.resolve(val))
. You can use a custom Promise-conforming library, i.e. simple.Promise = require('bluebird')
or simple.Promise = $q
.
Configures the stub to return a Promise (where available) rejecting with this error. Same as stub.returnWith(Promise.reject(val))
. You can use a custom Promise-conforming library, i.e. simple.Promise = require('bluebird')
or simple.Promise = $q
.
An array of behaviours, each having one of these properties:
Boolean (default: true) setting whether the queue of actions for this stub should repeat.
The most complete, framework-agnostic mocking library is sinon.js. It also has pages of documentation and lots of sugar-coating that we generally don't need. Keep it simple!
FAQs
Super simple stubs and spies with 1-step sandbox restore
The npm package simple-mock receives a total of 15,002 weekly downloads. As such, simple-mock popularity was classified as popular.
We found that simple-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.