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

173
Views
Trouble mocking ES6 Module using jest.unstable_mockModule

I'm attempting to mock a call to a class instance function on an ES6 module being imported by the code under test. I've followed the progress of the ES6 support and eventually stumbled onto this PR https://github.com/facebook/jest/pull/10976 which mentioned that in 27.1.1 support for jest.unstable_mockModule was added. I upgraded my version of Jest to take advantage and while the test doesn't error, it also doesn't seem to actually mock the module either.

This is the module under test:

// src/Main.mjs

import Responder from './Responder.mjs'
import Notifier from './Notifier.mjs'

export default {
  async fetch(request, environment, context) {
    let response

    try {
      response = new Responder(request, environment, context).respond()
    } catch (error) {
      return new Notifier().notify(error)
    }

    return response
  }
}

Here is the test:

// test/Main.test.mjs

import { jest } from '@jest/globals'
import main from '../src/Main.mjs'

describe('fetch', () => {
  test('Notifies on error', async () => {
    const mockNotify = jest.fn();
    
    jest.unstable_mockModule('../src/Notifier.mjs', () => ({
      notify: mockNotify
    }))

    const notifierMock = await import('../src/Notifier.mjs');
    
    await main.fetch(null, null, null)

    expect(mockNotify).toHaveBeenCalled()
  })
})

I'm trying to mock the call to Notify to expect it to have been called and while this will run, it raises an exception from inside the Notifier.notify() that is supposed to be mocked, so it appears that it isn't being mocked at all.

What am I missing? Any help is much appreciated. ๐Ÿ™

about 4 years ago ยท Juan Pablo Isaza
2 answers
Answer question

0

I believe it's because you're importing main at the start of the file. You need to do a dynamic import the same way as Notifier.mjs

// test/Main.test.mjs

import { jest } from '@jest/globals'


describe('fetch', () => {
  test('Notifies on error', async () => {
    const mockNotify = jest.fn();
    
    jest.unstable_mockModule('../src/Notifier.mjs', () => ({
      notify: mockNotify
    }))

    const notifierMock = await import('../src/Notifier.mjs');
    const main = await import('../src/Main.mjs');
    
    await main.fetch(null, null, null)

    expect(mockNotify).toHaveBeenCalled()
  })
})
about 4 years ago ยท Juan Pablo Isaza Report

0

I don't exactly understand the end confusion. But for fundamentals, to mock a particular attribute/method of an imported class, you have to mock the class itself and then mock the internal functions.

Therefore, as per your code, you may try something like:

import { jest } from '@jest/globals'
import main from '../src/Main.mjs'
import Notifier from './Notifier.mjs'
jest.mock('./Notifier.mjs', () => ({
    notify: jest.fn()
}))

describe('fetch', () => {
  test('Notifies on error', async () => {
    await main.fetch(null, null, null)
    expect(mockNotify).toHaveBeenCalled()
  })
})
about 4 years ago ยท Juan Pablo Isaza Report
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!