Comments (6)
What I am doing right now is, I am wrapping morphism in our own service (nestjs) so i can inject this throughout the application.
I can mock this without issues as I would be wrapping the public methods of my wrapper etc.
Although it would be interesting why the above is happening.
if I can provide any help, please let me know.
from morphism.
@iangregsondev Thank you for using Morphism
! I don't really know how ts-mockito
works, but I have the feeling it's better suited for Class objects https://github.com/NagRock/ts-mockito/blob/master/src/Mock.ts#L162.
Can you provide an example of a spy on fs
or process
? I know morphism is actually mockable with Jest #137 but I'll try to play with ts-mockito
and morphism
and get back to you if I have a clue :)
from morphism.
Hi,
sure I can provide an example with fs and with process, I am using those already - here goes.
import fs from "fs"
let spiedFs: typeof fs
let spiedProcess: NodeJS.Process
// Create spy of fs
spiedFs = spy(fs)
// Create spy of process
spiedProcess = spy(process)
// Return false when existsSync is called
when(spiedFs.existsSync(anything())).thenReturn(true)
process.exit(1)
console.log(fs.existsSync("something")) // returns true - even though it doesnt exist
verify(spiedProcess.exit(anything())).never() // fails as it was called
With regards to mock, yes we also use that a lot but that is when we pass the mock (instance) using a constructor.
fs, process are not passed in and also morphism is not passed in but actually being imported in another class just like fs etc
If I can provide further info then please let me know.
Maybe just something isn't compatible.
We actually use jest for running tests but we don't use the jest mocks, I think the api isn't that great :-) so we prefer to ts-mockito.
from morphism.
Hi I have some more updates, the spy I could not get to work but I tried with a mock and it seemed quite difficult at first.. Anyway, I got it working but mock is unable to determine the time..
import Morphism from "morphism"
// WORKING MOCK!
const mockedMorphism = mock(Morphism) // But this is any
when(mockedMorphism.morphism(anything(), anything())).thenReturn("ian" as any)
const myinstance = instance(mockedMorphism)
console.log(myinstance.morphism({} as any, {} as any))
The type that is returned morphism is a union type of
declare const Morphism: typeof morphism & IMorphismRegistry;
I can't do that outside becasue as far as I can see the IMorphismRegistry isnt being exported to the public api.
I would prefer for the spy to work but maybe there is a problem with ts-mockit - I am not sure.. I could get the fs and process spied without an issue.
With regards to mocking, I am mocking other classes etc and the type is known i.e.
As you can see, it infers the type
but on the mockedMorphism I get
so although the mock does work, when i do my when clause, becasue its any, I get no code completion / intellisense
ie..
Here is an output of the working mock, but as I say, there is no type associated with it and I was unsure how to get ahold of it
from morphism.
I am a little rusty with my type definitions but I checked "fs" and everything is wrapped inside a
declare module "fs" {
``
but I noticed that morphism type was not.
from morphism.
I am using nestjs, so my initial idea was to basically expose morphism as a global service but becasue there was no type that I could find - i could not figure that out.
I could make my own type :-)
But maybe there is something you expose or something that I am not seeing correctly.
from morphism.
Related Issues (20)
- Mocking `morphism` named export HOT 2
- [Question] How to construct an object with projection HOT 3
- [Bug] Inferred types break with certain target types HOT 2
- Difference between an object with type and using createSchema HOT 2
- Failing to change types of target schema (TypeScript) HOT 3
- Dealing with undefined source ? HOT 4
- A little help with flattening ? HOT 9
- [Question] Is there an easy way to apply the identity morphism? HOT 4
- A merge method for copying properties with mappings HOT 5
- [Question]: Who is using the Registry to store the mappers ?
- Map array of objects? HOT 1
- Help needed to flatten nest array within a object
- Generics do not work properly with typescript HOT 3
- Morphism playground doesnt work
- Mapping with runtime arguments?
- mapping array without functions
- [Question] Supporting circular references in destination types
- How to map nested types HOT 2
- Still being updated? HOT 3
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 morphism.