Skip to content

Commit c3b4be0

Browse files
committed
tweak constructor check to return any existing value if available first
1 parent 22a5d03 commit c3b4be0

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

src/ObservableObject.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ const proxyHandler: ProxyHandler<any> = {
412412
}
413413

414414
if (p === 'constructor') {
415-
return function observable() {};
415+
const ctor = peekInternal(node)?.constructor;
416+
return typeof ctor === 'function' ? ctor : Object;
416417
}
417418

418419
let value = peekInternal(node, /*activateRecursive*/ p === 'get' || p === 'peek');

tests/tests.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3863,12 +3863,14 @@ describe('Misc', () => {
38633863
});
38643864
test('Observable is compatible with vitest equality checks and error printing', () => {
38653865
const obs = observable({ foo: 'bar' });
3866-
const obj = obs.foo
3866+
const obj = obs.foo;
3867+
const ctor = obj.constructor;
38673868

38683869
// https://github.com/vitest-dev/vitest/blob/v3.2.4/packages/expect/src/jest-utils.ts#L42-L49
3869-
expect(!!obj && typeof obj === 'object' && 'asymmetricMatch' in obj).toBe(false)
3870+
expect(!!obj && typeof obj === 'object' && 'asymmetricMatch' in obj).toBe(false);
38703871

38713872
// https://github.com/vitest-dev/vitest/blob/v3.2.4/packages/pretty-format/src/index.ts#L288
3872-
expect(`${obj.constructor.name}`).toBe('observable')
3873+
expect(typeof ctor).toBe('function');
3874+
expect(obj.constructor).toBe(ctor);
38733875
});
38743876
});

0 commit comments

Comments
 (0)