You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been scaffolding an app with fastify-cli and I need to create tests that need a preparation phase (before and beforeEach) that uses plugins that are normally automatically loaded with fastify-autoload. These inside helpers and inside test files that are unreachable due to plugin encapsulation.
One of the plugin is the connection to DB, but is not the only one.
One of option that I read is about breaking plugin encapsulation with enclosing the app.js content in to a fastify-plugin. An other is load manually the plugin on before the test.
What is the best practice on this use case?
Your Environment
node version: 18
fastify version: >=4.15.0
os: Linux
any other relevant information: scaffolding with fastify-cli, tap for test
The text was updated successfully, but these errors were encountered:
Isn't this phase executed before creating the application itself?
yes, this phase is executed on test startup before all test in the file.
Is your plugin responsible for the db initialization? If it is needed for test only, it seems dangerous
One of them it is db initialization.
I'm not sure if I explained myself correctly. The plugin wasn't made specifically for testing, but it's the same used by my application during its execution. My goal is seed all the data on DB, auth, ... and to do it without create something that was made specifically for testing, but to use the same things that the app normally uses.
Even if it doesn't work, I pasted an example with my idea but i don't know if it's the correct way. My request for help is to set up the tests in the most fitting way with respect to the Fastify ecosystem.
import{test}from'tap'import{build}from'../helper.js'test('test roles',asynct=>{constapp=awaitbuild(t)letidt.before(asynct=>{constvalues=['myid','rolename']const{ row }=awaitapp.db.query('INSERT INTO roles(id, name, created_at, updated_at) VALUES($1, $2, NOW(), NOW()) RETURNING id ',values)id=row.id})t.test('ok test',asynct=>{constres=awaitapp.inject({url: `/api/role/${id}`})t.has(JSON.parse(res.payload),{id: id})})})
馃挰 How to prepare correctly db for test
I have been scaffolding an app with fastify-cli and I need to create tests that need a preparation phase (before and beforeEach) that uses plugins that are normally automatically loaded with fastify-autoload. These inside helpers and inside test files that are unreachable due to plugin encapsulation.
One of the plugin is the connection to DB, but is not the only one.
One of option that I read is about breaking plugin encapsulation with enclosing the app.js content in to a fastify-plugin. An other is load manually the plugin on before the test.
What is the best practice on this use case?
Your Environment
The text was updated successfully, but these errors were encountered: