Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
flow
support out-of-the-boxspy4js provides a stand-alone spy framework. It is decoupled by any dependencies and other assertion frameworks.
spy4js exports only one object called Spy
. The spy instances
are treated as class instances and come with a lot of useful features. See below for more.
yarn add --dev spy4js
npm install --save-dev spy4js
A spy instance can be initialized differently.
import {Spy} from 'spy4js';
// initialize directly
const spy1 = new Spy();
// initialize directly and supply an identifier for debugging purpose (default: 'the spy')
const spy2 = new Spy('special spy for me');
// initialize by mocking another objects attribute (usually this attribute is a function)
const someObject1 = new Date(2017, 1, 15);
const spy3 = Spy.on(someObject1, 'toJSON');
// (spy name will be accordingly: 'the spy on \'toJSON\'')
// initialize many by mocking another objects attributes
const someObject2 = new Date(2017, 1, 15);
const [spy4, spy5, spy6] = Spy.onMany(someObject2, 'toJSON', 'toString', 'getDate');
Any spy instance can be configured by overriding the default configuration. For example if you want to configure all spies not to favor own "equals" implementations.
Spy.configure({useOwnEquals: false});
You may apply additional behaviour to every spy. The valid operations here are:
configure
(some external libraries may use own "equals" implementations in an unexpected way)calls
(does make the spy call the provided functions sequentially)returns
(does make the spy return the provided params sequentially)throws
(does make the spy throw an error when called)transparent
(does make the spy call the original method of a mocked object)transparentAfter
(does make the spy call the original method of a mocked object after a certain amount of made calls)reset
(resets the registered calls which were already made)restore
(does make the spy restore the mocked object)All those methods on a spy are designed in a builder pattern. So you may chain any of these configurations. But be aware that some behaviours override existing behaviours.
const spy = Spy.on(someObject, 'someMethod');
// configure it to use NOT own "equals" implementations
spy.configure({useOwnEquals: false});
// make it call any functions
spy.calls(func1, func2, func3);
someObject.someMethod(arg); // returns func1(arg)
someObject.someMethod(arg1, arg2); // returns func2(arg1, arg2)
someObject.someMethod(arg); // returns func3(arg)
someObject.someMethod(arg1, arg2, arg3); // returns func3(arg1, arg2, arg3) // sticks to the last
// make it return any values
spy.returns(value1, value2);
someObject.someMethod(arg); // returns value1
someObject.someMethod(arg1, arg2); // returns value2
someObject.someMethod(arg); // returns value2 // sticks to the last
// make it throw any message (the message is optional)
spy.throws('throw this');
someObject.someMethod(arg); // throws new Error('throw this')
// make it return always the current date and transparentAfter 2 calls
spy.calls(() => new Date()).transparentAfter(2);
someObject.someMethod(arg); // returns new Date()
someObject.someMethod(arg1, arg2); // returns new(er) Date()
someObject.someMethod(arg); // returns someObject.someMethod(arg) // sticks to this behaviour
// make it immediatly transparent
spy.transparent();
// make it reset
spy.reset();
// make it restore
spy.restore(); // other than "transparent" does not control input and output of the mocked function anymore
Even as important are the facts, we want to display:
wasCalled
(does display that the spy was called a specifiable amount of times)wasNotCalled
(does display that the spy was never called)wasCalledWith
(does display that the spy was called at least once like with the provided params)wasNotCalledWith
(does display that the spy was never like with the provided params)hasCallHistory
(does display that the spy was called with the following params in the given order)Those methods on a spy display facts. Facts have to be true, otherwise they will throw an Exception, which displays in a formatted debug message why the given fact was a lie. By writing those facts in your tests, a big refactoring loses its scare.
const spy = new Spy();
spy.wasNotCalled();
// in fact: you never want to call a spy directly for any purpose
// -> therefore using flow this line would complain
spy([1, 'test', {attr: [4]}]);
spy.wasCalled(); // called at least once
spy.wasCalled(1); // called exactly once
spy('with this text');
spy.wasCalled(2); // called exactly 2 times
// the spy was called at least once with equal params
spy.wasCalledWith([1, 'test', {attr: [4]}]);
// the spy was not called with those params
spy.wasNotCalledWith([1, 'test', {attr: [3]}]);
// the spy was called twice with the following params and in same order
spy.hasCallHistory([ [ [1, 'test', {attr: [4]}] ], [ 'with this text' ] ]);
There is one static method that does restore all existing spies in all tests. This is extremely useful to clean up all still existing mocks and also a very comfortable to this automatically after every test (like in an "afterEach").
restoreAll
(does restore every existing spy)Spy.restoreAll();
Hint:
To integrate as default that all spies get restored after each test run,
you can integrate the following snippet to replace the default describe.
For those of you working with
create-react-app
may include the snippet in the src/setupTests.js
.
import { Spy } from 'spy4js';
const oldDescribe = describe;
window.describe = (string, func) => {
oldDescribe(string, () => {
afterEach(() => {
Spy.restoreAll();
});
return func();
});
};
And also sometimes it is necessary to have access to some of the call arguments with which the spy was called.
getCallArguments
(returns all call arguments for a specified call in an array)getCallArgument
(same as getCallArguments, but returns only a single element of the array)getCallCount
(returns the number of made calls)const spy = new Spy();
// make some calls
spy('string', 1);
spy([1, 2, 3]);
spy();
spy(null);
spy.getCallArguments(/* default = 0 */); // returns ['string', 1]
spy.getCallArgument(/* defaults = (0, 0) */); // returns 'string'
spy.getCallArgument(0, 1); // returns 1
spy.getCallArguments(1); // returns [[1, 2, 3]]
spy.getCallArgument(1); // returns [1, 2, 3]
spy.getCallArguments(2); // returns []
spy.getCallArgument(2); // returns undefined
spy.getCallArguments(3); // returns [null]
spy.getCallArgument(3); // returns null
spy.getCallArguments(4); // throws Exception because less calls were made
spy.getCallArgument(4); // throws same Exception
The last method is showCallArguments
. It is mostly used internally to improve the
debug messages, but can be while you are in a console.log-mania.
Spy(spyName:string = 'the spy') => Spy
The returned Spy instance has his own name-attribute (only) for debugging purpose.
Spy.configure(config:{useOwnEquals?:boolean}) => void
Using this function you may edit the default behaviour of every spy instance. The only configuration possibility for now is "useOwnEquals". See configure for more details.
Spy.on(object:Object, methodName:string) => Spy
Initializing a spy on an object, simply replaces the original function by a spy and stores the necessary information to be able to restore the mocked method.
If the attribute was already spied or is not a function, the Spy will throw an exception to avoid unexpected behaviour. You never want to spy other attributes than functions and for no purpose a spy should ever be spied.
Spy.onMany(object:Object, ...methodNames:Array<string>) => Array<Spy>
Initializing as many spies as required for one and the same object. Same as calling
Spy.on
for each method name.
Spy.restoreAll() => Array<Spy>
Does restore all mocked objects to their original state. See restore for further information.
Spy.IGNORE = $Internal Symbol$
This object can be passed anywhere where you want the "wasCalledWith" to ignore that object or value for comparison.
spy({prop: 'value', other: 13}, 12);
spy.wasCalledWith(Spy.IGNORE, 12);
spy.wasCalledWith({prop: Spy.IGNORE, other: 13}, 12);
spy.configure(config:{useOwnEquals?: boolean, persistent?: boolean}) => (this) Spy
With configure
the spy can be configured. One configuration possibility
is to ignore any equals
methods while comparing objects. There might be libraries which
come with those methods, but do not support ES6 classes or anything else. By default this
configuration is set to favor own equals
implementations while comparing objects.
Another possible configuration is to make the spy persist while other spies have to restore
when "restoreAll" was called. This spy can ONLY RESTORE the mocked object when
you configure it back to be NOT PERSISTENT. This configuration can only be applied to mocking
spies. For Spies created with new Spy()
this configuration will throw an exception.
spy.calls(...functions:Array<Function>) => (this) Spy
The provided functions will be called sequentially in order when the spy will be called.
Meaning spy.calls(func1, func2, func3)
will call first func1
then func2
and the rest
of the time func3
.
spy.returns(...args:Array<any>) => (this) Spy
The provided arguments will be returned sequentially in order when the spy will be called.
Meaning spy.returns(arg1, arg2, arg3)
will return first arg1
then arg2
and the rest
of the time arg3
.
spy.throws(message:?string) => (this) Spy
Perform this on a spy to make it throw an error when called. The error message can be provided but a default is also implemented.
spy.reset() => (this) Spy
Does reset the registered calls on that spy.
spy.restore() => (this) Spy
Restores the spied object, if existing, to its original state. The spy won't lose any other information. So it is still aware of made calls, can be plugged anywhere else and can still be called anywhere else. But it loses all references to the spied object.
If the spy was configured to be persistent this method will throw an error.
spy.transparent() => (this) Spy
Can be useful with spies on objects. It does make the spy behave like not existing. So the original function of the "mocked" object will be called, but the spy does remember the call information.
spy.transparentAfter(callCount:number) => (this) Spy
Works like transparent but the spy will get transparent after called as
often as specified. Meaning spy.transparentAfter(num)
will not be transparent on the first
num
calls.
spy.wasCalled(callCount:number = 0) => (fact) void
This call does display a fact. So if the spy is violating the fact, it is told to throw an error. The provided argument does represent the registered calls on that spy.
spy.wasNotCalled() => (fact) void
This fact displays that the spy was never called. Directly after the spy was reseted, this fact will be given.
spy.wasCalledWith(...args:Array<any>) => (fact) void
This fact displays that the spy was called at least once with equal arguments.
The equality check is a deep equality check, which (by default) does consider own "equals" implementations.
By supplying Spy.IGNORE
anywhere inside the expected call arguments, you
can avoid that the comparison is further executed. See Spy.IGNORE for further information and examples.
The deep equality check does also recursively iterate to the first difference found and is able to return a string which contains valuable information about the first found difference.
If any difference was detected. The fact is broken and a helpful error message will be displayed. If using monospaced consoles for the output which do support new lines, there will be really neat output. For examples see showCallArguments
spy.wasNotCalledWith(...args:Array<any>) => (fact) void
This fact displays simply the opposite of wasCalledWith.
spy.hasCallHistory(callHistory:Array<Array<any>>) => (fact) void
Works similiar to wasCalledWith but instead matches each call one by one in correct order and correct call count.
spy.getCallArguments(callNr:number = 0) => Array<any>
Returns the call arguments that were registered on the given call. Meaning
spy.getCallArguments(num)
does return the (num + 1)'th call arguments.
Throws an exception if the provided (callNr
- 1) is bigger than the made calls.
spy.getCallArgument(callNr:number = 0, argNr:number = 0) => any
Same as getCallArguments but returns only a single entry out
of the array of arguments. Most useful in situations where exactly one call param is expected.
If argNr
is given, it returns the (argNr + 1)'th argument of the call.
spy.getCallCount() => number
This method simply returns the number of made calls on the spy.
spy.showCallArguments(additionalInformation:Array<string> = []) => string
This primarily internally used method is responsible for returning formatted informative debug messages when facts are broken. Let's do an example:
const spy = new Spy('my awesome spy');
spy(42, 'test', {attr1: [1, 2, new Date(2017, 1, 20)], attr2: 1337});
spy(42, 'test', {attr1: [0, 2, new Date(2017, 1, 20)], attr2: 1336});
spy(42, 'test', {attr1: [1, 2, new Date(2017, 1, 21)], attr2: 1336});
spy(42, 'tes', {attr1: [1, 2, new Date(2017, 1, 20)], attr2: 1336});
spy(42, 'test');
The following broken fact...
spy.wasCalledWith(42, 'test', {attr1: [1, 2, new Date(2017, 1, 20)], attr2: 1336});
...would produce the following error output:
Error:
my awesome spy was considered to be called with the following arguments:
--> [42, "test", {attr1: [1, 2, >Date:1487545200000<], attr2: 1336}]
Actually there were:
call 0: [42, "test", {attr1: [1, 2, >Date:1487545200000<], attr2: 1337}]
--> 2 / attr2 / different number
call 1: [42, "test", {attr1: [0, 2, >Date:1487545200000<], attr2: 1336}]
--> 2 / attr1 / 0 / different number
call 2: [42, "test", {attr1: [1, 2, >Date:1487631600000<], attr2: 1336}]
--> 2 / attr1 / 2 / different date
call 3: [42, "tes", {attr1: [1, 2, >Date:1487545200000<], attr2: 1336}]
--> 1 / different string
call 4: [42, "test"]
--> 2 / one was undefined
There you can see that the arguments of the fact (displayed above all others) does not match any of the call arguments on the 5 made calls.
For each call we display additional error information (the first found difference).
If the additional information begins with a -->
there was made a deep equality.
If you would travers with the displayed keys you would be directed to those objects which differ.
In this example the arguments differ for call 0
in -->
the third argument (2
) and
its attribute attr2
because there was a different number.
While recursively traversing down in the deep equality check, the object keys will be reported.
Meaning that 2
is representing the index of the array. So for example if you want to grep the
different objects you could:
const callArgs = spy.getCallArguments(0/* for the 0'th call above*/);
const differentNumber = callArgs[2]['attr2'];
[1.6.0] - 2018-07-08
serialize-as-code
FAQs
Smart, compact and powerful spy test framework
The npm package spy4js receives a total of 2,262 weekly downloads. As such, spy4js popularity was classified as popular.
We found that spy4js demonstrated a healthy version release cadence and project activity because the last version was released less than 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
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.