|
|
@@ -5,6 +5,7 @@ import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
|
|
|
import {
|
|
|
EventBus,
|
|
|
LanguageCode,
|
|
|
+ Logger,
|
|
|
Order,
|
|
|
OrderStateTransitionEvent,
|
|
|
PluginCommonModule,
|
|
|
@@ -12,6 +13,7 @@ import {
|
|
|
RequestContext,
|
|
|
VendureEvent,
|
|
|
} from '@vendure/core';
|
|
|
+import { TestingLogger } from '@vendure/testing';
|
|
|
import path from 'path';
|
|
|
|
|
|
import { orderConfirmationHandler } from './default-email-handlers';
|
|
|
@@ -25,6 +27,8 @@ describe('EmailPlugin', () => {
|
|
|
let onSend: jest.Mock;
|
|
|
let module: TestingModule;
|
|
|
|
|
|
+ const testingLogger = new TestingLogger(() => jest.fn());
|
|
|
+
|
|
|
async function initPluginWithHandlers(
|
|
|
handlers: Array<EmailEventHandler<string, any>>,
|
|
|
options?: Partial<EmailPluginOptions>,
|
|
|
@@ -51,6 +55,9 @@ describe('EmailPlugin', () => {
|
|
|
providers: [MockService],
|
|
|
}).compile();
|
|
|
|
|
|
+ Logger.useLogger(testingLogger);
|
|
|
+ module.useLogger(new Logger());
|
|
|
+
|
|
|
const plugin = module.get(EmailPlugin);
|
|
|
eventBus = module.get(EventBus);
|
|
|
await plugin.onVendureBootstrap();
|
|
|
@@ -492,6 +499,71 @@ describe('EmailPlugin', () => {
|
|
|
expect(onSend.mock.calls[0][0].subject).toBe(`Order confirmation for #${order.code}`);
|
|
|
});
|
|
|
});
|
|
|
+
|
|
|
+ describe('error handling', () => {
|
|
|
+ it('Logs an error if the template file is not found', async () => {
|
|
|
+ const ctx = RequestContext.deserialize({
|
|
|
+ _channel: { code: DEFAULT_CHANNEL_CODE },
|
|
|
+ _languageCode: LanguageCode.en,
|
|
|
+ } as any);
|
|
|
+ testingLogger.errorSpy.mockClear();
|
|
|
+
|
|
|
+ const handler = new EmailEventListener('no-template')
|
|
|
+ .on(MockEvent)
|
|
|
+ .setFrom('"test from" <noreply@test.com>')
|
|
|
+ .setRecipient(() => 'test@test.com')
|
|
|
+ .setSubject('Hello {{ subjectVar }}');
|
|
|
+
|
|
|
+ await initPluginWithHandlers([handler]);
|
|
|
+
|
|
|
+ eventBus.publish(new MockEvent(ctx, true));
|
|
|
+ await pause();
|
|
|
+ expect(testingLogger.errorSpy.mock.calls[0][0]).toContain(`ENOENT: no such file or directory`);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('Logs a Handlebars error if the template is invalid', async () => {
|
|
|
+ const ctx = RequestContext.deserialize({
|
|
|
+ _channel: { code: DEFAULT_CHANNEL_CODE },
|
|
|
+ _languageCode: LanguageCode.en,
|
|
|
+ } as any);
|
|
|
+ testingLogger.errorSpy.mockClear();
|
|
|
+
|
|
|
+ const handler = new EmailEventListener('bad-template')
|
|
|
+ .on(MockEvent)
|
|
|
+ .setFrom('"test from" <noreply@test.com>')
|
|
|
+ .setRecipient(() => 'test@test.com')
|
|
|
+ .setSubject('Hello {{ subjectVar }}');
|
|
|
+
|
|
|
+ await initPluginWithHandlers([handler]);
|
|
|
+
|
|
|
+ eventBus.publish(new MockEvent(ctx, true));
|
|
|
+ await pause();
|
|
|
+ expect(testingLogger.errorSpy.mock.calls[0][0]).toContain(`Parse error on line 3:`);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('Logs an error if the loadData method throws', async () => {
|
|
|
+ const ctx = RequestContext.deserialize({
|
|
|
+ _channel: { code: DEFAULT_CHANNEL_CODE },
|
|
|
+ _languageCode: LanguageCode.en,
|
|
|
+ } as any);
|
|
|
+ testingLogger.errorSpy.mockClear();
|
|
|
+
|
|
|
+ const handler = new EmailEventListener('bad-template')
|
|
|
+ .on(MockEvent)
|
|
|
+ .setFrom('"test from" <noreply@test.com>')
|
|
|
+ .setRecipient(() => 'test@test.com')
|
|
|
+ .setSubject('Hello {{ subjectVar }}')
|
|
|
+ .loadData(context => {
|
|
|
+ throw new Error('something went horribly wrong!');
|
|
|
+ });
|
|
|
+
|
|
|
+ await initPluginWithHandlers([handler]);
|
|
|
+
|
|
|
+ eventBus.publish(new MockEvent(ctx, true));
|
|
|
+ await pause();
|
|
|
+ expect(testingLogger.errorSpy.mock.calls[0][0]).toContain(`something went horribly wrong!`);
|
|
|
+ });
|
|
|
+ });
|
|
|
});
|
|
|
|
|
|
const pause = () => new Promise(resolve => setTimeout(resolve, 100));
|