Skip to content

Commit 1542c24

Browse files
committed
fix(mock): allow assigning read-only properties
1 parent 5608150 commit 1542c24

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

projects/spectator/jest/test/spy-object/spy-object.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@ describe('SpyObject', () => {
99
person.sayHi.andReturn('Bye!');
1010
});
1111

12-
it('should keep getters/setters', () => {
12+
it('should enable spying on properties', () => {
1313
const person = createSpyObject(Person);
1414
person.birthYear = 1990;
1515
jest.spyOn(person, 'age', 'get').mockReturnValue(29);
1616

1717
expect(person.age).toBe(29);
1818
});
1919

20+
it('should enable setting properties by just assigning', () => {
21+
const person = createSpyObject(Person);
22+
person.birthYear = 1990;
23+
(person as any).age = 29;
24+
25+
expect(person.age).toBe(29);
26+
});
27+
2028
it('should allow setting properties', () => {
2129
const person = createSpyObject(Person);
2230

projects/spectator/src/lib/mock.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export function installProtoMethods<T>(mock: any, proto: any, createSpyFn: Funct
5555
mock[key] = createSpyFn(key);
5656
} else if (descriptor.get && !mock.hasOwnProperty(key)) {
5757
Object.defineProperty(mock, key, {
58-
get: () => null
58+
set: value => (mock[`_${key}`] = value),
59+
get: () => mock[`_${key}`]
5960
});
6061
}
6162
}

projects/spectator/test/spy-object/spy-object.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ describe('SpyObject', () => {
1717
expect(person.age).toBe(29);
1818
});
1919

20+
it('should enable setting properties by just assigning', () => {
21+
const person = createSpyObject(Person);
22+
person.birthYear = 1990;
23+
(person as any).age = 29;
24+
25+
expect(person.age).toBe(29);
26+
});
27+
2028
it('should allow setting properties', () => {
2129
const person = createSpyObject(Person);
2230

0 commit comments

Comments
 (0)