Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-mockito

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-mockito - npm Package Compare versions

Comparing version 0.1.2 to 0.1.3

lib/matcher/ArgsToMatchersValidator.d.ts

2

lib/matcher/MatchersToStringConverter.d.ts

@@ -1,4 +0,4 @@

import { Matcher } from './Matcher';
import { Matcher } from './type/Matcher';
export declare class MatchersToStringConverter {
convert(matchers: Array<Matcher>): string;
}

@@ -1,2 +0,2 @@

import { MethodStub } from './MethodStub';
import { MethodStub } from './stub/MethodStub';
export declare class MethodStubCollection {

@@ -3,0 +3,0 @@ private hadMoreThanOneBehavior;

@@ -6,3 +6,4 @@ import { MethodToStub } from './MethodToStub';

thenReturn(value: T): void;
throwsError(value: Error): void;
throwsError(error: Error): void;
thenCall(func: (...args) => any): void;
}
"use strict";
var MethodStub_1 = require('./MethodStub');
var ReturnValueMethodStub_1 = require('./stub/ReturnValueMethodStub');
var ThrowErrorMethodStub_1 = require('./stub/ThrowErrorMethodStub');
var CallFunctionMethodStub_1 = require('./stub/CallFunctionMethodStub');
var MethodStubSetter = (function () {

@@ -8,7 +10,10 @@ function MethodStubSetter(methodToStub) {

MethodStubSetter.prototype.thenReturn = function (value) {
this.methodToStub.methodStubCollection.add(new MethodStub_1.MethodStub(this.methodToStub.matchers, value));
this.methodToStub.methodStubCollection.add(new ReturnValueMethodStub_1.ReturnValueMethodStub(this.methodToStub.matchers, value));
};
MethodStubSetter.prototype.throwsError = function (value) {
this.methodToStub.methodStubCollection.add(new MethodStub_1.MethodStub(this.methodToStub.matchers, { error: value }));
MethodStubSetter.prototype.throwsError = function (error) {
this.methodToStub.methodStubCollection.add(new ThrowErrorMethodStub_1.ThrowErrorMethodStub(this.methodToStub.matchers, error));
};
MethodStubSetter.prototype.thenCall = function (func) {
this.methodToStub.methodStubCollection.add(new CallFunctionMethodStub_1.CallFunctionMethodStub(this.methodToStub.matchers, func));
};
return MethodStubSetter;

@@ -15,0 +20,0 @@ }());

import { MethodStubCollection } from './MethodStubCollection';
import { Matcher } from './matcher/Matcher';
import { Matcher } from './matcher/type/Matcher';
import { Mock } from './Mock';

@@ -4,0 +4,0 @@ export declare class MethodToStub {

@@ -1,2 +0,2 @@

import { Matcher } from './matcher/Matcher';
import { Matcher } from './matcher/type/Matcher';
import { MethodAction } from './MethodAction';

@@ -3,0 +3,0 @@ export declare class Mock {

"use strict";
var MethodStubCollection_1 = require('./MethodStubCollection');
var MethodToStub_1 = require('./MethodToStub');
var MethodStub_1 = require('./MethodStub');
var Matcher_1 = require('./matcher/Matcher');
var StrictEqualMatcher_1 = require('./matcher/StrictEqualMatcher');
var Matcher_1 = require('./matcher/type/Matcher');
var StrictEqualMatcher_1 = require('./matcher/type/StrictEqualMatcher');
var MethodAction_1 = require('./MethodAction');
var ReturnValueMethodStub_1 = require('./stub/ReturnValueMethodStub');
var Mock = (function () {

@@ -71,3 +71,5 @@ function Mock(clazz) {

_this.methodActions.push(action);
return _this.getMethodStub(key, args);
var methodStub = _this.getMethodStub(key, args);
methodStub.execute(args);
return methodStub.getValue();
};

@@ -83,12 +85,12 @@ };

if (!methodStub) {
return new MethodStub_1.MethodStub([], null).getValue();
return new ReturnValueMethodStub_1.ReturnValueMethodStub([], null);
}
else if (methodStub.getHadMoreThanOneBehavior() && methodStub.hasMatching(args)) {
return methodStub.getFirstMatchingAndRemove(args).getValue();
return methodStub.getFirstMatchingAndRemove(args);
}
else if (methodStub.hasMatching(args)) {
return methodStub.getFirstMatching(args).getValue();
return methodStub.getFirstMatching(args);
}
else {
return new MethodStub_1.MethodStub([], null).getValue();
return new ReturnValueMethodStub_1.ReturnValueMethodStub([], null);
}

@@ -95,0 +97,0 @@ };

"use strict";
var AnyNumberMatcher_1 = require('./matcher/AnyNumberMatcher');
var AnyNumberMatcher_1 = require('./matcher/type/AnyNumberMatcher');
var ts_mockito_1 = require('./ts-mockito');

@@ -4,0 +4,0 @@ var Generic = (function () {

{
"name": "ts-mockito",
"version": "0.1.2",
"version": "0.1.3",
"description": "",

@@ -8,3 +8,4 @@ "main": "lib/ts-mockito.js",

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"compile": "rm -rf lib && ./node_modules/.bin/tsc -p ./src",
"test": "./node_modules/.bin/karma start karma.conf.js --single-run"
},

@@ -11,0 +12,0 @@ "author": "kuster.maciej@gmail.com",

@@ -15,3 +15,4 @@ # ts-mockito

* `thenReturn` - return value
* `thenThrowsError` - throw an error
* `throwError` - throw an error
* `thenCall` - call custom method
* Checking if methods were called with given arguments (`verify`)

@@ -83,3 +84,3 @@ * `anything`, `notNull`, `anyString` etc. - for more flexible comparision

verify(mockedFoo.getBar(between(2, 3))).thrice(); // was called with arg beween 2-3 exactly three times
verify(mockedFoo.getBar(anyNumber()).time(4); // was called with any number arg exactly four times
verify(mockedFoo.getBar(anyNumber()).times(4); // was called with any number arg exactly four times
verify(mockedFoo.getBar(2)).atLeast(2); // was called with arg === 2 min two times

@@ -90,2 +91,33 @@ verify(mockedFoo.getBar(1)).atMoast(1); // was called with arg === 1 max one time

### Throwing errors
``` typescript
let mockedFoo:Foo = mock(Foo);
when(mockedFoo.getBar(10)).throwError(new Error('fatal error'));
let foo:Foo = instance(mockedFoo);
try {
foo.getBar(10);
} catch (error:Error) {
console.log(error.message); // 'fatal error'
}
```
### Custom function
You can also stub method with your own implementation
``` typescript
let mockedFoo:Foo = mock(Foo);
let foo:Foo = instance(mockedFoo);
when(mockedFoo.sumTwoNumbers(anyNumber(), anyNumber())).thenCall((arg1:number, arg2:number) => {
return arg1 * arg2;
});
// prints '50' because we've changed sum method implementation to multiply!
console.log(foo.sumTwoNumbers(5, 10));
```
### Recording multiple behaviors

@@ -92,0 +124,0 @@

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc