I have a previously-created .js
file that mocks away some of our functions for jest
test purposes. I'm migrating that to a .ts
file:
Server.ts
const Server = jest.genMockFromModule('../Server');
Server.getAsync = Server.default.getAsync;
// other REST-ful functions here
export default Server;
I am getting the following errors:
Property 'getAsync' does not exist on type '{}'
Property 'default' does not exist on type '{}'
Then, in a corresponding test file:
MyComponent.test.ts
import Server from 'path/to/Server';
jest.mock('path/to/Server');
const dispatchMock = jest.fn();
const getStateMock = jest.fn();
describe('MyComponent.someFunction', () => {
beforeEach(() => {
jest.resetAllMocks();
});
it('Does the right stuff', () => {
Server.getAsync.mockReturnValueOnce(Promise.resolve([{ key: 'value' }]));
dispatchMock.mockImplementationOnce((promise) => promise);
dispatchMock.mockImplementationOnce();
return someFunction()(dispatchMock)
.then(() => {
expect(Server.getAsync).toHaveBeenCalledTimes(1);
expect(Server.getAsync.mock.calls[0][0]).toBe('something');
});
});
});
I am getting errors on dispatchMock.mockImplementationOnce()
Expected 1 arguments, but got 0. (method) jest.MockInstance<{}>.mockImplementationOnce(fn: (...args: any[]) => any): jest.Mock<{}>
...on Server.getAsync.mockReturnValueOnce
Property 'mockReturnValueOnce' does not exist on type '(url: string, baseRoute?: string | null, loadingGenerator?: (isLoading: boolean) => { type: strin...'.
...and on Server.getAsync.mock
Property 'mock' does not exist on type '(url: string, baseRoute?: string | null, loadingGenerator?: (isLoading: boolean) => { type: strin...'.
I've been pounding my head on this for a while so any help would be greatly appreciated.
UPDATE
Okay, I added as any
to the end of the first line of my Server.ts
file so now it looks like:
const Server = jest.genMockFromModule('../Server') as any;
That got rid of the first set of errors. Still facing the errors in my .test.ts
file though.
UPDATE 2
I've noticed that when I run the actual jest tests, that they all pass even though there are TypeErrors. These issues don't appear to be related to actual tests.