fastify-cli icon indicating copy to clipboard operation
fastify-cli copied to clipboard

Can't access plugin inside tests

Open thales-maciel opened this issue 2 years ago • 7 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the issue has not already been raised

Issue

I've generated the project using the cli and then I modified my app.ts file so that it looks like this:

import { join } from 'path';
import AutoLoad, {AutoloadPluginOptions} from '@fastify/autoload';
import { FastifyInstance, FastifyPluginAsync } from 'fastify';
import { fastifyPostgres } from '@fastify/postgres';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUi from '@fastify/swagger-ui';
import sensible from '@fastify/sensible';
import cors from '@fastify/cors'

export type AppOptions = {} & Partial<AutoloadPluginOptions>;


// Pass --options via CLI arguments in command to enable these options.
const options: AppOptions = {}

const app: FastifyPluginAsync<AppOptions> = async (fastify: FastifyInstance, opts: any): Promise<void> => {
  fastify.register(sensible)

  fastify.register(fastifyPostgres, {
    connectionString: `postgres://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}/${process.env.DB_DATABASE}`
  })

  fastify.register(fastifySwagger, { hideUntagged: true })
  fastify.register(fastifySwaggerUi, {
    routePrefix: '/docs',
    uiConfig: { docExpansion: 'list' }
  })

  await fastify.register(cors, {})

  void fastify.register(AutoLoad, {
    dir: join(__dirname, 'routes'),
    options: opts
  })

}

export default app;
export { app, options }

The problem is that when i try to use the pg plugin inside a test I get an error Cannot read properties of undefined (reading 'query'), which makes me believe that the plugin hasn't been registered, but I don't know how to do it.

thales-maciel avatar May 08 '23 10:05 thales-maciel

I have the same problem, cannot access to any "global" plugin. I notice that the problem is only in the routes tests, while in plugins it works...

fm96-dev avatar May 17 '23 15:05 fm96-dev

I've notice that the build() function does not wrap the app plugin (exported in src/app.ts) with fp() function. I've tried to apply fp function and I can access all decorators defined from routes tests. I don't know if this is intentional or not....

fm96-dev avatar May 17 '23 16:05 fm96-dev

I noticed the same, Wrapping the app into fastify-plugin was just randomly removed in #446 with this change https://github.com/fastify/fastify-cli/commit/6dfee4e393573031343c4b932f40c54b7a0f7f65#diff-d422baf24ac606e8462ae86c1d33235f3c9299defa513b127d36661e3d766e9dL20-R24. @Eomm why was this feature removed while confusingly keeping the comment that is clearly no longer correct?

FWIW I found a viable workaround by adding the following to the end of app.js, and then setting process.env.NODE_ENV = "testing" in test/helpers.js before it calls buildApplication.

if (process.env.NODE_ENV === "testing") {
 // Ensure tests can access all decorators
 app[Symbol.for("skip-override")] = true;
}

Hornwitser avatar Aug 28 '23 14:08 Hornwitser

If @Hornwitser found the issue, would you like to send a Pull Request to address this issue? Remember to add unit tests.

Eomm avatar Aug 29 '23 07:08 Eomm

I'm not sure how to solve this issue, I'm rather unfamiliar with fastify.

Hornwitser avatar Aug 29 '23 11:08 Hornwitser

@Eomm I would like to work on this issue.

SumeetHaryani avatar Sep 20 '23 15:09 SumeetHaryani

Go for it!

Eomm avatar Sep 22 '23 17:09 Eomm

Any update on this ?

CorentinND avatar Jul 09 '24 10:07 CorentinND

Looking into it, there are perhaps several things to improve here. I think we should respect more open/close principle and allow users to decorate the app before ready is called.

jean-michelet avatar Jul 10 '24 07:07 jean-michelet

Maybe I should create a more general issue about refactoring, to make sure I understand how it (should) works, it could also be a lack of documentation.

jean-michelet avatar Jul 10 '24 07:07 jean-michelet