Skip to content

Commit

Permalink
fix: Injecting a property with .then results in undefined (#1570)
Browse files Browse the repository at this point in the history
  • Loading branch information
brahms116 committed May 30, 2024
1 parent 7619dd4 commit 8f76ca6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
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

0 comments on commit 8f76ca6

Please sign in to comment.