jasmine-auto-spies
Advanced tools
Comparing version 7.0.0 to 8.0.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [8.0.0](https://github.com/hirezio/auto-spies/compare/jasmine-auto-spies@7.0.0...jasmine-auto-spies@8.0.0) (2023-06-03) | ||
### Bug Fixes | ||
- **types:** typesafety for mocked objects/functions ([d1ffbac](https://github.com/hirezio/auto-spies/commit/d1ffbac0df82f9c54081e8ef5acc3b4bf0b288c5)), closes [#51](https://github.com/hirezio/auto-spies/issues/51) | ||
### BREAKING CHANGES | ||
- **types:** "mockReturnValue" for a function that returns a number no longer accepts something | ||
that's not a number | ||
# [7.0.0](https://github.com/hirezio/auto-spies/compare/jasmine-auto-spies@6.9.9...jasmine-auto-spies@7.0.0) (2023-03-28) | ||
@@ -8,0 +19,0 @@ |
@@ -1,4 +0,3 @@ | ||
/// <reference types="jasmine" /> | ||
import { AddSpyMethodsByReturnTypes } from '.'; | ||
import { Func } from '@hirez_io/auto-spies-core'; | ||
export declare function createFunctionSpy<FunctionType extends Func>(name: string): AddSpyMethodsByReturnTypes<FunctionType, jasmine.Spy>; | ||
export declare function createFunctionSpy<FunctionType extends Func>(name: string): AddSpyMethodsByReturnTypes<FunctionType>; |
/// <reference types="jasmine" /> | ||
import { Observable } from 'rxjs'; | ||
import { AddObservableSpyMethods, AddPromiseSpyMethods, Func, AddAccessorsSpies, CreateSyncAutoSpy, CreateObservableAutoSpy, CreatePromiseAutoSpy } from '@hirez_io/auto-spies-core'; | ||
export type Spy<ClassToSpyOn> = AddAutoSpies<ClassToSpyOn, jasmine.Spy> & AddAccessorsSpies<ClassToSpyOn, jasmine.Spy>; | ||
type AddAutoSpies<ClassToSpyOn, LibSpecificFunctionSpy> = { | ||
[Key in keyof ClassToSpyOn]: ClassToSpyOn[Key] extends Func ? AddSpyMethodsByReturnTypes<ClassToSpyOn[Key], LibSpecificFunctionSpy> : ClassToSpyOn[Key] extends Observable<infer ObservableReturnType> ? ClassToSpyOn[Key] & AddObservableSpyMethods<ObservableReturnType> : ClassToSpyOn[Key]; | ||
export type Spy<ClassToSpyOn> = AddJasmineAutoSpies<ClassToSpyOn> & AddAccessorsSpies<ClassToSpyOn, jasmine.Spy>; | ||
type AddJasmineAutoSpies<ClassToSpyOn> = { | ||
[Key in keyof ClassToSpyOn]: ClassToSpyOn[Key] extends Func ? AddSpyMethodsByReturnTypes<ClassToSpyOn[Key]> : ClassToSpyOn[Key] extends Observable<infer ObservableReturnType> ? ClassToSpyOn[Key] & AddObservableSpyMethods<ObservableReturnType> : ClassToSpyOn[Key]; | ||
}; | ||
export type AddSpyMethodsByReturnTypes<Method extends Func, LibSpecificFunctionSpy> = Method & (Method extends (...args: any[]) => infer ReturnType ? ReturnType extends Promise<infer PromiseReturnType> ? CreatePromiseAutoSpy<LibSpecificFunctionSpy, AddPromisesToJasmineFunctionSpy<PromiseReturnType>, PromiseReturnType> : ReturnType extends Observable<infer ObservableReturnType> ? CreateObservableAutoSpy<LibSpecificFunctionSpy, AddObservablesToJasmineFunctionSpy<ObservableReturnType>, ObservableReturnType> : CreateSyncAutoSpy<Method, LibSpecificFunctionSpy, AddCalledWithToJasmineFunctionSpy> : never); | ||
export type AddSpyMethodsByReturnTypes<Method extends Func> = Method & (Method extends (...args: any[]) => infer ReturnType ? ReturnType extends Promise<infer PromiseReturnType> ? CreatePromiseAutoSpy<jasmine.Spy<Method>, AddPromisesToJasmineFunctionSpy<PromiseReturnType>, Method> : ReturnType extends Observable<infer ObservableReturnType> ? CreateObservableAutoSpy<jasmine.Spy<Method>, AddObservablesToJasmineFunctionSpy<ObservableReturnType>, Method> : CreateSyncAutoSpy<Method, jasmine.Spy<Method>, AddCalledWithToJasmineFunctionSpy<Method>> : never); | ||
type AddPromisesToJasmineFunctionSpy<PromiseReturnType> = { | ||
@@ -15,10 +15,10 @@ and: AddPromiseSpyMethods<PromiseReturnType>; | ||
}; | ||
export interface AddCalledWithToJasmineFunctionSpy { | ||
calledWith(...args: any[]): { | ||
returnValue: (value: any) => void; | ||
export interface AddCalledWithToJasmineFunctionSpy<Method extends Func> { | ||
calledWith(...args: Parameters<Method>): { | ||
returnValue: (value: ReturnType<Method>) => void; | ||
}; | ||
mustBeCalledWith(...args: any[]): { | ||
returnValue: (value: any) => void; | ||
mustBeCalledWith(...args: Parameters<Method>): { | ||
returnValue: (value: ReturnType<Method>) => void; | ||
}; | ||
} | ||
export {}; |
@@ -7,7 +7,8 @@ import { Observable, Subject } from 'rxjs'; | ||
get observablePropAsGetter(): Observable<any>; | ||
getSyncValue(): string; | ||
getPromise(): Promise<any>; | ||
getObservable(): Observable<any>; | ||
getSyncValue(..._args: any[]): string; | ||
getNullableSyncValue(..._args: any[]): string | null; | ||
getPromise(..._args: any[]): Promise<any>; | ||
getObservable(..._args: any[]): Observable<any>; | ||
getSubject(): Subject<any>; | ||
arrowMethod: () => void; | ||
arrowMethod: () => string; | ||
} | ||
@@ -14,0 +15,0 @@ export declare class FakeChildClass extends FakeClass { |
@@ -20,2 +20,3 @@ "use strict"; | ||
/* eslint-disable @typescript-eslint/no-empty-function */ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
var rxjs_1 = require("rxjs"); | ||
@@ -27,3 +28,3 @@ var FakeClass = /** @class */ (function () { | ||
this.subjectProp = new rxjs_1.Subject(); | ||
this.arrowMethod = function () { }; | ||
this.arrowMethod = function () { return ''; }; | ||
} | ||
@@ -38,8 +39,27 @@ Object.defineProperty(FakeClass.prototype, "observablePropAsGetter", { | ||
FakeClass.prototype.getSyncValue = function () { | ||
var _args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
_args[_i] = arguments[_i]; | ||
} | ||
return ''; | ||
}; | ||
FakeClass.prototype.getNullableSyncValue = function () { | ||
var _args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
_args[_i] = arguments[_i]; | ||
} | ||
return ''; | ||
}; | ||
FakeClass.prototype.getPromise = function () { | ||
var _args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
_args[_i] = arguments[_i]; | ||
} | ||
return Promise.resolve(); | ||
}; | ||
FakeClass.prototype.getObservable = function () { | ||
var _args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
_args[_i] = arguments[_i]; | ||
} | ||
return (0, rxjs_1.of)(); | ||
@@ -46,0 +66,0 @@ }; |
{ | ||
"name": "jasmine-auto-spies", | ||
"version": "7.0.0", | ||
"version": "8.0.0", | ||
"author": { | ||
@@ -45,3 +45,3 @@ "name": "Shai Reznik", | ||
"dependencies": { | ||
"@hirez_io/auto-spies-core": "2.0.0" | ||
"@hirez_io/auto-spies-core": "3.0.0" | ||
}, | ||
@@ -53,3 +53,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "d049be48f0a5b7fef3960278b0ed67c5523707c6" | ||
"gitHead": "e433d8d3d012c8c38dfec53b13487214b47322fa" | ||
} |
@@ -8,3 +8,3 @@ import { AddSpyMethodsByReturnTypes } from '.'; | ||
name: string | ||
): AddSpyMethodsByReturnTypes<FunctionType, jasmine.Spy> { | ||
): AddSpyMethodsByReturnTypes<FunctionType> { | ||
return createFunctionAutoSpy( | ||
@@ -11,0 +11,0 @@ name, |
@@ -13,9 +13,9 @@ /// <reference types="jasmine" /> | ||
export type Spy<ClassToSpyOn> = AddAutoSpies<ClassToSpyOn, jasmine.Spy> & | ||
export type Spy<ClassToSpyOn> = AddJasmineAutoSpies<ClassToSpyOn> & | ||
AddAccessorsSpies<ClassToSpyOn, jasmine.Spy>; | ||
type AddAutoSpies<ClassToSpyOn, LibSpecificFunctionSpy> = { | ||
type AddJasmineAutoSpies<ClassToSpyOn> = { | ||
[Key in keyof ClassToSpyOn /* | ||
if it's a method */]: ClassToSpyOn[Key] extends Func | ||
? AddSpyMethodsByReturnTypes<ClassToSpyOn[Key], LibSpecificFunctionSpy> | ||
? AddSpyMethodsByReturnTypes<ClassToSpyOn[Key]> | ||
: // if it's a property of type Observable | ||
@@ -29,6 +29,3 @@ ClassToSpyOn[Key] extends Observable<infer ObservableReturnType> | ||
// Wrap the return type of the given function type with the appropriate spy methods | ||
export type AddSpyMethodsByReturnTypes< | ||
Method extends Func, | ||
LibSpecificFunctionSpy | ||
> = Method & | ||
export type AddSpyMethodsByReturnTypes<Method extends Func> = Method & | ||
(Method extends (...args: any[]) => infer ReturnType | ||
@@ -38,5 +35,5 @@ ? // returns a Promise | ||
? CreatePromiseAutoSpy< | ||
LibSpecificFunctionSpy, | ||
jasmine.Spy<Method>, | ||
AddPromisesToJasmineFunctionSpy<PromiseReturnType>, | ||
PromiseReturnType | ||
Method | ||
> | ||
@@ -46,5 +43,5 @@ : // returns an Observable | ||
? CreateObservableAutoSpy< | ||
LibSpecificFunctionSpy, | ||
jasmine.Spy<Method>, | ||
AddObservablesToJasmineFunctionSpy<ObservableReturnType>, | ||
ObservableReturnType | ||
Method | ||
> | ||
@@ -54,4 +51,4 @@ : // for any other type | ||
Method, | ||
LibSpecificFunctionSpy, | ||
AddCalledWithToJasmineFunctionSpy | ||
jasmine.Spy<Method>, | ||
AddCalledWithToJasmineFunctionSpy<Method> | ||
> | ||
@@ -68,13 +65,9 @@ : never); | ||
export interface AddCalledWithToJasmineFunctionSpy { | ||
calledWith( | ||
...args: any[] | ||
): { | ||
returnValue: (value: any) => void; | ||
export interface AddCalledWithToJasmineFunctionSpy<Method extends Func> { | ||
calledWith(...args: Parameters<Method>): { | ||
returnValue: (value: ReturnType<Method>) => void; | ||
}; | ||
mustBeCalledWith( | ||
...args: any[] | ||
): { | ||
returnValue: (value: any) => void; | ||
mustBeCalledWith(...args: Parameters<Method>): { | ||
returnValue: (value: ReturnType<Method>) => void; | ||
}; | ||
} |
@@ -166,7 +166,7 @@ import { createSpyFromClass } from '../create-spy-from-class'; | ||
fakeArgs = [1, { a: 2 }]; | ||
fakeClassSpy.getSyncValue.calledWith(...fakeArgs).returnValue(null); | ||
fakeClassSpy.getNullableSyncValue.calledWith(...fakeArgs).returnValue(null); | ||
}); | ||
When(() => { | ||
actualResult = fakeClassSpy.getSyncValue(...fakeArgs); | ||
actualResult = fakeClassSpy.getNullableSyncValue(...fakeArgs); | ||
}); | ||
@@ -173,0 +173,0 @@ |
/* eslint-disable @typescript-eslint/no-empty-function */ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { Observable, of, Subject } from 'rxjs'; | ||
@@ -13,11 +14,15 @@ | ||
public getSyncValue(): string { | ||
public getSyncValue(..._args: any[]): string { | ||
return ''; | ||
} | ||
public getPromise(): Promise<any> { | ||
public getNullableSyncValue(..._args: any[]): string | null { | ||
return ''; | ||
} | ||
public getPromise(..._args: any[]): Promise<any> { | ||
return Promise.resolve(); | ||
} | ||
public getObservable(): Observable<any> { | ||
public getObservable(..._args: any[]): Observable<any> { | ||
return of(); | ||
@@ -30,3 +35,3 @@ } | ||
public arrowMethod: () => void = () => {}; | ||
public arrowMethod: () => string = () => ''; | ||
} | ||
@@ -33,0 +38,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
168088
1776
+ Added@hirez_io/auto-spies-core@3.0.0(transitive)
- Removed@hirez_io/auto-spies-core@2.0.0(transitive)