'Trying to identify the open handle in an email sending module with nodemailer
I am creating a node module to send emails with nodemailer. The implementation is similar to what you see in the nodemailer module demo:
'use strict';
import nodemailer, { Transporter } from 'nodemailer';
import { EmailSender, EmailReceiver } from '../types/email';
declare global {
namespace NodeJS {
interface ProcessEnv {
SMTP_PORT: number;
SMTP_HOST: string;
SMTP_USER: string;
SMTP_PASSWORD: string;
}
}
}
async function sendEmail (sender: EmailSender, receiver: EmailReceiver, subject: string, text: string, html: string) {
const transporter: Transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST ?? '',
port: process.env.SMTP_PORT,
secure: process.env.NODE_ENV !== 'development',
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD,
},
});
try {
const info = await transporter.sendMail({
from: `"${sender.name}" <${sender.email}>`,
to: `"${receiver.name}" <${receiver.email}>`,
subject: subject,
text, html
});
return info.messageId;
} catch (error) {
console.error(error);
}
}
export { sendEmail };
After sending the email, the email id is received. Apparently it works fine, but when implementing the test with jest an open handle is detected.
import { createTestAccount, TestAccount } from 'nodemailer';
import { sendEmail } from '../src/email';
import { EmailSender, EmailReceiver } from '../src/types/email';
let sender: EmailSender;
let receiver: EmailReceiver;
let testAccount: TestAccount | null;
beforeAll(async () => {
testAccount = await createTestAccount();
process.env.NODE_ENV = 'development';
process.env.SMTP_USER = testAccount.user;
process.env.SMTP_PASSWORD = testAccount.pass;
process.env.SMTP_HOST = 'smtp.ethereal.email';
process.env.SMTP_PORT = 465;
sender = {
name: 'Test Kemus',
email: '[email protected]'
};
receiver = {
name: 'Test Receiver',
email: '[email protected]'
};
});
afterAll(() => {
testAccount = null;
});
describe('Send email function', () => {
it('should be defined and truthy', () => {
expect(sendEmail).toBeDefined();
expect(sendEmail).toBeTruthy();
});
it('should be a function', () => {
expect(sendEmail).toBeInstanceOf(Function);
});
});
describe('Send email', () => {
test('should send an email', async () => {
const subject = 'Test subject';
const text = 'Test text';
const html = '<h1>Test html</h1>';
try {
const info = await sendEmail(sender, receiver, subject, text, html);
expect(info).toBeTruthy();
expect(info).toBeInstanceOf(String);
} catch (error) {
console.error(error);
}
});
});
The test that gives an error is "should send an email":
thrown: "Exceeded timeout of 5000 ms for a test. Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
When I run the tests with the --detectOpenHandles flag, it shows me the following error:
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● TCPWRAP
27 |
28 | try {
> 29 | const info = await transporter.sendMail({
| ^
30 | from: `"${sender.name}" <${sender.email}>`,
31 | to: `"${receiver.name}" <${receiver.email}>`,
32 | subject: subject,
at SMTPTransport.getSocket (node_modules/nodemailer/lib/smtp-transport/index.js:70:16)
at SMTPTransport.send (node_modules/nodemailer/lib/smtp-transport/index.js:136:14)
at node_modules/nodemailer/lib/mailer/index.js:218:34
at Mail._processPlugins (node_modules/nodemailer/lib/mailer/index.js:266:20)
at node_modules/nodemailer/lib/mailer/index.js:188:18
at processPlugins (node_modules/nodemailer/lib/mailer/index.js:279:28)
at node_modules/nodemailer/lib/mailer/index.js:287:17
at Mail._convertDataImages (node_modules/nodemailer/lib/mailer/index.js:391:20)
at Mail._defaultPlugins.compile (node_modules/nodemailer/lib/mailer/index.js:31:41)
at processPlugins (node_modules/nodemailer/lib/mailer/index.js:283:13)
at Mail._processPlugins (node_modules/nodemailer/lib/mailer/index.js:291:9)
at Mail.sendMail (node_modules/nodemailer/lib/mailer/index.js:168:14)
at src/email/index.ts:29:36
at src/email/index.ts:8:71
at Object.<anonymous>.__awaiter (src/email/index.ts:4:12)
at sendEmail (src/email/index.ts:19:12)
at tests/send-email.test.ts:50:35
at tests/send-email.test.ts:8:71
at Object.<anonymous>.__awaiter (tests/send-email.test.ts:4:12)
at Object.<anonymous> (tests/send-email.test.ts:44:43)
I can't understand what the open handle is.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
