5
5
import { test , expect , jest } from "@jest/globals" ;
6
6
import type { createTransport , Transporter } from "nodemailer" ;
7
7
import { randomToken } from "./email" ;
8
+ import type * as loggerModule from "../app/functions/server/logging" ;
8
9
9
10
jest . mock ( "nodemailer" , ( ) => {
10
11
return {
11
12
createTransport : jest . fn ( ) ,
12
13
} ;
13
14
} ) ;
14
15
16
+ jest . mock ( "../app/functions/server/logging" , ( ) => {
17
+ return {
18
+ logger : {
19
+ info : jest . fn ( ) ,
20
+ warn : jest . fn ( ) ,
21
+ error : jest . fn ( ) ,
22
+ } ,
23
+ } ;
24
+ } ) ;
25
+
15
26
test ( "EmailUtils.sendEmail before .init() fails" , async ( ) => {
16
27
expect . assertions ( 1 ) ;
17
28
@@ -25,9 +36,9 @@ test("EmailUtils.sendEmail before .init() fails", async () => {
25
36
} ) ;
26
37
27
38
test ( "EmailUtils.init with empty host uses jsonTransport" , async ( ) => {
28
- const mockedConsoleInfo = jest
29
- . spyOn ( console , "info" )
30
- . mockImplementation ( ( ) => undefined ) ;
39
+ const mockedLoggerModule = jest . requireMock < typeof loggerModule > (
40
+ "../app/functions/server/logging" ,
41
+ ) ;
31
42
const mockedNodemailer : {
32
43
createTransport : jest . MockedFunction < typeof createTransport > ;
33
44
} = jest . requireMock ( "nodemailer" ) ;
@@ -37,7 +48,7 @@ test("EmailUtils.init with empty host uses jsonTransport", async () => {
37
48
expect ( mockedNodemailer . createTransport ) . toHaveBeenCalledWith ( {
38
49
jsonTransport : true ,
39
50
} ) ;
40
- expect ( mockedConsoleInfo ) . toHaveBeenCalledWith ( "smtpUrl-empty" , {
51
+ expect ( mockedLoggerModule . logger . info ) . toHaveBeenCalledWith ( "smtpUrl-empty" , {
41
52
message : "EmailUtils will log a JSON response instead of sending emails." ,
42
53
} ) ;
43
54
} ) ;
@@ -65,9 +76,9 @@ test("EmailUtils.sendEmail with recipient, subject, template, context calls gTra
65
76
const mockedNodemailer : {
66
77
createTransport : jest . MockedFunction < typeof createTransport > ;
67
78
} = jest . requireMock ( "nodemailer" ) ;
68
- const mockedConsoleInfo = jest
69
- . spyOn ( console , "info" )
70
- . mockImplementation ( ( ) => undefined ) ;
79
+ const mockedLoggerModule = jest . requireMock < typeof loggerModule > (
80
+ "../app/functions/server/logging" ,
81
+ ) ;
71
82
const { initEmail, sendEmail } = await import ( "./email" ) ;
72
83
73
84
const testSmtpUrl = "smtps://test:test@test:1" ;
@@ -86,13 +97,16 @@ test("EmailUtils.sendEmail with recipient, subject, template, context calls gTra
86
97
expect (
87
98
await sendEmail ( "[email protected] " , "subject" , "<html>test</html>" ) ,
88
99
) . toBe ( sendMailInfo ) ;
89
- expect ( mockedConsoleInfo ) . toHaveBeenCalledWith ( "sent_email" , sendMailInfo ) ;
100
+ expect ( mockedLoggerModule . logger . info ) . toHaveBeenCalledWith (
101
+ "sent_email" ,
102
+ sendMailInfo ,
103
+ ) ;
90
104
} ) ;
91
105
92
106
test ( "EmailUtils.sendEmail rejects with error" , async ( ) => {
93
- const mockedConsoleError = jest
94
- . spyOn ( console , "error" )
95
- . mockImplementation ( ( ) => undefined ) ;
107
+ const mockedLoggerModule = jest . requireMock < typeof loggerModule > (
108
+ "../app/functions/server/logging" ,
109
+ ) ;
96
110
const mockedNodemailer : {
97
111
createTransport : jest . MockedFunction < typeof createTransport > ;
98
112
} = jest . requireMock ( "nodemailer" ) ;
@@ -109,16 +123,16 @@ test("EmailUtils.sendEmail rejects with error", async () => {
109
123
await expect ( ( ) =>
110
124
sendEmail ( "[email protected] " , "subject" , "<html>test</html>" ) ,
111
125
) . rejects . toThrow ( "error" ) ;
112
- expect ( mockedConsoleError ) . toHaveBeenCalled ( ) ;
126
+ expect ( mockedLoggerModule . logger . error ) . toHaveBeenCalled ( ) ;
113
127
} ) ;
114
128
115
129
test ( "EmailUtils.init with empty host uses jsonTransport. logs messages" , async ( ) => {
116
130
const mockedNodemailer : {
117
131
createTransport : jest . MockedFunction < typeof createTransport > ;
118
132
} = jest . requireMock ( "nodemailer" ) ;
119
- const mockedConsoleInfo = jest
120
- . spyOn ( console , "info" )
121
- . mockImplementation ( ( ) => undefined ) ;
133
+ const mockedLoggerModule = jest . requireMock < typeof loggerModule > (
134
+ "../app/functions/server/logging" ,
135
+ ) ;
122
136
const { initEmail, sendEmail } = await import ( "./email" ) ;
123
137
124
138
const sendMailInfo = { messageId : "test id" , response : "test response" } ;
@@ -134,7 +148,10 @@ test("EmailUtils.init with empty host uses jsonTransport. logs messages", async
134
148
expect (
135
149
await sendEmail ( "[email protected] " , "subject" , "<html>test</html>" ) ,
136
150
) . toBe ( sendMailInfo ) ;
137
- expect ( mockedConsoleInfo ) . toHaveBeenCalledWith ( "sent_email" , sendMailInfo ) ;
151
+ expect ( mockedLoggerModule . logger . info ) . toHaveBeenCalledWith (
152
+ "sent_email" ,
153
+ sendMailInfo ,
154
+ ) ;
138
155
} ) ;
139
156
140
157
test ( "randomToken returns a random token of 2xlength (because of hex)" , ( ) => {
0 commit comments