Skip to content

Commit

Permalink
suggestion: await engine to support inline engine import in options (#…
Browse files Browse the repository at this point in the history
…422)

* await engine to support inline engine import in options

* add test for using engine imported as promise

* configure tap in .taprc only, and remove logging from the new test
  • Loading branch information
autopulated committed Apr 11, 2024
1 parent ffe8fe6 commit 8a78eb9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
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()
})
})
})

0 comments on commit 8a78eb9

Please sign in to comment.