@hirez_io/auto-spies-core
Advanced tools
Comparing version 1.1.3 to 1.2.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.2.0](https://github.com/hirezio/auto-spies/compare/@hirez_io/auto-spies-core@1.1.3...@hirez_io/auto-spies-core@1.2.0) (2020-10-19) | ||
### Features | ||
* **global:** printing function name in error when mustBeCalledWith is configured ([b5e0dbd](https://github.com/hirezio/auto-spies/commit/b5e0dbdf812d4c45f8109397e5aa4d33ffc37d82)), closes [#27](https://github.com/hirezio/auto-spies/issues/27) | ||
## [1.1.3](https://github.com/hirezio/auto-spies/compare/@hirez_io/auto-spies-core@1.1.2...@hirez_io/auto-spies-core@1.1.3) (2020-10-01) | ||
@@ -8,0 +19,0 @@ |
import { Observable } from 'rxjs'; | ||
export declare type Func = (...args: any[]) => any; | ||
export declare type ClassType<T> = { | ||
new (...args: any[]): T; | ||
[key: string]: any; | ||
}; | ||
export declare type CreateSyncAutoSpy<Method extends Func, LibSpecificFunctionSpy, LibSpecificFunctionSpyPlusCalledWith> = Method & LibSpecificFunctionSpy & LibSpecificFunctionSpyPlusCalledWith; | ||
@@ -4,0 +8,0 @@ declare type StringKeysForPropertyType<ObjectType, PropType> = Extract<{ |
@@ -19,6 +19,6 @@ import { Func } from '.'; | ||
export declare type SyncSpyMethodsDecorator<FrameworkSpecificType> = (calledWithObject: CalledWithObject, calledWithArgs: any[]) => CalledWithObject & FrameworkSpecificType; | ||
export declare type FunctionSpyFactory = (name: string, spyFunctionImplementation: Func) => { | ||
export declare type FunctionSpyFactory = (functionName: string, spyFunctionImplementation: Func) => { | ||
functionSpy: any; | ||
objectToAddSpyMethodsTo: any; | ||
}; | ||
export declare function createFunctionAutoSpy<ReturnType, LibSpecificType>(name: string, syncSpyMethodsDecorator: SyncSpyMethodsDecorator<LibSpecificType>, functionSpyFactory: FunctionSpyFactory): ReturnType; | ||
export declare function createFunctionAutoSpy<ReturnType, LibSpecificType>(functionName: string, syncSpyMethodsDecorator: SyncSpyMethodsDecorator<LibSpecificType>, frameworkFunctionSpyFactory: FunctionSpyFactory): ReturnType; |
@@ -21,13 +21,3 @@ "use strict"; | ||
var error_handler_1 = require("./errors/error-handler"); | ||
function createFunctionAutoSpy(name, syncSpyMethodsDecorator, functionSpyFactory) { | ||
// Function to pass to the specific testing library to call | ||
// whenever someone calls the spied on method | ||
function spyFunctionImpl() { | ||
var actualArgs = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
actualArgs[_i] = arguments[_i]; | ||
} | ||
return returnTheCorrectFakeValue(calledWithObject, mustBeCalledWithObject, valueContainer, actualArgs); | ||
} | ||
var _a = functionSpyFactory(name, spyFunctionImpl), functionSpy = _a.functionSpy, objectToAddSpyMethodsTo = _a.objectToAddSpyMethodsTo; | ||
function createFunctionAutoSpy(functionName, syncSpyMethodsDecorator, frameworkFunctionSpyFactory) { | ||
var calledWithObject = { | ||
@@ -44,2 +34,12 @@ wasConfigured: false, | ||
}; | ||
// Function to pass to the specific testing library to call | ||
// whenever someone calls the spied on method | ||
function spyFunctionImpl() { | ||
var actualArgs = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
actualArgs[_i] = arguments[_i]; | ||
} | ||
return returnTheCorrectFakeValue(calledWithObject, mustBeCalledWithObject, valueContainer, actualArgs, functionName); | ||
} | ||
var _a = frameworkFunctionSpyFactory(functionName, spyFunctionImpl), functionSpy = _a.functionSpy, objectToAddSpyMethodsTo = _a.objectToAddSpyMethodsTo; | ||
_1.addPromiseHelpersToFunctionSpy(objectToAddSpyMethodsTo, valueContainer); | ||
@@ -71,3 +71,3 @@ _1.addObservableHelpersToFunctionSpy(objectToAddSpyMethodsTo, valueContainer); | ||
} | ||
function returnTheCorrectFakeValue(calledWithObject, mustBeCalledWithObject, valueContainer, actualArgs) { | ||
function returnTheCorrectFakeValue(calledWithObject, mustBeCalledWithObject, valueContainer, actualArgs, functionName) { | ||
var e_1, _a, e_2, _b; | ||
@@ -115,3 +115,3 @@ if (calledWithObject.wasConfigured) { | ||
} | ||
error_handler_1.errorHandler.throwArgumentsError(actualArgs); | ||
error_handler_1.errorHandler.throwArgumentsError(actualArgs, functionName); | ||
} | ||
@@ -118,0 +118,0 @@ if (valueContainer._isRejectedPromise) { |
export declare const errorHandler: { | ||
throwArgumentsError(actualArgs: any[]): void; | ||
throwArgumentsError(actualArgs: any[], functionName: string): void; | ||
}; |
@@ -5,4 +5,4 @@ "use strict"; | ||
exports.errorHandler = { | ||
throwArgumentsError: function (actualArgs) { | ||
var errorMessage = "\n The function was configured with 'mustBeCalledWith'\n and expects to be called with specific arguments.\n -\n "; | ||
throwArgumentsError: function (actualArgs, functionName) { | ||
var errorMessage = "\n The function '" + functionName + "' was configured with\n 'mustBeCalledWith' and expects to be called with specific arguments.\n -\n "; | ||
if (!actualArgs || actualArgs.length === 0) { | ||
@@ -9,0 +9,0 @@ errorMessage += "\n But the function was called without any arguments\n "; |
{ | ||
"name": "@hirez_io/auto-spies-core", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"publishConfig": { | ||
@@ -57,3 +57,3 @@ "access": "public" | ||
}, | ||
"gitHead": "211e6aa2a00d0d2d5f299a0701d947b85d1acb17" | ||
"gitHead": "6e50d58d57803b4508c5d58f005b98424c3a5e0c" | ||
} |
@@ -5,2 +5,4 @@ import { Observable } from 'rxjs'; | ||
export type ClassType<T> = { new (...args: any[]): T; [key: string]: any }; | ||
export type CreateSyncAutoSpy< | ||
@@ -7,0 +9,0 @@ Method extends Func, |
@@ -35,3 +35,3 @@ import { | ||
export type FunctionSpyFactory = ( | ||
name: string, | ||
functionName: string, | ||
spyFunctionImplementation: Func | ||
@@ -44,22 +44,6 @@ ) => { | ||
export function createFunctionAutoSpy<ReturnType, LibSpecificType>( | ||
name: string, | ||
functionName: string, | ||
syncSpyMethodsDecorator: SyncSpyMethodsDecorator<LibSpecificType>, | ||
functionSpyFactory: FunctionSpyFactory | ||
frameworkFunctionSpyFactory: FunctionSpyFactory | ||
): ReturnType { | ||
// Function to pass to the specific testing library to call | ||
// whenever someone calls the spied on method | ||
function spyFunctionImpl(...actualArgs: any[]) { | ||
return returnTheCorrectFakeValue( | ||
calledWithObject, | ||
mustBeCalledWithObject, | ||
valueContainer, | ||
actualArgs | ||
); | ||
} | ||
const { functionSpy, objectToAddSpyMethodsTo } = functionSpyFactory( | ||
name, | ||
spyFunctionImpl | ||
); | ||
const calledWithObject: CalledWithObject = { | ||
@@ -79,2 +63,19 @@ wasConfigured: false, | ||
// Function to pass to the specific testing library to call | ||
// whenever someone calls the spied on method | ||
function spyFunctionImpl(...actualArgs: any[]) { | ||
return returnTheCorrectFakeValue( | ||
calledWithObject, | ||
mustBeCalledWithObject, | ||
valueContainer, | ||
actualArgs, | ||
functionName | ||
); | ||
} | ||
const { functionSpy, objectToAddSpyMethodsTo } = frameworkFunctionSpyFactory( | ||
functionName, | ||
spyFunctionImpl | ||
); | ||
addPromiseHelpersToFunctionSpy(objectToAddSpyMethodsTo, valueContainer); | ||
@@ -118,3 +119,4 @@ addObservableHelpersToFunctionSpy(objectToAddSpyMethodsTo, valueContainer); | ||
valueContainer: FunctionSpyReturnValueContainer, | ||
actualArgs: any[] | ||
actualArgs: any[], | ||
functionName: string | ||
) { | ||
@@ -146,3 +148,3 @@ if (calledWithObject.wasConfigured) { | ||
} | ||
errorHandler.throwArgumentsError(actualArgs); | ||
errorHandler.throwArgumentsError(actualArgs, functionName); | ||
} | ||
@@ -149,0 +151,0 @@ if (valueContainer._isRejectedPromise) { |
export const errorHandler = { | ||
throwArgumentsError(actualArgs: any[]): void { | ||
throwArgumentsError(actualArgs: any[], functionName: string): void { | ||
let errorMessage = ` | ||
The function was configured with 'mustBeCalledWith' | ||
and expects to be called with specific arguments. | ||
The function '${functionName}' was configured with | ||
'mustBeCalledWith' and expects to be called with specific arguments. | ||
- | ||
@@ -7,0 +7,0 @@ `; |
@@ -5,46 +5,55 @@ import { errorHandler } from './error-handler'; | ||
let fakeExpectedArgs: any[]; | ||
let fakeFunctionName: string; | ||
let fakeActualArgs: any[]; | ||
let actualError: any; | ||
Given(() => { | ||
fakeExpectedArgs = [1, 2]; | ||
}); | ||
When(() => { | ||
try { | ||
errorHandler.throwArgumentsError(fakeActualArgs); | ||
} catch (error) { | ||
actualError = error.message; | ||
} | ||
}); | ||
describe('Given actual args are empty and do not match', () => { | ||
describe('GIVEN a function name - ', () => { | ||
Given(() => { | ||
fakeActualArgs = []; | ||
fakeFunctionName = 'fakeFunction'; | ||
fakeExpectedArgs = [1, 2]; | ||
}); | ||
Then('throw the empty error', () => { | ||
expect(actualError).toContain('But the function was called without any arguments'); | ||
When(() => { | ||
try { | ||
errorHandler.throwArgumentsError(fakeActualArgs, fakeFunctionName); | ||
} catch (error) { | ||
actualError = error.message; | ||
} | ||
}); | ||
}); | ||
describe('Given actual args do not match', () => { | ||
Given(() => { | ||
fakeActualArgs = [1, 2]; | ||
}); | ||
describe('GIVEN actual args are empty and do not match', () => { | ||
Given(() => { | ||
fakeActualArgs = []; | ||
}); | ||
Then('throw error with arguments', () => { | ||
expect(actualError).toContain('But the actual arguments were: 1,2'); | ||
Then('throw the empty error', () => { | ||
expect(actualError).toContain("The function 'fakeFunction' was configured with"); | ||
expect(actualError).toContain( | ||
'But the function was called without any arguments' | ||
); | ||
}); | ||
}); | ||
}); | ||
describe('Given actual args of type object do not match', () => { | ||
Given(() => { | ||
fakeActualArgs = [{ yep: 1 }]; | ||
describe('GIVEN actual args do not match', () => { | ||
Given(() => { | ||
fakeActualArgs = [1, 2]; | ||
}); | ||
Then('throw error with arguments', () => { | ||
expect(actualError).toContain("The function 'fakeFunction' was configured with"); | ||
expect(actualError).toContain('But the actual arguments were: 1,2'); | ||
}); | ||
}); | ||
Then('throw error with arguments', () => { | ||
expect(actualError).toContain('But the actual arguments were: {"yep":1}'); | ||
describe('GIVEN actual args of type object do not match', () => { | ||
Given(() => { | ||
fakeActualArgs = [{ yep: 1 }]; | ||
}); | ||
Then('throw error with arguments', () => { | ||
expect(actualError).toContain("The function 'fakeFunction' was configured with"); | ||
expect(actualError).toContain('But the actual arguments were: {"yep":1}'); | ||
}); | ||
}); | ||
}); | ||
}); |
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
128270
1074