Skip to content

Commit

Permalink
test: fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 25, 2023
1 parent 8c07f1b commit e0da873
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 4 deletions.
6 changes: 4 additions & 2 deletions configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
* file that was distributed with this source code.
*/

import string from '@poppinss/utils/string'
import type Configure from '@adonisjs/core/commands/configure'

import { stubsRoot } from './stubs/main.js'

/**
Expand Down Expand Up @@ -62,8 +64,8 @@ export async function configure(command: Configure) {
if (unknownTransport) {
command.exitCode = 1
command.logger.logError(
`Invalid transport "${unknownTransport}". Supported transports are: ${KNOWN_TRANSPORTS.join(
','
`Invalid transport "${unknownTransport}". Supported transports are: ${string.sentence(
KNOWN_TRANSPORTS
)}`
)
return
Expand Down
108 changes: 106 additions & 2 deletions tests/integration/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,70 @@ test.group('Configure', (group) => {
context.fs.basePath = fileURLToPath(BASE_URL)
})

test('configure package', async ({ fs, assert }) => {
group.each.disableTimeout()

test('configure package with pre-defined transports', async ({ fs, assert }) => {
const ignitor = new IgnitorFactory()
.withCoreProviders()
.withCoreConfig()
.create(fs.baseUrl, {
importer: (filePath) => {
if (filePath.startsWith('./') || filePath.startsWith('../')) {
return import(new URL(filePath, fs.baseUrl).href)
}

return import(filePath)
},
})

const app = ignitor.createApp('web')
await app.init()
await app.boot()

await fs.create('.env', '')
await fs.createJson('tsconfig.json', {})
await fs.create('start/env.ts', `export default Env.create(new URL('./'), {})`)
await fs.create('adonisrc.ts', `export default defineConfig({})`)

const ace = await app.container.make('ace')

const command = await ace.create(Configure, [
'../../index.js',
'--transports=sparkpost',
'--transports=resend',
])
await command.exec()

await assert.fileExists('config/mail.ts')
await assert.fileExists('adonisrc.ts')
await assert.fileContains('adonisrc.ts', '@adonisjs/mail/mail_provider')
await assert.fileContains('config/mail.ts', 'defineConfig')
await assert.fileContains('config/mail.ts', `declare module '@adonisjs/mail/types' {`)

await assert.fileContains(
'config/mail.ts',
`
sparkpost: transports.sparkpost({
key: env.get('SPARKPOST_API_KEY'),
baseUrl: 'https://api.sparkpost.com/api/v1',
}),`
)
await assert.fileContains(
'config/mail.ts',
`
resend: transports.resend({
key: env.get('RESEND_API_KEY'),
baseUrl: 'https://api.resend.com',
}),`
)
await assert.fileContains('.env', 'SPARKPOST_API_KEY')
await assert.fileContains('.env', 'RESEND_API_KEY')

await assert.fileContains('start/env.ts', 'SPARKPOST_API_KEY: Env.schema.string()')
await assert.fileContains('start/env.ts', 'RESEND_API_KEY: Env.schema.string()')
})

test('report error when unknown transports are mentioned', async ({ fs, assert }) => {
const ignitor = new IgnitorFactory()
.withCoreProviders()
.withCoreConfig()
Expand All @@ -46,6 +109,47 @@ test.group('Configure', (group) => {
const ace = await app.container.make('ace')
ace.prompt.trap('Select the mail services you want to use').chooseOptions([2, 4])

const command = await ace.create(Configure, [
'../../index.js',
'--transports=sparkpost',
'--transports=foo',
])
await command.exec()

command.assertFailed()
await assert.fileNotExists('config/mail.ts')
await assert.fileExists('adonisrc.ts')
await assert.fileEquals('adonisrc.ts', `export default defineConfig({})`)
await assert.fileEquals('.env', '')
await assert.fileEquals('start/env.ts', `export default Env.create(new URL('./'), {})`)
})

test('prompt when no transports are mentioned', async ({ fs, assert }) => {
const ignitor = new IgnitorFactory()
.withCoreProviders()
.withCoreConfig()
.create(fs.baseUrl, {
importer: (filePath) => {
if (filePath.startsWith('./') || filePath.startsWith('../')) {
return import(new URL(filePath, fs.baseUrl).href)
}

return import(filePath)
},
})

const app = ignitor.createApp('web')
await app.init()
await app.boot()

await fs.create('.env', '')
await fs.createJson('tsconfig.json', {})
await fs.create('start/env.ts', `export default Env.create(new URL('./'), {})`)
await fs.create('adonisrc.ts', `export default defineConfig({})`)

const ace = await app.container.make('ace')
ace.prompt.trap('Select the mail services you want to use').chooseOptions([3, 4])

const command = await ace.create(Configure, ['../../index.js'])
await command.exec()

Expand Down Expand Up @@ -76,5 +180,5 @@ test.group('Configure', (group) => {

await assert.fileContains('start/env.ts', 'SPARKPOST_API_KEY: Env.schema.string()')
await assert.fileContains('start/env.ts', 'RESEND_API_KEY: Env.schema.string()')
}).timeout(1000 * 60)
})
})

0 comments on commit e0da873

Please sign in to comment.