Giter VIP home page Giter VIP logo

Comments (6)

iangregsondev avatar iangregsondev commented on August 26, 2024 1

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.

emyann avatar emyann commented on August 26, 2024

@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.

iangregsondev avatar iangregsondev commented on August 26, 2024

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.

iangregsondev avatar iangregsondev commented on August 26, 2024

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

image

but on the mockedMorphism I get

image

so although the mock does work, when i do my when clause, becasue its any, I get no code completion / intellisense

ie..

image

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

image

from morphism.

iangregsondev avatar iangregsondev commented on August 26, 2024

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.

iangregsondev avatar iangregsondev commented on August 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.