Jasmine Mock Factory
A Jasmine test util that uses a TypeScript class or an instance of a class to create a mock instance of that class.
Quick Start
import { SomeClass } from 'some-library';
import { MockFactory} from 'jasmine-mock-factory';
it('should pass', () => {
const mockInstance = MockFactory.create(SomeClass);
mockInstance._spy.doSomething._func.and.returnValue('awesome!');
mockInstance.doSomething();
expect(mockInstance.doSomething).toHaveBeenCalled();
}
Quick reference
const mockInstance1 = MockFactory.create(RealClass);
const mockInstance2 = MockFactory.create(realInstance);
const spy1 = mockInstance._spy.functionName._func
const spy2 = mockInstance._spy.propertyName._get
const spy3 = mockInstance._spy.propertyName._set
Prerequisite
This util is built with and for Jasmine test framework. Basic understanding of Jasmine is assumed.
This util requires ES6 Proxy and only contains *.ts
files that must be compiled with a TypeScript compiler.
Usage
Install
npm install jasmine-mock-factory --save-dev
Import
Import the library with ES6 Module Syntax:
import { MockFactory } from 'jasmine-mock-factory'
Creating a mock
From a TypeScript class
class RealClass {
}
...
const mockInstance = MockFactory.create(RealClass);
From an instance of a class
const realInstance: RealInterface = new RealClass();
...
const mockInstance = MockFactory.create(realInstance);
From window objects
const mockWindow = MockFactory.create(window);
const mockDocument = MockFactory.create(document);
const mockLocation = MockFactory.create(location);
Using a mock
MockFactory.create()
will return an object with the same interface as the real object. You can invoke functions and access properties on this object.- In addition, the mock object provides a
_spy
facade, where you can access and config spies on functions and properties.
const mockInstance = MockFactory.create(location);
mockInstance.reload();
let temp = mockInstance.search;
mockInstance.hash = 'myHash';
mockInstance._spy.reload._func;
mockInstance._spy.search._get;
mockInstance._spy.hash._set;
Invoking functions
- All functions will have a
jasmine.Spy
as the initial value. The spy cannot be overwritten and returns undefined
by default. - To access protected and private functions, cast the mockInstance
as any
or use bracket notation.
mockInstance.publicFunction(42);
(mockInstance as any).privateFunction(42);
mockInstance['privateFunction'](42);
Spying/stubbing functions
- You can change return values of functions or assert their calls by accessing them directly or through the
_spy
facade. - Access a function spy on
mockInstance._spy.functionName._func
.
mockInstance._spy.publicFunction._func.and.returnValue(42);
(mockInstance.publicFunction as jasmine.Spy).and.returnValue(42);
mockInstance._spy.privateFunction._func.and.returnValue(42);
((mockInstance as any).privateFunction as jasmine.Spy).and.returnValue(42);
Accessing properties
- All properties have
undefined
as the initial value. The value can be overwritten with anything. - You have read and write access to any property, even if they were readonly in the real object.
- To read or write a protected or private property, cast the mockInstance
as any
or use bracket notation. - To write a readonly property, cast the mockInstance
as any
. The bracket notation won't work. - By default, modification to the properties will persist, even if a getter or setter exists in the real object.
mockInstance.publicProperty = 42;
let temp = mockInstance.publicProperty;
mockInstance.readonlyProperty = 42;
(mockInstance as any).readonlyProperty = 42;
mockInstance['readonlyProperty'] = 42;
(mockInstance as any).privateProperty = 'foo';
mockInstance['privateProperty'] = 'foo';
Spying/stubbing getters and setters
- All properties have spies on the getter and setter, even if the getter and setter don't exist in the real object.
- Access a getter spy on
mockInstance._spy.propertyName._get
. - Access a setter spy on
mockInstance._spy.propertyName._set
. - NOTE: modification to the properties will not persist after getter or setter spies are customized
- NOTE:
expect(mockInstance.someProperty).toBe(...)
will trigger mockInstance._spy.someProperty._get
. Design the sequence of your assertions carefully to avoid shooting yourself in the foot.
let temp = mockInstance.publicProperty;
expect(mockInstance._spy.publicProperty._get).toHaveBeenCalled();
mockInstance.publicProperty = 42;
expect(mockInstance._spy.publicProperty._set).toHaveBeenCalledWith(42);
expect(mockInstance.publicProperty).toBe(42);
mockInstance._spy.publicProperty._set.and.callFake(() => { });
mockInstance.publicProperty = 100;
expect(mockInstance.publicProperty).toBe(100);
mockInstance['privateProperty'] = 42;
expect(mockInstance._spy.privateProperty._set).toHaveBeenCalledWith(42);
expect(mockInstance['privateProperty']).toBe(42);
mockInstance._spy.privateProperty._get.and.returnValue(100);
mockInstance['privateProperty'] = 42;
expect(mockInstance['privateProperty']).toBe(42);
Develope
This project is built with Angular CLI
Running unit tests
Run ng test
to execute the unit tests via Karma.