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

Implementing ts-mocha is throwing type errors in Inject #2764

Closed
Navos opened this issue Apr 17, 2019 · 14 comments
Closed

Implementing ts-mocha is throwing type errors in Inject #2764

Navos opened this issue Apr 17, 2019 · 14 comments
Labels
developer-experience Issues affecting ease of use and overall experience of LB users help wanted IoC/Context @loopback/context: Dependency Injection, Inversion of Control stale

Comments

@Navos
Copy link

Navos commented Apr 17, 2019

Description / Steps to reproduce / Feature proposal

I've been trying to implement ts-mocha instead of the built in lb-mocha for faster test running and while I have taken care of the main issues within my code, one problem remains related to when inject checks if a getter or setter is a function.

The issue stems from the following lines: https://github.com/strongloop/loopback-next/blob/master/packages/context/src/inject.ts#L551 and https://github.com/strongloop/loopback-next/blob/master/packages/context/src/inject.ts#L426

When modifying the code and trying to make working adjustments, typeof targetType === 'function' returns true, but the check against the interface Function does not.

Current Behavior

The type of AuthenticateActionProvider.constructor[1] (Object) is not Setter function

Expected Behavior

It understand the given object is a function.

@raymondfeng
Copy link
Contributor

It seems that ts-node does not emit Function for the design type of Setter. ts-mocha probably sees Object instead. Please note typeof Object === 'function' returns true as Object itself is a class/constructor.

@raymondfeng
Copy link
Contributor

Maybe there is a possibility to relax the check if the design type is Object, which pretty much hints that no design type is inferred.

@raymondfeng
Copy link
Contributor

@Navos Please try #2765

@raymondfeng
Copy link
Contributor

raymondfeng commented Apr 17, 2019

@Navos Can you make sure emitDecoratorMetadata is enabled for your ts-mocha setup? See https://www.typescriptlang.org/docs/handbook/compiler-options.html.

#2765 skips the assertions but @loopback/rest also relies on the design time metadata for type introspection.

@Navos
Copy link
Author

Navos commented Apr 17, 2019

@raymondfeng I tried the separate branch and the added the emitDecoratorMetadata and still having the same issue. Debugging through the new code, isDesignTypeSupported is returning true.

@raymondfeng
Copy link
Contributor

If you have a github repo to reproduce the issue, I can take a look.

@Navos
Copy link
Author

Navos commented Apr 17, 2019

Hey Raymond, I cant show you the code I am currently working on due to it being work related, but I reproduced the error with the log-extension example here https://github.com/Navos/ts-mocha-implementation/blob/master/package.json

@raymondfeng
Copy link
Contributor

Here is the transpiled result from ts-node:

__decorate([
    context_1.inject(keys_1.EXAMPLE_LOG_BINDINGS.LOGGER, { optional: true }),
    __metadata("design:type", typeof (_a = typeof types_1.LogWriterFn !== "undefined" && types_1.LogWriterFn) === "function" ? _a : Object)
], LogActionProvider.prototype, "writeLog", void 0);
__decorate([
    context_1.inject(keys_1.EXAMPLE_LOG_BINDINGS.APP_LOG_LEVEL, { optional: true }),
    __metadata("design:type", typeof (_b = typeof keys_1.LOG_LEVEL !== "undefined" && keys_1.LOG_LEVEL) === "function" ? _b : Object)
], LogActionProvider.prototype, "logLevel", void 0);
LogActionProvider = __decorate([
    __param(0, context_1.inject.getter(core_1.CoreBindings.CONTROLLER_CLASS)),
    __param(1, context_1.inject.getter(core_1.CoreBindings.CONTROLLER_METHOD_NAME)),
    __param(2, context_1.inject(keys_1.EXAMPLE_LOG_BINDINGS.TIMER)),
    __metadata("design:paramtypes", [typeof (_c = typeof context_1.Getter !== "undefined" && context_1.Getter) === "function" ? _c : Object, typeof (_d = typeof context_1.Getter !== "undefined" && context_1.Getter) === "function" ? _d : Object, typeof (_e = typeof types_1.TimerFn !== "undefined" && types_1.TimerFn) === "function" ? _e : Object])
], LogActionProvider);

@raymondfeng
Copy link
Contributor

And here is what tsc produces:

__decorate([
    context_1.inject(keys_1.EXAMPLE_LOG_BINDINGS.LOGGER, { optional: true }),
    __metadata("design:type", Function)
], LogActionProvider.prototype, "writeLog", void 0);
__decorate([
    context_1.inject(keys_1.EXAMPLE_LOG_BINDINGS.APP_LOG_LEVEL, { optional: true }),
    __metadata("design:type", Number)
], LogActionProvider.prototype, "logLevel", void 0);
LogActionProvider = __decorate([
    __param(0, context_1.inject.getter(core_1.CoreBindings.CONTROLLER_CLASS)),
    __param(1, context_1.inject.getter(core_1.CoreBindings.CONTROLLER_METHOD_NAME)),
    __param(2, context_1.inject(keys_1.EXAMPLE_LOG_BINDINGS.TIMER)),
    __metadata("design:paramtypes", [Function, Function, Function])
], LogActionProvider);

@raymondfeng
Copy link
Contributor

typeof context_1.Getter is undefined as Getter is just a TypeScript type definition and it's erased at runtime. As a result, ts-node only emits Object instead of Function.

@bajtos bajtos added the IoC/Context @loopback/context: Dependency Injection, Inversion of Control label Apr 18, 2019
@Navos
Copy link
Author

Navos commented Apr 22, 2019

@raymondfeng Hey Raymond, what will happen with this issue?

@bajtos
Copy link
Member

bajtos commented Jun 14, 2019

Possibly related: #3160

@Navos is this issue still relevant? Have you managed to find any solution or any workaround?

I am looking into ways how to switch loopback-next from Mocha to Jest (see #3159). It seems to me that the solution for on-the-fly-compilation used by Jest is different from the solution used by ts-mocha (and ts-node) and the problem described in this issue is not triggered by Jest.

Would you like to try Jest instead of ts-mocha and let us know if it works any better?

@bajtos bajtos added the developer-experience Issues affecting ease of use and overall experience of LB users label Jun 14, 2019
@stale
Copy link

stale bot commented Aug 31, 2020

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

@stale
Copy link

stale bot commented Oct 4, 2020

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

@stale stale bot closed this as completed Oct 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience Issues affecting ease of use and overall experience of LB users help wanted IoC/Context @loopback/context: Dependency Injection, Inversion of Control stale
Projects
None yet
Development

No branches or pull requests

3 participants