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

Decorators cannot be used to decorate parameters #1574

Closed
mohammadamer opened this issue Jul 2, 2024 · 2 comments
Closed

Decorators cannot be used to decorate parameters #1574

mohammadamer opened this issue Jul 2, 2024 · 2 comments

Comments

@mohammadamer
Copy link

Expected Behavior

I expect to implement unit test and unit tests to pass without errors.

Current Behavior

I got this error "Decorators cannot be used to decorate parameters" when implementing unit testing in my app.
I expect that unit tests to pass with pointing to the line where I implement the "@Inject"
All errors says "Decorators cannot be used to decorate parameters" and pointing to the "@Inject" in the constructor of the class.

Possible Solution

by removing the "@Inject" from constructor, unit tests pass.

Steps to Reproduce (for bugs)

Got the error with this syntex:

@injectable()
export default class MutationHandler implements IMutationHandler {
    private _observers = new Map<string, MutationObserver>();
    private _sharePointPageHandler: ISharePointPageHandler;
    public constructor(
        @inject(TYPES.ISharePointPageHandler) sharePointPageHandler: ISharePointPageHandler
    ) {
        this._sharePointPageHandler = sharePointPageHandler;
        this.handleActionsSubcellMutation = this.handleActionsSubcellMutation.bind(this);
    }
}

Test passes with this syntex

export default class MutationHandler implements IMutationHandler {
    private _observers = new Map<string, MutationObserver>();
    private _sharePointPageHandler: ISharePointPageHandler;
    public constructor(
        sharePointPageHandler: ISharePointPageHandler
    ) {
        this._sharePointPageHandler = sharePointPageHandler;
        this.handleActionsSubcellMutation = this.handleActionsSubcellMutation.bind(this);
    }

Context

It prevent us from building unit tests for the project.

Your Environment

  • Version used: inversify version: 6.0.2
  • Environment name and version: nodeVersion: 18.19.1
  • Operating System and version (windows desktop) 11
  • Jest version: 29.7.0

Another Environment for another project where unit test passes but with different versions:

  • Version used: inversify version: 5.1.1
  • Environment name and version: nodeVersion: 14.17.0
  • Operating System and version (windows desktop) 11
  • Jest version: 25.5.4
@egmacke
Copy link

egmacke commented Sep 23, 2024

@mohammadamer are you using the legacy experimental decorators flag? InversifyJS was written before the Typescript 5.0 implemetation/release of the finalised decorator proposal and is not currently compatible with it.

For the time being, if you want to use inversifyjs decorators then you still need to enable the legacy decorator support when compiling.

https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#differences-with-experimental-legacy-decorators

@notaphplover
Copy link
Member

This issue is due mostly to not enabling legacy decorators. Feel free to open a new one if it's not the case.

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

3 participants