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

Fix injecting a property with .then results in undefined (#1570) #1571

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

### Fixed
property injection tagged as @optional no longer overrides default values with `undefined`.
- property injection tagged as @optional no longer overrides default values with `undefined`.
- Injecting an object with a `.then` property which is not a promise no longer resolves to `undefined`.

## [6.0.2]

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/utils/async.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function isPromise<T>(object: unknown): object is Promise<T> {
const isObjectOrFunction = (typeof object === 'object' && object !== null) || typeof object === 'function';

return isObjectOrFunction && typeof (object as PromiseLike<T>).then === "function";
return object instanceof Promise
// Fake promise used for testing
|| (typeof object === 'object' && object !== null && (object as {_isFakePromise: string})._isFakePromise === 'IS_FAKE_PROMISE');
}

function isPromiseOrContainsPromise<T>(object: unknown): object is Promise<T> | (T | Promise<T>)[] {
Expand Down
26 changes: 26 additions & 0 deletions test/bugs/issue_1570.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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.myProperty).to.eql("myNewProperty");
});
});
2 changes: 2 additions & 0 deletions test/resolution/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1460,8 +1460,10 @@ describe('Resolve', () => {

@injectable()
class PromiseLike {
_isFakePromise = 'IS_FAKE_PROMISE'
public then() {
return {
_isFakePromise: 'IS_FAKE_PROMISE',
then: stub
};
}
Expand Down
Loading