chuanqisun / jasmine-mock-factory Goto Github PK
View Code? Open in Web Editor NEWA Jasmine helper for creating mocked classes
License: MIT License
A Jasmine helper for creating mocked classes
License: MIT License
export interface SpiedMember {
_func?: jasmine.Spy;
_get?: jasmine.Spy;
_set?: jasmine.Spy;
}
Can these properties be defined as non-nullable?
When strictNullChecks
is set to true, following recommended usage produce error:
service._spy.toDataObject._func.and.returnValue({ id: null }); // Object is possible undefined
to avoid this error I must use following constructions, which looks dirty.
service._spy.toDataObject._func && service._spy.toDataObject._func.and.returnValue
TypeScript 2.9 made changes to keys so that they can be of types string | number | symbol, instead of just string. In src/lib/index.ts lines 82, 85, and 92, propertyName: keyof T
is being passed into ensureProperty
which takes a string, causing an error to occur when compiling with TypeScript 2.9.
The library currently depends on Angular Cli 1.1.0 to run all the jasmine tests. This is a unnecessary dependency that bloats the library by a lot. I'm adding this issue to collect ideas to track the work for modernizing the test dependencies. Ideally, typescript + jasmine + some light-weight test runner is all we need.
var AppService = /** @class */ (function () {
function AppService() {
this.appInitialized$ = new BehaviorSubject(false);
}
Object.defineProperty(AppService.prototype, "isAppInitialized", {
get: function () {
return this.appInitialized$.getValue();
},
enumerable: true,
configurable: true
});
const appService = MockFactory.create(AppService);
appService ._spy.isAppInitialized._get.and.returnValue(true);
TypeError: Cannot read property 'getValue' of undefined
// if target is property
if (typeof this.prototype[propertyName] !== 'function') { // <--- error here
// we add getters and setters to all properties to make the read and write spy-able
var descriptor = {
get: /* istanbul ignore next: Can't reach. spyOnProperty() requires its presence to install spies */ function () { },
set: /* istanbul ignore next: Can't reach. spyOnProperty() requires its presence to install spies */ function (value) { },
enumerable: true,
configurable: true,
};
Why Object.getOwnPropertyDescriptors(this.prototype)[propName]
is not used?
Unfortunately, this helper does not work for jest.
But there are few place to fix.
const getterSpy = spyOnProperty(this.stub, propertyName, 'get').and.callFake(() => this.spy[propertyName]._value);
const setterSpy = spyOnProperty(this.stub, propertyName, 'set').and.callFake(value => this.spy[propertyName]._value = value);
// =>
const getterSpy = jest
.spyOn(this.stub, propertyName, 'get')
.mockImplementation(() => this.spy[propertyName]._value);
const setterSpy = jest
.spyOn(this.stub, propertyName, 'set')
.mockImplementation((value) => (this.spy[propertyName]._value = value));
const spy = jasmine.createSpy(propertyName);
// =>
const spy = jest.fn().mockName(propertyName);
Maybe possible detect environment in runtime and call appropriate methods? Or it is better to create separate module for jest...
Hey nice project!
I was working on something similar was looking for something open source and found this. I found that you can tighten up the typing by defining an intersection type return on the create method. eg
export class MockFactory {
/**
* create a mock object that has the identical interface with the class you passed in
*/
public static create<T extends object>(blueprint: Type<T> | T): Proxy<T> & {[M in keyof T]: jasmine.Spy} {
It allows you to do
mockClass1Instance = MockFactory.create(Class1);
mockClass1Instance.publicMethod.and.returnValue(999);
Without having to type cast mockClass1Instance.publicMethod as jasmine.Spy
Hope it helps!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.