Skip to content

Commit fde6da7

Browse files
committed
Handle TypeError on read-only properties in monkey patching tests
GJS throws `TypeError` if you try to assign a read-only property.
1 parent 2d8d0d6 commit fde6da7

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

spec/core/jasmineNamespaceSpec.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@ describe('The jasmine namespace', function() {
3333
const existingVal = jasmineUnderTest[key];
3434

3535
try {
36-
jasmineUnderTest[key] = 'monkey patch';
36+
try {
37+
jasmineUnderTest[key] = 'monkey patch';
38+
} catch (e) {
39+
expect(e).toBeInstanceOf(TypeError);
40+
}
41+
3742
expect(jasmineUnderTest[key]).toBe(existingVal);
3843
} finally {
3944
// This will be a no-op if the test passed, but will prevent state
4045
// leakage if it failed.
41-
jasmineUnderTest[key] = existingVal;
46+
if (jasmineUnderTest[key] !== existingVal) {
47+
jasmineUnderTest[key] = existingVal;
48+
}
4249
}
4350
});
4451
}

spec/helpers/monkeyPatchingSpecs.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ globalThis.isNonMonkeyPatchableClass = function(ctor, makeInstance) {
44
const existing = ctor.prototype;
55

66
try {
7-
ctor.prototype = {};
7+
try {
8+
ctor.prototype = {};
9+
} catch (e) {
10+
expect(e).toBeInstanceOf(TypeError);
11+
}
12+
813
expect(ctor.prototype).toBe(existing);
914
} finally {
1015
// This will be a no-op if the test passed, but will prevent state
1116
// leakage if it failed.
12-
ctor.prototype = existing;
17+
if (ctor.prototype !== existing) {
18+
ctor.prototype = existing;
19+
}
1320
}
1421
});
1522

@@ -35,14 +42,21 @@ globalThis.isNonMonkeyPatchableClass = function(ctor, makeInstance) {
3542
const existingValue = ctor.prototype[k];
3643

3744
try {
38-
ctor.prototype[k] = {};
45+
try {
46+
ctor.prototype[k] = {};
47+
} catch (e) {
48+
expect(e).toBeInstanceOf(TypeError);
49+
}
50+
3951
expect(ctor.prototype[k])
4052
.withContext(k)
4153
.toBe(existingValue);
4254
} finally {
4355
// This will be a no-op if the test passed, but will prevent state
4456
// leakage if it failed.
45-
ctor.prototype[k] = existingValue;
57+
if (ctor.prototype[k] !== existingValue) {
58+
ctor.prototype[k] = existingValue;
59+
}
4660
}
4761
}
4862

@@ -55,7 +69,13 @@ globalThis.isNonMonkeyPatchableClass = function(ctor, makeInstance) {
5569

5670
for (const k of Object.getOwnPropertyNames(ctor.prototype)) {
5771
any = true;
58-
instance[k] = {};
72+
73+
try {
74+
instance[k] = {};
75+
} catch (e) {
76+
expect(e).toBeInstanceOf(TypeError);
77+
}
78+
5979
expect(instance[k])
6080
.withContext(k)
6181
.toBe(ctor.prototype[k]);

0 commit comments

Comments
 (0)