From 42696566f674489dfc3b0df62f5e10340c4f8b31 Mon Sep 17 00:00:00 2001 From: William Valentin Date: Sat, 7 Feb 2026 10:15:40 -0800 Subject: [PATCH] fix: resolve whatsapp-web.js ESM import for Node.js v25 whatsapp-web.js lacks proper ESM named exports, causing SyntaxError on import. Switch to default import with destructuring, use InstanceType for the Client type annotation, and update test mock to provide both default and named exports. --- src/channels/whatsapp/adapter.test.ts | 16 +++++++++++----- src/channels/whatsapp/adapter.ts | 5 +++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/channels/whatsapp/adapter.test.ts b/src/channels/whatsapp/adapter.test.ts index a9efa0f..72cada4 100644 --- a/src/channels/whatsapp/adapter.test.ts +++ b/src/channels/whatsapp/adapter.test.ts @@ -32,13 +32,19 @@ function createMockClient() { let mockClient = createMockClient(); -vi.mock('whatsapp-web.js', () => ({ - Client: vi.fn().mockImplementation(() => mockClient), - LocalAuth: vi.fn().mockImplementation((opts: Record) => ({ +vi.mock('whatsapp-web.js', () => { + const Client = vi.fn().mockImplementation(() => mockClient); + const LocalAuth = vi.fn().mockImplementation((opts: Record) => ({ type: 'local', ...opts, - })), -})); + })); + return { + default: { Client, LocalAuth, MessageMedia: vi.fn() }, + Client, + LocalAuth, + MessageMedia: vi.fn(), + }; +}); import { WhatsAppAdapter, type WhatsAppAdapterConfig } from './adapter.js'; import type { InboundMessage } from '../types.js'; diff --git a/src/channels/whatsapp/adapter.ts b/src/channels/whatsapp/adapter.ts index 06007e0..37b2f0e 100644 --- a/src/channels/whatsapp/adapter.ts +++ b/src/channels/whatsapp/adapter.ts @@ -7,7 +7,8 @@ * Messages are chunked at 4096 chars (same as Telegram). */ -import { Client, LocalAuth, MessageMedia } from 'whatsapp-web.js'; +import WhatsApp from 'whatsapp-web.js'; +const { Client, LocalAuth, MessageMedia } = WhatsApp; import type { Attachment, InboundMessage, @@ -57,7 +58,7 @@ export class WhatsAppAdapter implements ChannelAdapter { readonly name = 'whatsapp'; private _status: ChannelStatus = 'disconnected'; - private client: Client | null = null; + private client: InstanceType | null = null; private messageHandler?: (msg: InboundMessage) => void; private config: WhatsAppAdapterConfig; private botId?: string;