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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing compatibility with NestJS #58

Open
alexandercerutti opened this issue Jan 6, 2025 · 5 comments
Open

Testing compatibility with NestJS #58

alexandercerutti opened this issue Jan 6, 2025 · 5 comments

Comments

@alexandercerutti
Copy link
Contributor

Hi @vinayak25, I was playing around with testing on the plugin I created.
I started by looking at the example code inside the sample app automatically cloned.

As there is no documentation on Intent website on how to build tests, I tried to give a look to NestJS documentation.

As the example code is pretty similar, I tried to follow the example it provided and adding both my Controller and my Service.

/**
 * @type {import("@nestjs/common").INestApplication}
 */
let app;

/**
 * @type {import("intent-passkit-webservice").v1.Registration.Controller}
 */
let registrationController;

/**
 * @type {import("intent-passkit-webservice").v1.Registration.Service}
 */
let registrationService;

beforeEach(async () => {
	const moduleRef = await Test.createTestingModule({
		controllers: [RegistrationController],
		providers: [RegistrationService],
	}).compile();

	app = moduleRef.createNestApplication();
	await app.init();

	registrationService = await moduleRef.get(RegistrationService);
	registrationController = await moduleRef.get(RegistrationController);

});

However, when app.init() happens, NestJS spits out the following error:

Caution

An invalid controller has been detected. "RegistrationController" does not have the @controller() decorator but it is being listed in the "controllers" array of some module.

I started debugging both NestJS code and IntentJS code.

What I found is kind of weird to me: apparently the two libs @Controller() decorators seem to be incompatible each-other:

  • @intentjs/core sets the property @intentjs/controller_path
  • nest looks for the property path

export function ControllerMetadata(path?: string, options?: ControllerOptions) {
return function (target: Function) {
Reflect.defineMetadata(CONTROLLER_KEY, path || '', target);
Reflect.defineMetadata(CONTROLLER_OPTIONS, options, target);
};
}

So, fixing it would be as easy as doing this (I already tried and it worked).

function ControllerMetadata(path, options) {
    return function (target) {
        Reflect.defineMetadata(constants_1.CONTROLLER_KEY, path || '', target);
+       Reflect.defineMetadata("path", path || '', target);
        Reflect.defineMetadata(constants_1.CONTROLLER_OPTIONS, options, target);
    };
}

But before opening a PR or whatever, I'd like to understand if this is intentional and why, or if you are expecting a different way to test controllers defined with @Controller() from @intentjs/core that I don't know.

In my opinion, making it compatible with Nest would be the best thing.

Let me know!

Copy link

linear bot commented Jan 6, 2025

@alexandercerutti
Copy link
Contributor Author

Hi @vinayak25, just a quick ping. This is quite blocking me as I'd like to publish my library for Intent with the right tests. If this is a no-go, I'll proceed without them. I just need to understand what can be done and what can I do to help.

Thank you!

@vinayak25
Copy link
Member

Hey @alexandercerutti I am working on a testing framework for IntentJS. It's in the work right now, so for now you can ship your package without it. Let me know if you would need any help from my side

@alexandercerutti
Copy link
Contributor Author

I see. Thank you for the answer! I'll ship without tests and add them in the future.

@alexandercerutti
Copy link
Contributor Author

@vinayak25 if you are interested, here is the plugin I published for IntentJS, with a documentation in wikis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants