-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathpatch-object-define-property.ts
36 lines (34 loc) · 1.23 KB
/
patch-object-define-property.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* It's a hack to allow for spying on non-configurable properties of objects.
* In particular, it allows to spy on simple functions imported from 3rd party libraries.
*
* This file should NOT be imported in the production code, but only in unit tests!
*
* It monkey-patches globally `Object.defineProperty` to avoid setting any object property as non-configurable.
* Thanks to that, `spyOnProperty` can overwrite a getter of any object's property.
*
* Without this hack, an error would be thrown '<spyOnProperty> : isDevMode is not declared configurable' for
* such a test code:
* ```
* import * as AngularCore from '@angular/core';
* spyOnProperty(AngularCore, 'isDevMode').and.returnValue(() => false);
* ```
*
* It's needed since `[email protected]`.
* See https://github.com/angular/angular/commit/45a73dddfdf3f32ad4203c71c06b6a4be50f4a31
*/
const { defineProperty } = Object;
Object.defineProperty = (object, name, meta) => {
if (meta.get && !meta.configurable) {
return defineProperty(object, name, {
...meta,
configurable: true,
});
}
return defineProperty(object, name, meta);
};