Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

318
Views
Using jest.spyOn with import module syntax

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!