I'm writing some tests for my node application with Jest.
My unit tests rely on spyOn to mock some functions:
const instrumentsData = require('../../../../dataAccess/instrumentsData');
describe('Test updateInstrument', () => {
let getSpyInstruments: jest.SpyInstance;
it('Update instrument - success', async () => {
getSpyInstruments = jest.spyOn(instrumentsData, 'update');
getSpyInstruments.mockReturnValueOnce({_id: 'testID', ...mockInstrumentParams});
InstrumentsData is a module that exports various function, like update.
Since I want to use the import syntax I changed the import like the following:
import * as instrumentsData from '../../../../dataAccess/instrumentsData';
With instrumentsData it works, because there are multiple functions being returned, without a default export. If I'm doing the same with a file with a single default export (like currenciesData, that contains the default export for findOneCurrency), it triggers this error:
getSpyCurrencies = jest.spyOn(currenciesData, 'findOneCurrency');
No overload matches this call. Overload 1 of 4, '(object: typeof import("project/src/dataAccess/currenciesData"), method: "default"): SpyInstance<Promise<LeanDocument<ICurrencyModel & { _id: any; }>>, [id: ...]>', gave the following error. Argument of type '"findOneCurrency"' is not assignable to parameter of type '"default"'. Overload 2 of 4, '(object: typeof import("project/src/dataAccess/currenciesData"), method: never): SpyInstance<never, never>', gave the following error. Argument of type 'string' is not assignable to parameter of type 'never'.ts(2769)
How can I fix this?