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

Injecting an object with a .then property which is not a promise resolves to undefined #1570

Closed
brahms116 opened this issue May 30, 2024 · 1 comment
Assignees

Comments

@brahms116
Copy link

brahms116 commented May 30, 2024

When you inject an object with a property of .then but its not a promise, it resolves to undefined. This is because when resolving it believes that its a promise when its not

Expected Behavior

This should pass...

import { expect } from "chai";
import { Container, inject, injectable } from "../../src/inversify";

describe("Issue", () => {
  it("It should not return injected value as undefined if the value contains a .then property but it is not a promise", () => {
    const container = new Container();

    interface Injected {
      myProperty: string;
      then: () => number;
    }

    @injectable()
    class ResolveMe {
      constructor(@inject("Injected") public injected: Injected) {}
    }

    container.bind("Injected").toConstantValue({
      myProperty: "myNewProperty",
      then: () => 1
    });

    const me = container.resolve(ResolveMe);
    expect(me.injected.myProperty).to.eql("myNewProperty");
  });
});

Current Behavior

injected resolves to undefined

Possible Solution

I have linked a PR for a possible approach #1571

@notaphplover
Copy link
Member

notaphplover commented Oct 24, 2024

Hey @brahms116, I had a look at your issue.

Inversify relies on the Promises/A+ standard and tries to apply their resolution algorithm when it detects a thenable.

According to the standard resolution procedure, "Injected" must be resolved to the value 1.

Consider this passing test:

import { expect } from "chai";
import { Container, inject, injectable } from "../../src/inversify";

describe("Issue 1570", () => {
  it("It should not return injected value as undefined if the value contains a .then property but it is not a promise", () => {
    const container = new Container();

    interface Injected {
      myProperty: string;
      then: () => number;
    }

    @injectable()
    class ResolveMe {
      constructor(@inject("Injected") public injected: Injected) {}
    }

    container.bind("Injected").toConstantValue({
      myProperty: "myNewProperty",
      then: () => 1,
    });

    const me = container.resolve(ResolveMe);
    expect(me.injected).to.eql(1);
  });
});

This is the expected behavior when working with thenables, we cannot rely on the Promise class because it's not the only possible expected Promise. Any class implementing the Promises/A+ spec is a valid promise as well, and I would suggest you not to rely on instance of Promise to detect a Promise for such a reason.

I'll close this issue now. If you wish to continue the discussion, feel free to reply me in this issue and I'll reopen it asap.

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

No branches or pull requests

2 participants