My application built on Fastify (
"fastify": "^4.26.0"
) operates smoothly under normal conditions with no issues. However, when trying to incorporate unit testing using Vitest, every test fails despite their simplicity. Upon troubleshooting, I found that by commenting out certain lines in my app.ts
file, the tests run successfully:
// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
void app.register(AutoLoad, {
dir: join(__dirname, 'plugins'),
options: Object.assign({}, opts),
})
Seeking a solution, I stumbled upon this GitHub issue. Though not identical to my problem, it pointed me towards a temporary workaround. I configured my vitest.config.mts
file as follows:
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
include: ["test/**/*.test.ts"],
silent: false,
server: {
deps: {
inline: ["@fastify/autoload"],
},
},
},
});
This setup allowed me to run tests without any trouble for several months. However, I am now stuck as I am consistently facing this error:
FAIL test/main.test.ts > App > should start the app
FastifyError: Plugin must be a function or a promise. Received: 'object'
❯ validatePlugin node_modules/avvio/lib/validate-plugin.js:19:13
❯ Boot._addPlugin node_modules/avvio/boot.js:166:3
❯ Boot.use node_modules/avvio/boot.js:139:25
❯ Object.server.<computed> [as register] node_modules/avvio/boot.js:204:14
❯ registerPlugin node_modules/@fastify/autoload/index.js:342:11
340| }
341|
342| fastify.register(plugin, options)
| ^
343|
344| meta.registered = true
❯ registerAllPlugins node_modules/@fastify/autoload/index.js:112:19
❯ autoload node_modules/@fastify/autoload/index.js:92:7