@@ -2,11 +2,21 @@ import { send, resetTransport } from '../src/index';
22import { createSmtpCatcher } from './smtp-catcher' ;
33
44describe ( 'send' , ( ) => {
5+ const ORIGINAL_ENV = { ...process . env } ;
6+
57 afterEach ( ( ) => {
68 resetTransport ( ) ;
9+ // Remove any env var added during the test
10+ for ( const key of Object . keys ( process . env ) ) {
11+ if ( ! ( key in ORIGINAL_ENV ) ) {
12+ delete process . env [ key ] ;
13+ }
14+ }
15+ // Restore original values
16+ Object . assign ( process . env , ORIGINAL_ENV ) ;
717 } ) ;
818
9- it ( 'sends email via SMTP catcher' , async ( ) => {
19+ it ( 'sends email via SMTP catcher with overrides ' , async ( ) => {
1020 const catcher = await createSmtpCatcher ( ) ;
1121
1222 try {
@@ -33,4 +43,76 @@ describe('send', () => {
3343 await catcher . stop ( ) ;
3444 }
3545 } , 10000 ) ;
46+
47+ it ( 'sends email using SMTP config from environment variables' , async ( ) => {
48+ const catcher = await createSmtpCatcher ( ) ;
49+
50+ // Set SMTP config via environment variables
51+ process . env . SMTP_HOST = catcher . host ;
52+ process . env . SMTP_PORT = String ( catcher . port ) ;
53+ process . env . SMTP_SECURE = 'false' ;
54+ process . env . SMTP_FROM = 'env-sender@example.com' ;
55+ process . env . SMTP_TLS_REJECT_UNAUTHORIZED = 'false' ;
56+ process . env . SMTP_REQUIRE_TLS = 'false' ;
57+
58+ // Clear cached transport to pick up new env vars
59+ resetTransport ( ) ;
60+
61+ try {
62+ // Send without overrides - should use env vars
63+ await send ( {
64+ to : 'recipient@example.com' ,
65+ subject : 'Email from env config' ,
66+ text : 'This email was sent using env var configuration.'
67+ } ) ;
68+
69+ const message = await catcher . waitForMessage ( 5000 ) ;
70+
71+ expect ( message . raw ) . toContain ( 'Subject: Email from env config' ) ;
72+ expect ( message . raw ) . toContain ( 'This email was sent using env var configuration.' ) ;
73+ expect ( message . raw ) . toContain ( 'From: env-sender@example.com' ) ;
74+ } finally {
75+ await catcher . stop ( ) ;
76+ }
77+ } , 10000 ) ;
78+
79+ it ( 'overrides take precedence over environment variables' , async ( ) => {
80+ const catcher = await createSmtpCatcher ( ) ;
81+
82+ // Set env vars with WRONG port - would fail if used
83+ process . env . SMTP_HOST = '127.0.0.1' ;
84+ process . env . SMTP_PORT = '59999' ;
85+ process . env . SMTP_SECURE = 'false' ;
86+ process . env . SMTP_FROM = 'env-sender@example.com' ;
87+
88+ // Clear cached transport
89+ resetTransport ( ) ;
90+
91+ try {
92+ // Send with overrides - should use overrides (correct port), not env vars
93+ await send (
94+ {
95+ to : 'recipient@example.com' ,
96+ subject : 'Override test' ,
97+ text : 'This email should use override config, not env.'
98+ } ,
99+ {
100+ host : catcher . host ,
101+ port : catcher . port , // Correct port from catcher
102+ secure : false ,
103+ from : 'override-sender@example.com' ,
104+ tlsRejectUnauthorized : false
105+ }
106+ ) ;
107+
108+ const message = await catcher . waitForMessage ( 5000 ) ;
109+
110+ expect ( message . raw ) . toContain ( 'Subject: Override test' ) ;
111+ expect ( message . raw ) . toContain ( 'This email should use override config' ) ;
112+ expect ( message . raw ) . toContain ( 'From: override-sender@example.com' ) ;
113+ expect ( message . raw ) . not . toContain ( 'From: env-sender@example.com' ) ;
114+ } finally {
115+ await catcher . stop ( ) ;
116+ }
117+ } , 10000 ) ;
36118} ) ;
0 commit comments