Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

test db prepare #874

Open
rommelandrea opened this issue May 3, 2023 · 2 comments
Open

test db prepare #874

rommelandrea opened this issue May 3, 2023 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@rommelandrea
Copy link

馃挰 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

  • node version: 18
  • fastify version: >=4.15.0
  • os: Linux
  • any other relevant information: scaffolding with fastify-cli, tap for test
@rommelandrea rommelandrea added the help wanted Extra attention is needed label May 3, 2023
@Eomm
Copy link
Member

Eomm commented May 4, 2023

I need to create tests that need a preparation phase

Isn't this phase executed before creating the application itself?

These inside helpers and inside test files that are unreachable due to plugin encapsulation.

Is your plugin responsible for the db initialization? If it is needed for test only, it seems dangerous

@rommelandrea
Copy link
Author

Hi @Eomm, thank you for you reply.

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', async t => {
  const app = await build(t)
  let id

  t.before(async t => {
    const values = [
      'myid',
      'rolename'
    ]

    const { row } = await app.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', async t => {
    const res = await app.inject({
      url: `/api/role/${id}`
    })
    t.has(JSON.parse(res.payload), { id: id })
  })
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants