Skip to content

Commit

Permalink
test: mock nodemailer network calls (#9957)
Browse files Browse the repository at this point in the history
In tests, nodemailer was making network calls to create test accounts
using ethereal.email. This mocks them appropriately.
  • Loading branch information
denolfe authored Dec 13, 2024
1 parent 4334940 commit 9c8cdea
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 42 deletions.
15 changes: 15 additions & 0 deletions packages/payload-cloud/src/email.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Config, Payload } from 'payload'

import { jest } from '@jest/globals'
import nodemailer from 'nodemailer'
import { defaults } from 'payload'

import { payloadCloudEmail } from './email.js'
Expand All @@ -13,6 +14,20 @@ describe('email', () => {

const mockedPayload: Payload = jest.fn() as unknown as Payload

beforeAll(() => {
// Mock createTestAccount to prevent calling external services
jest.spyOn(nodemailer, 'createTestAccount').mockImplementation(() => {
return Promise.resolve({
imap: { host: 'imap.test.com', port: 993, secure: true },
pass: 'testpass',
pop3: { host: 'pop3.test.com', port: 995, secure: true },
smtp: { host: 'smtp.test.com', port: 587, secure: false },
user: 'testuser',
web: 'https://webmail.test.com',
})
})
})

beforeEach(() => {
defaultConfig = defaults as Config
})
Expand Down
14 changes: 14 additions & 0 deletions packages/payload-cloud/src/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ describe('plugin', () => {

const skipVerify = true

beforeAll(() => {
// Mock createTestAccount to prevent calling external services
jest.spyOn(nodemailer, 'createTestAccount').mockImplementation(() => {
return Promise.resolve({
imap: { host: 'imap.test.com', port: 993, secure: true },
pass: 'testpass',
pop3: { host: 'pop3.test.com', port: 995, secure: true },
smtp: { host: 'smtp.test.com', port: 587, secure: false },
user: 'testuser',
web: 'https://webmail.test.com',
})
})
})

beforeEach(() => {
createTransportSpy = jest.spyOn(nodemailer, 'createTransport').mockImplementationOnce(() => {
return {
Expand Down
58 changes: 16 additions & 42 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions test/jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import console from 'console'
global.console = console

import nodemailer from 'nodemailer'

import { generateDatabaseAdapter } from './generateDatabaseAdapter.js'

process.env.PAYLOAD_DISABLE_ADMIN = 'true'
Expand All @@ -11,6 +13,18 @@ process.env.PAYLOAD_PUBLIC_CLOUD_STORAGE_ADAPTER = 's3'
process.env.NODE_OPTIONS = '--no-deprecation'
process.env.PAYLOAD_CI_DEPENDENCY_CHECKER = 'true'

// Mock createTestAccount to prevent calling external services
jest.spyOn(nodemailer, 'createTestAccount').mockImplementation(() => {
return Promise.resolve({
imap: { host: 'imap.test.com', port: 993, secure: true },
pass: 'testpass',
pop3: { host: 'pop3.test.com', port: 995, secure: true },
smtp: { host: 'smtp.test.com', port: 587, secure: false },
user: 'testuser',
web: 'https://webmail.test.com',
})
})

const dbAdapter = process.env.PAYLOAD_DATABASE || 'mongodb'

generateDatabaseAdapter(dbAdapter)
1 change: 1 addition & 0 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"jwt-decode": "4.0.0",
"mongoose": "8.8.3",
"next": "15.0.3",
"nodemailer": "6.9.10",
"payload": "workspace:*",
"qs-esm": "7.0.2",
"react": "19.0.0",
Expand Down

0 comments on commit 9c8cdea

Please sign in to comment.