Comments (23)
@michalstocki mocking interfaces is planned but rather not in the coming weeks. Also I will need some help with this feature from you.
from ts-mockito.
Mocking abstract classes is not a trivial task, because abstract methods do not exist in JavaScript. We can't simply iterate over the object properties to create the correct mock object.
abstract class AbstractClass {
public abstract getValue():number;
public getOtherValue():string {
return '';
}
}
produces:
var AbstractClass = (function () {
function AbstractClass() {
}
AbstractClass.prototype.getOtherValue = function () {
return '';
};
return AbstractClass;
}());
This could potentially be implemented in the way similar to mocking of interfaces (using Proxy object). Maybe using the same technique. However the feature is still not supported. @NagRock could you confirm whether the feature is planned in the nearest future?
const abstractMock:AbstractClass = mockType<AbstractClass>();
from ts-mockito.
@woodcockjosh it doesn't seem to be that obvious how this can be implemented easily.. I use this workaround:
interface LoadBalancer {
calculate: (...) => Promise<any>;
}
const loadBalancerMock =
mock.mock<LoadBalancer>({ prototype: { calculate: (): any => undefined } } as any);
from ts-mockito.
@woodcockjosh I'm not sure how you intended that response to come across, but it sounded really douchey. 😕
from ts-mockito.
from ts-mockito.
😄 But it's the essential value of an abstract class: it enforces consistency of it's implementations. I think you should not create a special Extension class only for the mocking purpose. You can use one of the extensions already existing in your code.
from ts-mockito.
Thanks to @dyong0 we can now mock abstract
and generic
types in version 2.1.0 (https://github.com/NagRock/ts-mockito/releases/tag/v2.1.0)!
I will close this issue and open new for interfaces mocking.
from ts-mockito.
When we gonna be able to mock interfaces? It's like really I have to mock an implementation? That's totally broken and defeats the whole point of mocking.
from ts-mockito.
+1 for mocking interfaces! 😩
Edit: Just noticed what I wanted is actually pretty easy:
interface ITest {
one: string;
two(): number;
}
const service = (dep: ITest) => {
dep.two();
};
const testMock: Partial<ITest> = {
two: jest.fn(() => 1),
};
const subject = service(testMock as ITest);
Peaked into https://github.com/jjherscheid/ts-mocks/blob/master/src/mocks/mock.ts
from ts-mockito.
Nevertheless, the simple workaround for lack of this feature is using any extension of the AbstractClass
to mock;
// Exntension.ts
export class Extension extends AbstractClass {
public getValue():number {
return 1;
}
}
// test
const abstractMock:AbstractClass = mock(Extension);
Note, that abstractMock
is in type of AbstractClass
to not allow calling of extra methods that Extenstion
could have.
from ts-mockito.
Thanks for the explanation and the quick response! I'm using the workaround right now. One thing I don't like about it is that any changes in the number or signature of abstract methods has to be replicated in the Extension class.
from ts-mockito.
@ssynix please give feedback is it works as expected.
from ts-mockito.
@NagRock I found another issue on mocking generics. Mocking a generic class or interface loses members with its generic types. For example, mongoose.Model is an generic interface
interface Model<T extends Document> extends NodeJS.EventEmitter, ModelProperties { ... }
which has
create(docs: any[], callback?: (err: any, res: T[]) => void): Promise<T[]>;
where(path: string, val?: Object): Query<any>;
When you mock it:
let mockedModel = mock(mongoose.Model);
let model = instance(mockedModel);
it loses some parts of its body:
yet keeps members without its generic types:
Can you please add a note for this issue on mocking generics? README would be nice to place it.
from ts-mockito.
Plus, when you console.log()
mocked a generic. It displays null
, but actually it's not.
let mockedModel = mock(mongoose.Model);
let model = instance(mockedModel);
console.log(model); // null
model === null; // false
from ts-mockito.
@dyong0 I will try to fix it instead of adding info in readme. Thanks for reporting this!
from ts-mockito.
@dyong0 you trying to mock interface (not abstract class) that is not supported yet.
from ts-mockito.
@NagRock right, it doesn't have tests for mocking interfaces. but looks working fine for interfaces too. btw the issue i reported was about mocking generics. i think the same problem happens on generic classes as well as abstract ones.
from ts-mockito.
@dyong0 could you please provide a sample repository with this? It would be much simpler for me to fix this with project that reproduces issue.
from ts-mockito.
When mocking abstract classes, abstract methods are not mocked (version 2.1.1). I assume that this is the same problem that prevents mocking interfaces, but at minimum the docs should be updated to prevent confusion.
abstract class Foo {
abstract foo(): void;
bar(): void {}
}
...
const mock = mock(Foo)
mock.foo // undefined
mock.bar // function
from ts-mockito.
I tried to run @mpiroc example with v2.2.5 and got the same output (foo is undefined),
I did include es6 for tsconfig lib param.
I dont understand - this should be supported now? if so what am i doing wrong?
Thanks!
from ts-mockito.
Hi, thanks for reporting. I will check it.
from ts-mockito.
Hey guys. Was this issue solved? I can see the PR that adds support for mocking abstract classes was merged, but I'm still getting the error mentioned by @mpiroc. I'm using version 2.2.9. Thanks
from ts-mockito.
Fine, I'll do a pull request with a solution. That's obviously not the solution.
from ts-mockito.
Related Issues (20)
- Verify if a function is called in a resolved promise
- (instance(mock(MyClass)) instanceof MyClass) is false. HOT 6
- Verify on interface mock does not work (2.6.1)
- Argument Matcher anyBoolean() Missing
- reset(mock1, mock2) expects that mock1 and mock2 have the same type. HOT 1
- When with literal object argument not working HOT 4
- Visibility/log on arguments mismatch in function call HOT 1
- Is it going to be maintained? HOT 32
- verify() is not able to verify arguments except for the last call HOT 1
- Captured arguments are changed by object changes HOT 1
- Proxy created with instance(mockedClass) gets "then" function -> Results in Timeout when Using Nestjs DI HOT 2
- Interface mocks cannot be bound in inversify containers HOT 5
- can you mock functions from a module?
- TypeError when using `match` with a string
- How to mock a builder pattern
- How to mock catchError HOT 1
- anyOfType Matcher
- Feature request: better toString() outputs showing expected value HOT 2
- Question: How to call original method on spy after interception
- How to mock an async function that sets two values in the MockClass?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-mockito.