🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

jasmine-auto-spies

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jasmine-auto-spies - npm Package Compare versions

Comparing version

to
5.0.0

2

package.json
{
"name": "jasmine-auto-spies",
"version": "4.1.2",
"version": "5.0.0",
"repository": {

@@ -5,0 +5,0 @@ "type": "git",

@@ -203,9 +203,6 @@ # jasmine-auto-spies

### Use `throwOnMismatch()` to turn a conditional stub into a mock
### Use `mustBeCalledWith()` to create a mock instead of a stub
```ts
myServiceSpy.getProducts
.calledWith(1)
.returnValue(true)
.throwOnMismatch();
myServiceSpy.getProducts.mustBeCalledWith(1).returnValue(true);
```

@@ -212,0 +209,0 @@

@@ -56,6 +56,9 @@ "use strict";

var calledWithObject = {
calledWithMethodWasCalled: false,
shouldThrow: false,
calledWithMap: new Map()
wasCalled: false,
argsToValuesMap: new Map()
};
var mustBeCalledWithObject = {
wasCalled: false,
argsToValuesMap: new Map()
};
var valueContainer = {

@@ -71,3 +74,3 @@ value: undefined

}
return spyFunctionCallFakeImplementation(calledWithObject, valueContainer, actualArgs);
return spyFunctionImplementation(calledWithObject, mustBeCalledWithObject, valueContainer, actualArgs);
});

@@ -79,3 +82,3 @@ spyFunction.calledWith = function () {

}
calledWithObject.calledWithMethodWasCalled = true;
calledWithObject.wasCalled = true;
calledWithObject = addSyncHandlingToCalledWith(calledWithObject, calledWithArgs);

@@ -86,12 +89,23 @@ calledWithObject = promises_spy_utils_1.addPromiseHandlingToCalledWith(calledWithObject, calledWithArgs);

};
spyFunction.mustBeCalledWith = function () {
var calledWithArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
calledWithArgs[_i] = arguments[_i];
}
mustBeCalledWithObject.wasCalled = true;
mustBeCalledWithObject = addSyncHandlingToCalledWith(mustBeCalledWithObject, calledWithArgs);
mustBeCalledWithObject = promises_spy_utils_1.addPromiseHandlingToCalledWith(mustBeCalledWithObject, calledWithArgs);
mustBeCalledWithObject = observable_spy_utils_1.addObservableHandlingToCalledWith(mustBeCalledWithObject, calledWithArgs);
return mustBeCalledWithObject;
};
return spyFunction;
}
function spyFunctionCallFakeImplementation(calledWithObject, valueContainer, actualArgs) {
var e_1, _a;
if (calledWithObject.calledWithMethodWasCalled) {
function spyFunctionImplementation(calledWithObject, mustBeCalledWithObject, valueContainer, actualArgs) {
var e_1, _a, e_2, _b;
if (calledWithObject.wasCalled) {
try {
for (var _b = __values(calledWithObject.calledWithMap.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
var storedCalledWithArgs = _c.value;
for (var _c = __values(calledWithObject.argsToValuesMap.keys()), _d = _c.next(); !_d.done; _d = _c.next()) {
var storedCalledWithArgs = _d.value;
if (deep_equal_1.default(storedCalledWithArgs, actualArgs)) {
return calledWithObject.calledWithMap.get(storedCalledWithArgs);
return calledWithObject.argsToValuesMap.get(storedCalledWithArgs);
}

@@ -103,9 +117,24 @@ }

try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
}
finally { if (e_1) throw e_1.error; }
}
if (calledWithObject.shouldThrow) {
error_handling_1.throwArgumentsError(actualArgs);
}
if (mustBeCalledWithObject.wasCalled) {
try {
for (var _e = __values(mustBeCalledWithObject.argsToValuesMap.keys()), _f = _e.next(); !_f.done; _f = _e.next()) {
var storedCalledWithArgs = _f.value;
if (deep_equal_1.default(storedCalledWithArgs, actualArgs)) {
return mustBeCalledWithObject.argsToValuesMap.get(storedCalledWithArgs);
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
}
finally { if (e_2) throw e_2.error; }
}
error_handling_1.throwArgumentsError(actualArgs);
}

@@ -116,8 +145,3 @@ return valueContainer.value;

calledWithObject.returnValue = function (value) {
calledWithObject.calledWithMap.set(calledWithArgs, value);
return {
throwOnMismatch: function () {
calledWithObject.shouldThrow = true;
}
};
calledWithObject.argsToValuesMap.set(calledWithArgs, value);
};

@@ -124,0 +148,0 @@ return calledWithObject;

export interface SpyFunctionReturnValueContainer {
value: any;
}
export interface MockTransformer {
throwOnMismatch: () => void;
}
export interface CalledWithObject {
calledWithMethodWasCalled: boolean;
shouldThrow: boolean;
calledWithMap: Map<any, any>;
returnValue?: (value: any) => MockTransformer;
resolveWith?: (value?: any) => MockTransformer;
rejectWith?: (value?: any) => MockTransformer;
nextWith?(value?: any): MockTransformer;
nextOneTimeWith?(value?: any): MockTransformer;
throwWith?(value: any): MockTransformer;
complete?(): MockTransformer;
wasCalled: boolean;
argsToValuesMap: Map<any, any>;
returnValue?: (value: any) => void;
resolveWith?: (value?: any) => void;
rejectWith?: (value?: any) => void;
nextWith?(value?: any): void;
nextOneTimeWith?(value?: any): void;
throwWith?(value: any): void;
complete?(): void;
}

@@ -29,8 +29,3 @@ "use strict";

subject.next(value);
calledWithObject.calledWithMap.set(calledWithArgs, subject);
return {
throwOnMismatch: function () {
calledWithObject.shouldThrow = true;
}
};
calledWithObject.argsToValuesMap.set(calledWithArgs, subject);
};

@@ -40,26 +35,11 @@ calledWithObject.nextOneTimeWith = function (value) {

subject.complete();
calledWithObject.calledWithMap.set(calledWithArgs, subject);
return {
throwOnMismatch: function () {
calledWithObject.shouldThrow = true;
}
};
calledWithObject.argsToValuesMap.set(calledWithArgs, subject);
};
calledWithObject.throwWith = function (value) {
subject.error(value);
calledWithObject.calledWithMap.set(calledWithArgs, subject);
return {
throwOnMismatch: function () {
calledWithObject.shouldThrow = true;
}
};
calledWithObject.argsToValuesMap.set(calledWithArgs, subject);
};
calledWithObject.complete = function () {
subject.complete();
calledWithObject.calledWithMap.set(calledWithArgs, subject);
return {
throwOnMismatch: function () {
calledWithObject.shouldThrow = true;
}
};
calledWithObject.argsToValuesMap.set(calledWithArgs, subject);
};

@@ -66,0 +46,0 @@ return calledWithObject;

@@ -14,16 +14,6 @@ "use strict";

calledWithObject.resolveWith = function (value) {
calledWithObject.calledWithMap.set(calledWithArgs, Promise.resolve(value));
return {
throwOnMismatch: function () {
calledWithObject.shouldThrow = true;
}
};
calledWithObject.argsToValuesMap.set(calledWithArgs, Promise.resolve(value));
};
calledWithObject.rejectWith = function (value) {
calledWithObject.calledWithMap.set(calledWithArgs, Promise.reject(value));
return {
throwOnMismatch: function () {
calledWithObject.shouldThrow = true;
}
};
calledWithObject.argsToValuesMap.set(calledWithArgs, Promise.reject(value));
};

@@ -30,0 +20,0 @@ return calledWithObject;

/// <reference types="jasmine" />
import { Observable } from 'rxjs';
import { MockTransformer } from './create-spy-from-class.types';
export declare type Spy<T> = {

@@ -9,15 +8,18 @@ [k in keyof T]: AddSpyTypes<T[k]>;

export interface PromiseSpyMethod<T> {
resolveWith(value?: T): MockTransformer;
rejectWith(value?: any): MockTransformer;
resolveWith(value?: T): void;
rejectWith(value?: any): void;
}
export interface ObservableSpyMethod<T> {
nextWith(value?: T): MockTransformer;
nextOneTimeWith(value?: T): MockTransformer;
throwWith(value: any): MockTransformer;
complete(): MockTransformer;
nextWith(value?: T): void;
nextOneTimeWith(value?: T): void;
throwWith(value: any): void;
complete(): void;
}
export interface SpyMethod {
calledWith(...args: any[]): {
returnValue: (value: any) => MockTransformer;
returnValue: (value: any) => void;
};
mustBeCalledWith(...args: any[]): {
returnValue: (value: any) => void;
};
}

@@ -28,2 +30,3 @@ export declare type AddSpyOnFunction<T extends (...args: any[]) => any> = T & SpyMethod & jasmine.Spy;

calledWith(...args: any[]): PromiseSpyMethod<Unpacked<T>>;
mustBeCalledWith(...args: any[]): PromiseSpyMethod<Unpacked<T>>;
} & jasmine.Spy;

@@ -33,4 +36,5 @@ export declare type AddSpyOnObservable<T extends Observable<any>> = {

calledWith(...args: any[]): ObservableSpyMethod<Unpacked<T>>;
mustBeCalledWith(...args: any[]): ObservableSpyMethod<Unpacked<T>>;
} & jasmine.Spy;
export declare type AddSpyByReturnTypes<TF extends (...args: any[]) => any> = TF & (TF extends (...args: any[]) => infer TR ? TR extends (...args: any[]) => infer R2 ? AddSpyOnFunction<TR> : TR extends Promise<any> ? AddSpyOnPromise<TR> : TR extends Observable<any> ? AddSpyOnObservable<TR> : AddSpyOnFunction<TF> : never);
export declare type Unpacked<T> = T extends Array<infer U1> ? U1 : T extends (...args: any[]) => infer U2 ? U2 : T extends Promise<infer U3> ? U3 : T extends Observable<infer U4> ? U4 : T;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet