#Medusa Extender Custom Service

33 messages · Page 1 of 1 (latest)

echo basalt
#

How to access a custom service written using medusa extender inside a jest testing file?
There is the documentation on how to use a service inside another service, API endpoint or a subscription but not how to import it and use inside a regular function.

#

@steep tusk ?

steep tusk
#

Hi there,
Sorry I can't help you yet, I'm still reading the documentation and trying to figure things out ^^

But from what I see the service is still a class that is extended, so you can mock it the same way, right?

import { MyService } from './src/modules/test.service.ts';

jest.mock('./src/modules/test.service.ts', () => {
  class MockedMyService {
    public foo = jest.fn().mockReturnValue('mocked value');
  }
  return { MyService: MockedMyService };
});

But I may be wrong

dry plover
#

Indeed, when you test a service you can import it as usual and pass the mocks in the constructor. Personally i like doing something like you can see here https://github.com/medusajs/medusa/blob/master/packages/medusa/src/services/__tests__/payment-provider.js which you can see in the fixture i create a fake container where i register the mocks and the target service and then resolve it int the tests. You can also find some example in the extender core 👍 finally, you can go for @steep tusk solution, there is multiple ways ahah

GitHub

The open-source Shopify alternative ⚡️. Contribute to medusajs/medusa development by creating an account on GitHub.

echo basalt
#

@queen jackal

#

I have created a few custom modules using medusa extender

queen jackal
#

Okey I am here I just finish to take my walk and am with you

echo basalt
#

I want to write unit tests for their service functions

#

I want to be able to access the functions which are inside the custom modules service files on the test file

#

but based on the medusa documentation, I can only using service functions on other services, api endpoints using req.resolve, and subscriber files

dry plover
#

You import the service in your test file and mock the dependencies of that service

echo basalt
#

I couldn't find a way to call the service functions inside my jest test files

dry plover
#

You can test a service like in any other app

echo basalt
#

suppose this is my service file

dry plover
#

I shared the different approaches in the previous messages. Maybe @queen jackal will be better pedagogic than me ahah

echo basalt
#

this is the service file

dry plover
#

Perfect, here you can follow what i ve mentioned above

#

From a classical approach it would look like

const service = new Myservice({ manager: mockManager, etc })

#

Then service.method(arg1, arg2)

#

With a container approach, you can look at the fixtures of the file i shared previously, which show you a service like yours being tested using a container approach

#

I have literally shared you a file where a service is being tested 😊

queen jackal
#

Okey let's go am back to my desktop

#

Maybe you need a second PoV

echo basalt
#

can you join in a short 5 minutes call with me? @queen jackal

queen jackal
#

The problem is not easy to understand because it calls Jest and Medusa Extender

echo basalt
#

@dry plover

dry plover
#

You are missing the other props of the container you are using. Among them, the repo that you can mock as well

echo basalt
#

Argument of type '{ manager: any; }' is not assignable to parameter of type 'InjectedDependencies'.

#

how?

dry plover
#

You have to provide what is expected indeed. I suggest you to acknowledge the payment provider test and fixtures i ve shared you