|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { after, before, describe, it, mock } = require('node:test') |
| 4 | +const path = require('node:path') |
| 5 | +const Fastify = require('fastify') |
| 6 | +const autoLoad = require('../../../') |
| 7 | +const assert = require('node:assert') |
| 8 | + |
| 9 | +describe('Issue 462: ignore invalid and/or empty .js and .mjs files when using esm', function () { |
| 10 | + const app = Fastify() |
| 11 | + |
| 12 | + before(async function () { |
| 13 | + app.register(autoLoad, { |
| 14 | + dir: path.join(__dirname, 'routes'), |
| 15 | + forceESM: true, |
| 16 | + }) |
| 17 | + mock.method(app.log, 'debug') |
| 18 | + await app.ready() |
| 19 | + }) |
| 20 | + |
| 21 | + after(async function () { |
| 22 | + await app.close() |
| 23 | + }) |
| 24 | + |
| 25 | + it('should respond correctly to /api', async function () { |
| 26 | + const res = await app.inject({ url: '/api' }) |
| 27 | + |
| 28 | + assert.strictEqual(res.statusCode, 200) |
| 29 | + assert.deepStrictEqual(res.json(), { path: '/api' }) |
| 30 | + |
| 31 | + // check debug call when importing empty/invalid esm files |
| 32 | + // 5 debug log including: empty.cjs, empty.js, empty.mjs, hello.mjs, object.mjs |
| 33 | + // api.mjs pass the check so it will not trigger the debug log |
| 34 | + // increasing this number if there are more empty/invalid esm files you want to test |
| 35 | + assert.strictEqual(app.log.debug.mock.callCount(), 5) |
| 36 | + }) |
| 37 | +}) |
0 commit comments