Merci pour m'aider avec un test difficile d'Electron. Maintenant je peu faire un mock de l'object BrowserWindow et tester le classes de niveu plus bas. Il-y-a un example:
import{MyWindow}from'./my-window';import{mocked}from'ts-jest/utils';import{BrowserWindow}from'electron';jest.mock('electron',()=>({BrowserWindow:jest.fn(),}));describe('My Window Test',()=>{it('should show the window',()=>{constwindowShow=jest.fn();constwindowMock:any=mocked(BrowserWindow,true);windowMock.mockImplementation(()=>({show:windowShow,loadURL:jest.fn(),}));constmyWindow=newMyWindow();myWindow.openWindow();expect(windowShow).toHaveBeenCalled();});});
I try to mock a FeathersJS service class with your article example without any success.
I cannot figure out how to check class methods calls.
I ended up an another way with this:
import{mocked}from'ts-jest/utils';importappfrom'../../src/app';import{Notifications}from'../../src/services/notifications/notifications.class';import{NotificationsData}from'../../src/types';jest.mock('../../src/services/notifications/notifications.class');constmockNotificationsService=mocked(Notifications,true);constmockNotificationsCreate=mocked(mockNotificationsService.mock.instances[0].create)mockNotificationsCreate.mockImplementation(()=>Promise.resolve({}asNotificationsData));beforeEach(()=>{// Clear registered calls from older tests.mockNotificationsCreate.mockClear();})describe('\'foo\' service',()=>{it('mock implement test',()=>{returnapp.service('foo').create({foo:'bar'}).then(()=>{expect(mockNotificationsCreate).toBeCalledTimes(1);expect(mockNotificationsCreate).toHaveBeenNthCalledWith(1,{applicationSlug:'kidways',body:'body',title:'title',userIds:['user1']},{});})},);it('mock implement test second',()=>{returnapp.service('foo').create({foo:42}).then(()=>{expect(mockNotificationsCreate).toBeCalledTimes(2);expect(mockNotificationsCreate).toHaveBeenNthCalledWith(1,{applicationSlug:'slug',body:'body',title:'title',userIds:['user1']},{});expect(mockNotificationsCreate).toHaveBeenNthCalledWith(2,{applicationSlug:'slug',body:'42',title:'title',userIds:['user1']},{});})},);});
This works but the implements looks wrong. I have to first mock the whole class with jest.mock then mock one of the method while storing it on a variable for test check.
I got an error saying X.mockClear is not a function (X is the class I want to mock). I can't find anything wrong with my setup. Could you help suggest what I can do to resolve it. Thanks
I also found value in understanding what the Mock Implementation is doing. In this link, jonathancreamer.com/testing-typesc..., explains what that is before mockImplementation was created. An assumption on creation regardless it explains that well which makes this article very fluent.
Thanks. It looks pretty easy.
I was wondering when a mockedClass is re-used throughout the project, how this may look like. jest.mock() does not return anything, does it?
We're a place where coders share, stay up-to-date and grow their careers.
Merci pour m'aider avec un test difficile d'Electron. Maintenant je peu faire un mock de l'object BrowserWindow et tester le classes de niveu plus bas. Il-y-a un example:
how do you call the
playSoundFilemethod?What took me forever to realize is the jest.mock has to be done above the describe block.
additionally, instead of using this library, I now use
Update: I now use the following method: dev.to/bmitchinson/mocking-classes...
I try to mock a FeathersJS service class with your article example without any success.
I cannot figure out how to check class methods calls.
I ended up an another way with this:
This works but the implements looks wrong. I have to first mock the whole class with
jest.mockthen mock one of the method while storing it on a variable for test check.Is it the way to go? Will you do the same way?
Thanks!
mocked()function from ts-jest saved my life! Thank you for the tip :)You're welcome!
I got an error saying X.mockClear is not a function (X is the class I want to mock). I can't find anything wrong with my setup. Could you help suggest what I can do to resolve it. Thanks
@devakone thanks a lot man! 🚀
Merci Beaucoup.
De rien!
Anyone looking for the link to the ts-jest mocked document, it is this kulshekhar.github.io/ts-jest/docs/...
Github and Kulshekhar have been inverted to find the docs.
I also found value in understanding what the Mock Implementation is doing. In this link, jonathancreamer.com/testing-typesc..., explains what that is before mockImplementation was created. An assumption on creation regardless it explains that well which makes this article very fluent.
Thanks. It looks pretty easy.
I was wondering when a mockedClass is re-used throughout the project, how this may look like. jest.mock() does not return anything, does it?