So this is telling you that the property is not writable
.
You can work around this by overriding the property descriptor for the duration of your test
Note that you can only do this if the property is configurable
.
I have found that in Angular if you do the below prop descriptors come as non-configurable so you will not be able to apply the solution
// this will cause prop descriptors to come as non-configurable
import from 'zone.js'
So in your test.ts
do this instead
// this will cause prop descriptors to come as configurable
import from 'zone.js/dist/zone'
import from 'zone.js/dist/zone-testing'
import * as someNsObj from 'external/lib';
// get the current descriptor
const originalDesc = Object.getOwnPropertyDescriptor(someNsObj, 'targetFunction');
// replace with a writable prop
beforeAll(() => {
Object.defineProperty(someNsObj, 'targetFunction', {
enumerable: true,
configurable: true,
writable: true, // this is what makes the difference
value: () => {}, // or whatever makes sense
});
});
// restore the original descriptor
afterAll(() => {
Object.defineProperty(someNsObj, 'targetFunction', originalDesc);
});