react-confirm-alert

Testing

Mock the Promise API, or test through the real dialog.

Mock

Vitest
vi.mock('react-confirm-alert', () => ({
  confirm: vi.fn().mockResolvedValue(true),
}))
Jest
jest.mock('react-confirm-alert', () => ({
  confirm: jest.fn().mockResolvedValue(true),
}))

confirmAlert returns void, so this type-checks:

import * as lib from 'react-confirm-alert'

jest.spyOn(lib, 'confirmAlert').mockImplementation(() => {})

Through the real dialog

jsdom has no showModal, so the dialog renders as a plain element with the same roles:

const result = confirm({ title: 'Delete?' })
const button = await screen.findByRole('button', { name: 'Confirm' })
await user.click(button)
expect(await result).toBe(true)

Jest needs CSS imports mapped:

jest.config.js
moduleNameMapper: { '\\.css$': 'identity-obj-proxy' }

On this page