![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
jasmine-mock-factory
Advanced tools
A Jasmine test util that uses a TypeScript class or an instance of a class to create a mock instance of that class.
import { SomeClass } from 'some-library';
import { MockFactory} from 'jasmine-mock-factory';
it('should pass', () => {
const mockInstance = MockFactory.create(SomeClass);
/* arrange */
mockInstance._spy.doSomething._func.and.returnValue('awesome!');
/* act */
mockInstance.doSomething(); // returns 'awesome!'
/* assert */
expect(mockInstance.doSomething).toHaveBeenCalled();
}
/* create a mock from a class*/
const mockInstance1 = MockFactory.create(RealClass);
/* create a mock from an instance*/
const mockInstance2 = MockFactory.create(realInstance);
/* access a function spy */
const spy1 = mockInstance._spy.functionName._func
/* access a getter spy */
const spy2 = mockInstance._spy.propertyName._get
/* access a setter spy */
const spy3 = mockInstance._spy.propertyName._set
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.
npm install jasmine-mock-factory --save-dev
Import the library with ES6 Module Syntax:
import { MockFactory } from 'jasmine-mock-factory'
class RealClass {
// This is a typescript class
}
...
const mockInstance = MockFactory.create(RealClass);
const realInstance: RealInterface = new RealClass();
...
const mockInstance = MockFactory.create(realInstance);
/* make sure you have included dom types from the TypeScript library */
const mockWindow = MockFactory.create(window);
const mockDocument = MockFactory.create(document);
const mockLocation = MockFactory.create(location);
MockFactory.create()
will return an object with the same interface as the real object. You can invoke functions and access properties on this object._spy
facade, where you can access and config spies on functions and properties. const mockInstance = MockFactory.create(location);
mockInstance.reload(); // use it as if it were the real window.location
let temp = mockInstance.search; // use it as if it were the real window.search
mockInstance.hash = 'myHash'; // use it as if it were the real window.hash
mockInstance._spy.reload._func; // returns the spy behind location.reload
mockInstance._spy.search._get; // returns the spy behind the getter for location.search
mockInstance._spy.hash._set; // returns the spy behind the setter for location.hash
jasmine.Spy
as the initial value. The spy cannot be overwritten and returns undefined
by default.as any
or use bracket notation. mockInstance.publicFunction(42); // the spy behind it is invoked with 42
(mockInstance as any).privateFunction(42);
mockInstance['privateFunction'](42); // equivalent
_spy
facade.mockInstance._spy.functionName._func
. /* stubbing a public function */
mockInstance._spy.publicFunction._func.and.returnValue(42);
(mockInstance.publicFunction as jasmine.Spy).and.returnValue(42); // equivalent, but not recommented because it requires casting
/* stubbing a private function */
mockInstance._spy.privateFunction._func.and.returnValue(42);
((mockInstance as any).privateFunction as jasmine.Spy).and.returnValue(42); // equivalent, but not recommented because it requires casting twice
undefined
as the initial value. The value can be overwritten with anything.as any
or use bracket notation.as any
. The bracket notation won't work. /* persist modification */
mockInstance.publicProperty = 42;
let temp = mockInstance.publicProperty; // temp = 42;
/* access readonly property */
mockInstance.readonlyProperty = 42; // typescript compiler error
(mockInstance as any).readonlyProperty = 42; // no problem
mockInstance['readonlyProperty'] = 42; // typescript compiler error
/* access private property */
(mockInstance as any).privateProperty = 'foo';
mockInstance['privateProperty'] = 'foo'; // equivalent
mockInstance._spy.propertyName._get
.mockInstance._spy.propertyName._set
.expect(mockInstance.someProperty).toBe(...)
will trigger mockInstance._spy.someProperty._get
. Design the sequence of your assertions carefully to avoid shooting yourself in the foot. /* assert getter calls */
let temp = mockInstance.publicProperty;
expect(mockInstance._spy.publicProperty._get).toHaveBeenCalled();
/* assert setter calls on a public property */
mockInstance.publicProperty = 42;
expect(mockInstance._spy.publicProperty._set).toHaveBeenCalledWith(42);
/* customize setter */
expect(mockInstance.publicProperty).toBe(42); // pass. setter hasn't been customized
mockInstance._spy.publicProperty._set.and.callFake(() => { /* noop */});
mockInstance.publicProperty = 100;
expect(mockInstance.publicProperty).toBe(100); // fail. expect 42 to be 100. setter was customized
/* assert setter calls on a private property */
mockInstance['privateProperty'] = 42;
expect(mockInstance._spy.privateProperty._set).toHaveBeenCalledWith(42);
/* customize getter */
expect(mockInstance['privateProperty']).toBe(42); // pass. getter hasn't been customized
mockInstance._spy.privateProperty._get.and.returnValue(100);
mockInstance['privateProperty'] = 42;
expect(mockInstance['privateProperty']).toBe(42); // fail, expect 100 to be 42. getter was customzied
This project is built with Angular CLI
Run ng test
to execute the unit tests via Karma.
2.0.0
_spy
facade.FAQs
A Jasmine helper for creating mocked classes
The npm package jasmine-mock-factory receives a total of 284 weekly downloads. As such, jasmine-mock-factory popularity was classified as not popular.
We found that jasmine-mock-factory 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.