Skip to content

Commit 61d3961

Browse files
authored
fix(email-resend): http fetch error handling swallows errors (#17798)
copy of #17795 for v3
1 parent 5061283 commit 61d3961

3 files changed

Lines changed: 267 additions & 107 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { Payload } from 'payload'
2+
3+
import { describe, expect, it } from 'vitest'
4+
5+
import { resendAdapter } from './index.js'
6+
7+
/**
8+
* Real end-to-end checks against the live Resend API. Skipped unless RESEND_API_KEY
9+
* is set, so the default unit run and CI never hit the network.
10+
*
11+
* Run with:
12+
* RESEND_API_KEY=re_xxx pnpm vitest run packages/email-resend/src/email-resend.int.spec.ts
13+
*
14+
* Uses Resend's sandbox identities, which require no domain verification:
15+
* from: onboarding@resend.dev
16+
* to: delivered@resend.dev (also bounced@resend.dev / complained@resend.dev)
17+
*/
18+
describe.skipIf(!process.env.RESEND_API_KEY)('email-resend (integration)', () => {
19+
const mockPayload = {} as unknown as Payload
20+
21+
const adapter = (apiKey: string) =>
22+
resendAdapter({
23+
apiKey,
24+
defaultFromAddress: 'onboarding@resend.dev',
25+
defaultFromName: 'Payload Integration Test',
26+
})({ payload: mockPayload })
27+
28+
it('should send a real email and return a Resend id', async () => {
29+
const result = (await adapter(process.env.RESEND_API_KEY!).sendEmail({
30+
from: 'Payload <onboarding@resend.dev>',
31+
subject: 'Payload email-resend integration test',
32+
text: 'This message was sent by the email-resend integration test.',
33+
to: 'delivered@resend.dev',
34+
})) as { id: string }
35+
36+
expect(result.id).toEqual(expect.any(String))
37+
expect(result.id.length).toBeGreaterThan(0)
38+
})
39+
40+
it('should throw a descriptive APIError for an invalid API key', async () => {
41+
// Exercises the real Resend error path end-to-end (JSON 401/403 body),
42+
// proving the adapter surfaces the reason instead of a JSON parse error.
43+
await expect(() =>
44+
adapter('re_invalid_key_for_testing').sendEmail({
45+
from: 'Payload <onboarding@resend.dev>',
46+
subject: 'Should fail',
47+
text: 'Should fail',
48+
to: 'delivered@resend.dev',
49+
}),
50+
).rejects.toThrow(/Error sending email/)
51+
})
52+
})

0 commit comments

Comments
 (0)