-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement tests for all basic functionality
Also fix sendMail's options to accept the template and context properties.
- Loading branch information
Showing
8 changed files
with
4,670 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
require('ts-node/register'); | ||
require('./server/polyfills'); | ||
|
||
module.exports = { | ||
'moduleFileExtensions': [ | ||
'ts', | ||
'js', | ||
'json' | ||
'json', | ||
'ts', | ||
], | ||
'transform': { | ||
'^.+\\.ts$': 'ts-jest' | ||
}, | ||
'testRegex': '\/lib\/.*\\.spec\\.(ts|js)$', | ||
'rootDir': 'lib', | ||
'testRegex': '/lib/.*\\.spec\\.(ts|js)$', | ||
'globals': { | ||
'ts-jest': { | ||
'tsConfigFile': 'tsconfig.json' | ||
'tsConfig': 'tsconfig.json' | ||
} | ||
} | ||
}, | ||
'preset': 'ts-jest', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** Dependencies **/ | ||
import { SendMailOptions } from 'nodemailer'; | ||
|
||
export interface MailerSendMailOptions extends SendMailOptions { | ||
template?: string; | ||
context?: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import MailMessage = require('nodemailer/lib/mailer/mail-message'); | ||
import SMTPTransport = require('nodemailer/lib/smtp-transport'); | ||
import { MAILER_OPTIONS } from "./constants/mailer-options.constant"; | ||
import { MailerOptions } from "./interfaces/mailer-options.interface"; | ||
import { MailerService } from "./mailer.service"; | ||
import { HandlebarsAdapter } from './adapters/handlebars.adapter'; | ||
import { PugAdapter } from './adapters/pug.adapter'; | ||
|
||
/** | ||
* Common testing code for testing up a testing module and MailerService | ||
*/ | ||
async function getMailerServiceForOptions( | ||
options: MailerOptions | ||
): Promise<MailerService> { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
{ | ||
name: MAILER_OPTIONS, | ||
provide: MAILER_OPTIONS, | ||
useValue: options | ||
}, | ||
MailerService | ||
] | ||
}).compile(); | ||
|
||
const service = module.get<MailerService>(MailerService); | ||
|
||
return service; | ||
} | ||
|
||
/** | ||
* Common testing code for spying on the SMTPTransport's send() implementation | ||
*/ | ||
function spyOnSmtpSend(onMail: (mail: MailMessage) => void) { | ||
return jest.spyOn(SMTPTransport.prototype, 'send').mockImplementation(function (mail: MailMessage, callback: (err: Error | null, info: SMTPTransport.SentMessageInfo) => void): void { | ||
onMail(mail); | ||
callback(null, { | ||
envelope: { | ||
from: mail.data.from as string, | ||
to: [mail.data.to as string] | ||
}, | ||
messageId: 'ABCD' | ||
}); | ||
}); | ||
} | ||
|
||
describe("MailerService", () => { | ||
it("should not be defined if a transport is not provided", async () => { | ||
await expect(getMailerServiceForOptions({})).rejects.toMatchInlineSnapshot( | ||
`[Error: Make sure to provide a nodemailer transport configuration object, connection url or a transport plugin instance.]` | ||
); | ||
}); | ||
|
||
it("should accept a smtp transport string", async () => { | ||
const service = await getMailerServiceForOptions({ | ||
transport: "smtps://[email protected]:[email protected]" | ||
}); | ||
|
||
expect(service).toBeDefined(); | ||
expect((service as any).transporter.transporter).toBeInstanceOf(SMTPTransport); | ||
}); | ||
|
||
it("should accept smtp transport options", async () => { | ||
const service = await getMailerServiceForOptions({ | ||
transport: { | ||
secure: true, | ||
auth: { | ||
user: '[email protected]', | ||
pass: 'pass', | ||
}, | ||
options: { | ||
host: 'smtp.domain.com', | ||
}, | ||
} | ||
}); | ||
|
||
expect(service).toBeDefined(); | ||
expect((service as any).transporter.transporter).toBeInstanceOf(SMTPTransport); | ||
}); | ||
|
||
|
||
it("should accept a smtp transport instance", async () => { | ||
const transport = new SMTPTransport({}) | ||
const service = await getMailerServiceForOptions({ | ||
transport: transport | ||
}); | ||
|
||
expect(service).toBeDefined(); | ||
expect((service as any).transporter.transporter).toBe(transport); | ||
}); | ||
|
||
it('should send emails with nodemailer', async () => { | ||
let lastMail: MailMessage; | ||
const send = spyOnSmtpSend((mail: MailMessage) => { | ||
lastMail = mail; | ||
}); | ||
|
||
const service = await getMailerServiceForOptions({ | ||
transport: "smtps://[email protected]:[email protected]" | ||
}); | ||
|
||
await service.sendMail({ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Test', | ||
html: 'This is test.' | ||
}); | ||
|
||
expect(send).toHaveBeenCalled(); | ||
expect(lastMail.data.from).toBe('[email protected]'); | ||
expect(lastMail.data.to).toBe('[email protected]'); | ||
expect(lastMail.data.subject).toBe('Test'); | ||
expect(lastMail.data.html).toBe('This is test.'); | ||
}); | ||
|
||
it('should use mailerOptions.defaults when send emails', async () => { | ||
let lastMail: MailMessage; | ||
const send = spyOnSmtpSend((mail: MailMessage) => { | ||
lastMail = mail; | ||
}); | ||
|
||
const service = await getMailerServiceForOptions({ | ||
transport: "smtps://[email protected]:[email protected]", | ||
defaults: { | ||
from: '[email protected]' | ||
} | ||
}); | ||
|
||
await service.sendMail({ | ||
to: '[email protected]', | ||
subject: 'Test', | ||
html: 'This is test.' | ||
}); | ||
|
||
expect(send).toHaveBeenCalled(); | ||
expect(lastMail.data.from).toBe('[email protected]'); | ||
}); | ||
|
||
it('should compile template with the handlebars adapter', async () => { | ||
let lastMail: MailMessage; | ||
const send = spyOnSmtpSend((mail: MailMessage) => { | ||
lastMail = mail; | ||
}); | ||
|
||
const service = await getMailerServiceForOptions({ | ||
transport: new SMTPTransport({}), | ||
template: { | ||
adapter: new HandlebarsAdapter(), | ||
} | ||
}); | ||
|
||
await service.sendMail({ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Test', | ||
template: __dirname + '/test-templates/handlebars-template', | ||
context: { | ||
world: 'World', | ||
}, | ||
}); | ||
|
||
expect(send).toHaveBeenCalled(); | ||
expect(lastMail.data.from).toBe('[email protected]'); | ||
expect(lastMail.data.html).toBe('<p>Handlebars test template.</p>\n<p>Hello World!</p>\n'); | ||
}); | ||
|
||
it('should compile template with the pug adapter', async () => { | ||
let lastMail: MailMessage; | ||
const send = spyOnSmtpSend((mail: MailMessage) => { | ||
lastMail = mail; | ||
}); | ||
|
||
const service = await getMailerServiceForOptions({ | ||
transport: new SMTPTransport({}), | ||
template: { | ||
adapter: new PugAdapter(), | ||
} | ||
}); | ||
|
||
await service.sendMail({ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Test', | ||
template: __dirname + '/test-templates/pug-template', | ||
context: { | ||
world: 'World', | ||
}, | ||
}); | ||
|
||
expect(send).toHaveBeenCalled(); | ||
expect(lastMail.data.from).toBe('[email protected]'); | ||
expect(lastMail.data.html).toBe('<p>Pug test template.</p><p>Hello World!</p>'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<p>Handlebars test template.</p> | ||
<p>Hello {{world}}!</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
p Pug test template. | ||
p= "Hello " + world + "!" |
Oops, something went wrong.