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

Property injection with multiple tests in seperate files #266

Closed
CobeLapierre opened this issue Jun 22, 2016 · 4 comments
Closed

Property injection with multiple tests in seperate files #266

CobeLapierre opened this issue Jun 22, 2016 · 4 comments
Assignees
Labels
Milestone

Comments

@CobeLapierre
Copy link

CobeLapierre commented Jun 22, 2016

using: inversify@2.0.0-beta.10

I'm getting an error using property injection for one of the two tests.
When i remove one of these tests no errors at all.

Error: Missing required @Injectable annotation in: C.

I recreated the problem in a small project see url below.
I can't figure out what the problem might be.
My guess would be something gets executed twice.

repo:
https://github.com/carbosound1/inversify-testing-example
you can use "gulp watch" or "gulp test" to run karma

@remojansen
Copy link
Member

Hi thanks for reporting. I will investigate and try to fix this ASAP.

@remojansen remojansen added this to the 2.0.0-rc.1 milestone Jun 22, 2016
@remojansen remojansen added the bug label Jun 22, 2016
@remojansen remojansen self-assigned this Jun 23, 2016
@remojansen remojansen mentioned this issue Jun 23, 2016
18 tasks
@remojansen
Copy link
Member

Hi, I'm trying to reproduce this issue in our unit test. I'm using the following test:

    it("Trying to reproduce issue #266", () => {

        const kernel = new Kernel();
        let pInject = makePropertyInjectDecorator(kernel);

        let symbols = {
            IC: Symbol("IC")
        };

        class A {
            public b: B;

            constructor() {
                this.b = new B("test");
            }
        };

        class B {

            public name: string;

            @pInject(symbols.IC)
            public c: IC;

            constructor(name: string) {
                this.name = name;
            }

        };

        interface IC {
            test: string;
            getName(name: string): string;
        }

        @injectable()
        class C implements IC {
            public test: string;

            constructor() {
                this.test = "this is a test property";
            }

            public getName(name: string): string {
                return "this is the getName function " + name;
            }
        }

        kernel.bind<IC>(symbols.IC).to(C);
        let a = new A();
        expect(a.b).not.eql(undefined);
        expect(a.b.name).eql("test");
        expect(a.b.c.getName(a.b.name)).eql("this is the getName function " + a.b.name);

    });

The test is working so I'm almost sure that your problem is the same as #262. You are importing reflect-metadata multiple times.

As I explained in #262:

You need to remove the following from your code:

import "reflect-metadata";

This line should be be used only once by the consumers.

The consumers are the ones that need to import it. For example, InversifyJS uses Reflect but you as a consumer need to import it.

We do import reflect when we run the unit test because the unit test can be considered as a consumer.

So you need to change your code to import "reflect-metadata" only once in your whole application.

@CobeLapierre
Copy link
Author

CobeLapierre commented Jun 23, 2016

Sorry, it is indeed the problem with importing the metadata. I hadn't noticed the #262 issue.
The ticket can be closed. Thanks

@remojansen
Copy link
Member

No problem 😄 happy to help.

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

No branches or pull requests

2 participants