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: inject property should be configurable #85

Merged
merged 1 commit into from
Jan 28, 2023
Merged
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
4 changes: 2 additions & 2 deletions core/runtime/src/impl/EggObjectUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class EggObjectUtil {
get(): any {
return eggObject.obj;
},
configurable: false,
configurable: true,
enumerable: true,
};
}
Expand All @@ -22,7 +22,7 @@ export class EggObjectUtil {
const eggObject = EggContainerFactory.getEggObject(proto, objName);
return eggObject.obj;
},
configurable: false,
configurable: true,
enumerable: true,
};
}
Expand Down
22 changes: 22 additions & 0 deletions core/runtime/test/EggObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EggTestContext } from './fixtures/EggTestContext';
import TestUtil from './util';
import { EggContainerFactory } from '..';
import { Foo, Bar } from './fixtures/modules/lifecycle-hook/object';
import { Bar as ExtendsBar } from './fixtures/modules/extends-module/Base';
import mm from 'mm';
import { ContextHandler } from '../src/model/ContextHandler';
import { SingletonBar } from './fixtures/modules/inject-context-to-singleton/object';
Expand Down Expand Up @@ -101,4 +102,25 @@ describe('test/EggObject.test.ts', () => {
await ctx.destroy({});
});
});

describe('property mock', () => {
beforeEach(() => {
mm(ContextHandler, 'getContext', () => {
return ctx;
});
});

it('should work', async () => {
const instance = await TestUtil.createLoadUnitInstance('extends-module');
const barProto = EggPrototypeFactory.instance.getPrototype('bar', instance.loadUnit);
const barObj = await EggContainerFactory.getOrCreateEggObject(barProto, barProto.name);
const bar = barObj.obj as ExtendsBar;
const foo = {};
mm(bar, 'foo', foo);
assert(bar.foo === foo);

await TestUtil.destroyLoadUnitInstance(instance);
await ctx.destroy({});
});
});
});