@@ -2,12 +2,18 @@ import { send, resetTransport } from '../src/index';
22import { createSmtpCatcher } from './smtp-catcher' ;
33
44describe ( 'send' , ( ) => {
5- const originalEnv = { ...process . env } ;
5+ const ORIGINAL_ENV = { ...process . env } ;
66
77 afterEach ( ( ) => {
88 resetTransport ( ) ;
9- // Restore original env vars
10- process . env = { ...originalEnv } ;
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 ) ;
1117 } ) ;
1218
1319 it ( 'sends email via SMTP catcher with overrides' , async ( ) => {
@@ -67,4 +73,44 @@ describe('send', () => {
6773 await catcher . stop ( ) ;
6874 }
6975 } , 10000 ) ;
76+
77+ it ( 'overrides take precedence over environment variables' , async ( ) => {
78+ const catcher = await createSmtpCatcher ( ) ;
79+
80+ // Set env vars with WRONG port - would fail if used
81+ process . env . SMTP_HOST = '127.0.0.1' ;
82+ process . env . SMTP_PORT = '59999' ;
83+ process . env . SMTP_SECURE = 'false' ;
84+ process . env . SMTP_FROM = 'env-sender@example.com' ;
85+
86+ // Clear cached transport
87+ resetTransport ( ) ;
88+
89+ try {
90+ // Send with overrides - should use overrides (correct port), not env vars
91+ await send (
92+ {
93+ to : 'recipient@example.com' ,
94+ subject : 'Override test' ,
95+ text : 'This email should use override config, not env.'
96+ } ,
97+ {
98+ host : catcher . host ,
99+ port : catcher . port , // Correct port from catcher
100+ secure : false ,
101+ from : 'override-sender@example.com' ,
102+ tlsRejectUnauthorized : false
103+ }
104+ ) ;
105+
106+ const message = await catcher . waitForMessage ( 5000 ) ;
107+
108+ expect ( message . raw ) . toContain ( 'Subject: Override test' ) ;
109+ expect ( message . raw ) . toContain ( 'This email should use override config' ) ;
110+ expect ( message . raw ) . toContain ( 'From: override-sender@example.com' ) ;
111+ expect ( message . raw ) . not . toContain ( 'From: env-sender@example.com' ) ;
112+ } finally {
113+ await catcher . stop ( ) ;
114+ }
115+ } , 10000 ) ;
70116} ) ;
0 commit comments