Skip to content

Commit

Permalink
fix: mocking of getters/setters on automatically mocked classes (#13398)
Browse files Browse the repository at this point in the history
  • Loading branch information
staplespeter committed Oct 7, 2022
1 parent 30da552 commit f126336
Show file tree
Hide file tree
Showing 9 changed files with 966 additions and 122 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@
### Fixes

- `[babel-plugin-jest-hoist]` Ignore `TSTypeQuery` when checking for hoisted references ([#13367](https://github.com/facebook/jest/pull/13367))
- `[jest-mock]` Fix mocking of getters and setters on classes ([#13398](https://github.com/facebook/jest/pull/13398))
- `[jest-reporters]` Revert: Transform file paths into hyperlinks ([#13399](https://github.com/facebook/jest/pull/13399))
- `[@jest/types]` Infer type of `each` table correctly when the table is a tuple or array ([#13381](https://github.com/facebook/jest/pull/13381))
- `[@jest/types]` Rework typings to allow the `*ReturnedWith` matchers to be called with no argument ([#13385](https://github.com/facebook/jest/pull/13385))
Expand Down
36 changes: 36 additions & 0 deletions packages/jest-mock/src/__tests__/__fixtures__/SuperTestClass.ts
@@ -0,0 +1,36 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

export class SuperTestClass {
static staticTestProperty = 'staticTestProperty';

static get staticTestAccessor(): string {
return 'staticTestAccessor';
}

static set staticTestAccessor(_x: string) {
return;
}

static staticTestMethod(): string {
return 'staticTestMethod';
}

testProperty = 'testProperty';

get testAccessor(): string {
return 'testAccessor';
}
set testAccessor(_x: string) {
return;
}

testMethod(): string {
return 'testMethod';
}
}
11 changes: 11 additions & 0 deletions packages/jest-mock/src/__tests__/__fixtures__/TestClass.ts
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import {SuperTestClass} from './SuperTestClass';

export default class TestClass extends SuperTestClass {}
54 changes: 54 additions & 0 deletions packages/jest-mock/src/__tests__/__fixtures__/class-mocks-types.ts
@@ -0,0 +1,54 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

export default class SuperTestClass {
static staticTestProperty = 'staticTestProperty';

static get staticTestAccessor(): string {
return 'staticTestAccessor';
}

static set staticTestAccessor(_x: string) {
return;
}

static staticTestMethod(): string {
return 'staticTestMethod';
}

static get(): string {
return 'get';
}

static set(): string {
return 'set';
}

testProperty = 'testProperty';

get testAccessor(): string {
return 'testAccessor';
}
set testAccessor(_x: string) {
return;
}

testMethod(): string {
return 'testMethod';
}

get(): string {
return 'get';
}

set(): string {
return 'set';
}
}

export class TestClass extends SuperTestClass {}
130 changes: 130 additions & 0 deletions packages/jest-mock/src/__tests__/class-mocks-dual-import.test.ts
@@ -0,0 +1,130 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import {SuperTestClass} from './__fixtures__/SuperTestClass';
import TestClass from './__fixtures__/TestClass';
jest.mock('./__fixtures__/SuperTestClass');
jest.mock('./__fixtures__/TestClass');

describe('Testing the mocking of a class hierarchy defined in multiple imports', () => {
it('can call an instance method - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(SuperTestClass.prototype, 'testMethod')
.mockImplementation(() => {
return 'mockTestMethod';
});
const testClassInstance = new SuperTestClass();
expect(testClassInstance.testMethod()).toBe('mockTestMethod');
expect(mockTestMethod).toHaveBeenCalledTimes(1);

mockTestMethod.mockClear();
});

it('can call a superclass instance method - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(TestClass.prototype, 'testMethod')
.mockImplementation(() => {
return 'mockTestMethod';
});
const testClassInstance = new TestClass();
expect(testClassInstance.testMethod()).toBe('mockTestMethod');
expect(mockTestMethod).toHaveBeenCalledTimes(1);
});

it('can read a value from an instance getter - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(SuperTestClass.prototype, 'testAccessor', 'get')
.mockImplementation(() => {
return 'mockTestAccessor';
});
const testClassInstance = new SuperTestClass();
expect(testClassInstance.testAccessor).toBe('mockTestAccessor');
expect(mockTestMethod).toHaveBeenCalledTimes(1);

mockTestMethod.mockClear();
});

it('can read a value from a superclass instance getter - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(TestClass.prototype, 'testAccessor', 'get')
.mockImplementation(() => {
return 'mockTestAccessor';
});
const testClassInstance = new TestClass();
expect(testClassInstance.testAccessor).toBe('mockTestAccessor');
expect(mockTestMethod).toHaveBeenCalledTimes(1);
});

it('can write a value to an instance setter - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(SuperTestClass.prototype, 'testAccessor', 'set')
.mockImplementation((_x: string) => {
return () => {};
});
const testClassInstance = new SuperTestClass();
testClassInstance.testAccessor = '';
expect(mockTestMethod).toHaveBeenCalledTimes(1);

mockTestMethod.mockClear();
});

it('can write a value to a superclass instance setter - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(TestClass.prototype, 'testAccessor', 'set')
.mockImplementation((_x: string) => {
return () => {};
});
const testClassInstance = new TestClass();
testClassInstance.testAccessor = '';
expect(mockTestMethod).toHaveBeenCalledTimes(1);
});

it('can read a value from a static getter - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(SuperTestClass, 'staticTestAccessor', 'get')
.mockImplementation(() => {
return 'mockStaticTestAccessor';
});
expect(SuperTestClass.staticTestAccessor).toBe('mockStaticTestAccessor');
expect(mockTestMethod).toHaveBeenCalledTimes(1);

mockTestMethod.mockClear();
});

it('can read a value from a superclass static getter - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(TestClass, 'staticTestAccessor', 'get')
.mockImplementation(() => {
return 'mockStaticTestAccessor';
});
expect(TestClass.staticTestAccessor).toBe('mockStaticTestAccessor');
expect(mockTestMethod).toHaveBeenCalledTimes(1);
});

it('can write a value to a static setter - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(SuperTestClass, 'staticTestAccessor', 'set')
.mockImplementation((_x: string) => {
return () => {};
});
SuperTestClass.staticTestAccessor = '';
expect(mockTestMethod).toHaveBeenCalledTimes(1);

mockTestMethod.mockClear();
});

it('can write a value to a superclass static setter - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(TestClass, 'staticTestAccessor', 'set')
.mockImplementation((_x: string) => {
return () => {};
});
TestClass.staticTestAccessor = '';
expect(mockTestMethod).toHaveBeenCalledTimes(1);
});
});

0 comments on commit f126336

Please sign in to comment.