Japa Test Mail using trap() function doesn't stop email nor make assertions #3512
Answered
by
thetutlage
mikesaintsg
asked this question in
Help
-
@adonisjs/core ^5.4.2 Tried to test this simple email to see if it works but no luck. I do have this hooked up to mailtrap and it is receiving the emails, so I'm not too sure why the trap isn't working: I made sure to plan for the assertions and nothing runs:
import Route from '@ioc:Adonis/Core/Route'
import Mail from '@ioc:Adonis/Addons/Mail'
Route.get('/', async ({ view }) => {
await Mail.send((message) => {
message
.from('[email protected]')
.to('[email protected]')
.subject('Welcome to my app')
.text('emails/welcome')
})
return view.render('welcome')
})
import test from 'japa'
import supertest from 'supertest'
import Mail from '@ioc:Adonis/Addons/Mail'
const BASE_URL = `http://${process.env.HOST}:${process.env.PORT}`
test.group('Welcome', async (group) => {
test('ensure home page works', async (assert) => {
assert.plan(3)
await supertest.agent(BASE_URL).get('/').expect(200)
Mail.trap((message) => {
assert.deepEqual(message.to, [
{
address: 'should fail',
},
])
assert.deepEqual(message.from, {
address: 'should fail',
})
assert.equal(message.subject, 'should fail')
})
})
group.afterEach(() => {
Mail.restore()
})
})
import 'reflect-metadata'
import { configure } from 'japa'
import sourceMapSupport from 'source-map-support'
import { Ignitor } from '@adonisjs/core/build/standalone'
process.env.NODE_ENV = 'testing'
sourceMapSupport.install({ handleUncaughtExceptions: false })
const ignitor = new Ignitor(__dirname)
export const httpServer = ignitor.httpServer()
export const application = httpServer.application
application.switchEnvironment('test')
configure({
files: ['tests/**/*.spec.ts'],
before: [
async () => {
await httpServer.start()
},
],
}) |
Beta Was this translation helpful? Give feedback.
Answered by
thetutlage
Jan 25, 2022
Replies: 1 comment 1 reply
-
It because you are trapping the email after making a request to the server. This is how you are doing it await supertest.agent(BASE_URL).get('/').expect(200)
Mail.trap((message) => {
assert.deepEqual(message.to, [
{
address: 'should fail',
},
])
assert.deepEqual(message.from, {
address: 'should fail',
})
assert.equal(message.subject, 'should fail')
})
}) This is how it should be structured // First trap the email
Mail.trap((message) => {
assert.deepEqual(message.to, [
{
address: 'should fail',
},
])
assert.deepEqual(message.from, {
address: 'should fail',
})
assert.equal(message.subject, 'should fail')
})
})
// Then make the request
await supertest.agent(BASE_URL).get('/').expect(200) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mikesaintsg
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It because you are trapping the email after making a request to the server.
This is how you are doing it
This is how it should be structured