#Medusa Extender Custom Service
33 messages · Page 1 of 1 (latest)
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
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
Okey I am here I just finish to take my walk and am with you
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
You import the service in your test file and mock the dependencies of that service
I couldn't find a way to call the service functions inside my jest test files
You can test a service like in any other app
suppose this is my service file
I shared the different approaches in the previous messages. Maybe @queen jackal will be better pedagogic than me ahah
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 😊
can you join in a short 5 minutes call with me? @queen jackal
The problem is not easy to understand because it calls Jest and Medusa Extender
You are missing the other props of the container you are using. Among them, the repo that you can mock as well
Argument of type '{ manager: any; }' is not assignable to parameter of type 'InjectedDependencies'.
how?
You have to provide what is expected indeed. I suggest you to acknowledge the payment provider test and fixtures i ve shared you