
Product
Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
@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)
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)
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 23 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.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
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.