
Research
Two Malicious Rust Crates Impersonate Popular Logger to Steal Wallet Keys
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
@adrianhelvik/mock
Advanced tools
[](https://travis-ci.org/adrianhelvik/mock) [](https://coveralls.io/github/adrianhelvi
I have now been using and loving this module for some time, so I decided to write some docs and share it with the world!
import mock from '@adrianhelvik/mock'
const m = mock()
m.x.y('Hello world').z('How are you')
expect(m.x.y.$args[0]).toEqual(['Hello world'])
expect(m.x.y.z.$args[0]).toEqual(['How are you'])
m.message = 'cool brah'
expect(m.message).toEqual('cool brah')
m.fn = (who) => 'Hello ' + who
expect(m.fn('you!')).toEqual('Hello you!')
expect(m.fn('someone!')).toEqual('Hello someone!')
expect(m.fn.$args[0]).toEqual(['you!'])
expect(m.fn.$args[1]).toEqual(['someone!'])
This creates a mock object. All properties are unique and preserved mock objects as well. Calling the mock as a function returns a preserved mock object as well.
const m = mock()
expect(m.foo).toBe(m.foo)
expect(m()).toBe(m())
expect(m()).not.toBe(m)
This property resolves to an array containing the lists of arguments for calls to this mock.
Given the following calls:
const m = mock()
m.foo(1, 2, 3)
m.foo(4, 5, 6)
.. we would get the following array when accessing m.foo.$args
:
[
[1, 2, 3],
[4, 5, 6],
]
This property returns true for any mock object.
const m = mock()
m.$isMock === true
m.foo.isMock === true
m.bar().$isMock === true
If a mocked value is used as a promise, that's accounted for the then property returns an asynchronously resolved promise.
The promise resolves to undefined.
The good part about this is that you can use async/await and not worry about a thing!
expect(typeof mock().then).toBe('function')
const resolvedTo = await mock()
expect(resolvedTo).toBe(undefined)
Se the tests for further info. Supports catch binding as well.
If you want a promise to fail, you can set m.$throws = true
.
const m = mock()
m.$throws = true
m.foo.bar()
.then(() => done.fail('Should not succeed!')
.catch(error => done())
You can assign properties to a mock object. This is often very useful in testing.
const m = mock()
m.meaning.of.life = 42
expect(m.meaning.of.life).toBe(42)
When assigning functions as a property of a mock, you will also
have access to $args
of this function.
Note that the function will lose equality with the original function as it is proxied.
const m = mock()
const mockEncrypt = password => 'secret:' + password
m.encrypt = mockEncrypt
const encrypted = m.encrypt('my password')
// it uses the mock function
expect(encrypted).toEqual('secret:my password')
// and you have access to $args
expect(m.encrypt.$args[0]).toEqual(['my password'])
// , but it does not point to the same object anymore
expect(m.encrypt).not.toBe(mockEncrypt)
You can reassign then for testing custom thenables. You must however remember to call the received function for the promise to resolve.
const m = mock()
let called = false
m.then = function (fn) {
called = true
fn()
}
await m
expect(called).toBe(true)
const m = mock()
m.foo(1).bar(2)
expect(m.foo.bar.$args).toBe(m.foo().bar.$args)
FAQs
[](https://travis-ci.org/adrianhelvik/mock) [](https://coveralls.io/github/adrianhelvi
The npm package @adrianhelvik/mock receives a total of 5 weekly downloads. As such, @adrianhelvik/mock popularity was classified as not popular.
We found that @adrianhelvik/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.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.