From d6b0962f44340e903f8f73e5529d066347a6baa4 Mon Sep 17 00:00:00 2001 From: James Crosby Date: Wed, 10 Apr 2024 16:29:06 +0100 Subject: [PATCH 1/3] await engine to support inline engine import in options --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 368ea21..c2cad44 100644 --- a/index.js +++ b/index.js @@ -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 From b2540b53eef3bf00ba11c305e55abd234d04514e Mon Sep 17 00:00:00 2001 From: James Crosby Date: Wed, 10 Apr 2024 18:07:57 +0100 Subject: [PATCH 2/3] add test for using engine imported as promise --- package.json | 2 +- test/test-import-engine.mjs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/test-import-engine.mjs diff --git a/package.json b/package.json index 50d5339..cddffd5 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "lint": "standard", "test-with-snapshot": "cross-env TAP_SNAPSHOT=1 tap test/test-ejs-with-snapshot.js", "test": "npm run test:unit && npm run test:typescript", - "test:unit": "tap", + "test:unit": "tap test/*.js test/*.mjs", "test:typescript": "tsd" }, "repository": { diff --git a/test/test-import-engine.mjs b/test/test-import-engine.mjs new file mode 100644 index 0000000..c56b375 --- /dev/null +++ b/test/test-import-engine.mjs @@ -0,0 +1,34 @@ +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 + +console.log('running import test') + +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() + }) + }) +}) From a26b2b3033233dff7c49b15848b0e2cd80e0f1e8 Mon Sep 17 00:00:00 2001 From: James Crosby Date: Wed, 10 Apr 2024 18:16:41 +0100 Subject: [PATCH 3/3] configure tap in .taprc only, and remove logging from the new test --- .taprc | 2 +- package.json | 2 +- test/test-import-engine.mjs | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.taprc b/.taprc index 27ed255..0110bb6 100644 --- a/.taprc +++ b/.taprc @@ -5,4 +5,4 @@ coverage: true nyc-arg: [--exclude=out] files: - - test/**/*.js + - test/**/*.{js,mjs} diff --git a/package.json b/package.json index cddffd5..50d5339 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "lint": "standard", "test-with-snapshot": "cross-env TAP_SNAPSHOT=1 tap test/test-ejs-with-snapshot.js", "test": "npm run test:unit && npm run test:typescript", - "test:unit": "tap test/*.js test/*.mjs", + "test:unit": "tap", "test:typescript": "tsd" }, "repository": { diff --git a/test/test-import-engine.mjs b/test/test-import-engine.mjs index c56b375..b62b606 100644 --- a/test/test-import-engine.mjs +++ b/test/test-import-engine.mjs @@ -5,8 +5,6 @@ import fs from 'node:fs' const test = t.test const sget = get.concat -console.log('running import test') - test('using an imported engine as a promise', t => { t.plan(3) const fastify = Fastify()