Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggestion: await engine to support inline engine import in options #422

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .taprc
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ coverage: true
nyc-arg: [--exclude=out]

files:
- test/**/*.js
- test/**/*.{js,mjs}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function fastifyView (fastify, opts) {
}
const charset = opts.charset || 'utf-8'
const propertyName = opts.propertyName || 'view'
const engine = opts.engine[type]
const engine = await opts.engine[type]
const globalOptions = opts.options || {}
const templatesDir = resolveTemplateDir(opts)
const includeViewExtension = opts.includeViewExtension || false
Expand Down
32 changes: 32 additions & 0 deletions test/test-import-engine.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import t from 'tap'
import get from 'simple-get'
import Fastify from 'fastify'
import fs from 'node:fs'
const test = t.test
const sget = get.concat

test('using an imported engine as a promise', t => {
t.plan(3)
const fastify = Fastify()
const data = { text: 'text' }
const ejs = import('ejs')

fastify.register(import('../index.js'), { engine: { ejs }, templates: 'templates' })

fastify.get('/', (req, reply) => {
reply.view('index.ejs', data)
})

fastify.listen({ port: 0 }, err => {
t.error(err)

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, async (err, response, body) => {
t.error(err)
t.equal((await ejs).render(fs.readFileSync('./templates/index.ejs', 'utf8'), data), body.toString())
fastify.close()
})
})
})