test/client: fix callback counter test

This commit is contained in:
Zack Schuster 2020-10-30 14:34:09 -07:00 committed by Zack Schuster
parent d799559261
commit 9cd062f10d
1 changed files with 33 additions and 7 deletions

View File

@ -63,19 +63,45 @@ test.after(async (t) => {
}); });
test('client invokes callback exactly once for invalid connection', async (t) => { test('client invokes callback exactly once for invalid connection', async (t) => {
t.plan(1);
const msg = { const msg = {
from: 'foo@bar.baz', from: 'foo@bar.baz',
to: 'foo@bar.baz', to: 'foo@bar.baz',
subject: 'hello world', subject: 'hello world',
text: 'hello world', text: 'hello world',
}; };
try { await t.notThrowsAsync(
const invalidClient = new SMTPClient({ host: 'bar.baz' }); new Promise((resolve, reject) => {
await promisify(invalidClient.send.bind(invalidClient))(new Message(msg)); let counter = 0;
} catch (err) { const invalidClient = new SMTPClient({ host: 'bar.baz' });
t.true(err instanceof Error); const incrementListener = () => {
} if (counter > 0) {
reject();
} else {
counter++;
}
};
invalidClient.smtp.addListener('incrementTestCounter', incrementListener);
invalidClient.send(new Message(msg), (err) => {
if (err == null || counter > 0) {
reject();
} else {
invalidClient.smtp.emit('incrementTestCounter');
}
});
// @ts-expect-error the error event is only accessible from the protected socket property
invalidClient.smtp.sock.once('error', () => {
invalidClient.smtp.removeListener(
'incrementTestCounter',
incrementListener
);
if (counter === 1) {
resolve();
} else {
reject();
}
});
})
);
}); });
test('client has a default connection timeout', async (t) => { test('client has a default connection timeout', async (t) => {