Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mocking of getters/setters on automatically mocked classes #13460

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
8d31725
Revert "Revert "fix: mocking of getters/setters on automatically mock…
staplespeter Oct 6, 2022
46c92d6
bug 13140 - Added condition to prevent refs to auto-mocked superclass…
staplespeter Oct 6, 2022
571ad48
Merge branch 'main' into bug-13140
staplespeter Oct 6, 2022
d315f78
bug 13140 - Lint changes
staplespeter Oct 7, 2022
6c3af0d
bug 13140 - requested repo reorg
staplespeter Oct 7, 2022
c36a196
Update jest.config.mjs
staplespeter Oct 7, 2022
e7e4242
Update CHANGELOG.md
SimenB Oct 7, 2022
9be113c
Merge branch 'main' into bug-13140
SimenB Oct 7, 2022
2d2648d
Merge branch 'main' into bug-13140
staplespeter Oct 15, 2022
e7f1a1c
bug-13140 - added tests for function exports mocked using spyOn. The…
staplespeter Oct 15, 2022
d9efe2d
bug-13140 - reinstating spyOn functionality that allowed mocking of f…
staplespeter Oct 15, 2022
f329d8a
bug-13140: Added restoring of accessor properties. Updated tests for…
staplespeter Oct 17, 2022
fa0843d
Merge branch 'main' into bug-13140
staplespeter Oct 17, 2022
bd5ab67
bug-13140: falling back to user of hasOwnProperty() in tests
staplespeter Oct 17, 2022
80375e2
bug-13140: applying lint check exception per line
staplespeter Oct 17, 2022
8907420
Merge branch 'main' into bug-13140
SimenB Oct 18, 2022
da76c29
Merge branch 'facebook:main' into bug-13140
staplespeter Oct 18, 2022
2facdf1
bug-13140: reverting spyOn methods to the original format.
staplespeter Oct 18, 2022
ee0013a
bug-13140: reinstating change removed from last merge frommain
staplespeter Oct 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 92 additions & 0 deletions packages/jest-mock/src/__tests__/class-mocks-dual-import.test.ts
Expand Up @@ -35,4 +35,96 @@ describe('Testing the mocking of a class hierarchy defined in multiple imports',
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);
});
});
94 changes: 94 additions & 0 deletions packages/jest-mock/src/__tests__/class-mocks-single-import.test.ts
Expand Up @@ -109,6 +109,54 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
mockTestMethod.mockClear();
});

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(testTypes.TestClass.prototype, 'testAccessor', 'get')
.mockImplementation(() => {
return 'mockTestAccessor';
});
const testClassInstance = new testTypes.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(testTypes.TestClass.prototype, 'testAccessor', 'set')
.mockImplementation((_x: string) => {
return () => {};
});
const testClassInstance = new testTypes.TestClass();
testClassInstance.testAccessor = '';
expect(mockTestMethod).toHaveBeenCalledTimes(1);
});

it('can call a static method - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(SuperTestClass, 'staticTestMethod')
Expand Down Expand Up @@ -178,4 +226,50 @@ describe('Testing the mocking of a class hierarchy defined in a single import',

mockTestMethod.mockClear();
});

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(testTypes.TestClass, 'staticTestAccessor', 'get')
.mockImplementation(() => {
return 'mockStaticTestAccessor';
});
expect(testTypes.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(testTypes.TestClass, 'staticTestAccessor', 'set')
.mockImplementation((_x: string) => {
return () => {};
});
testTypes.TestClass.staticTestAccessor = '';
expect(mockTestMethod).toHaveBeenCalledTimes(1);
});
});
14 changes: 13 additions & 1 deletion packages/jest-mock/src/__tests__/class-mocks.test.ts
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*
*/

describe('Testing the mocking of a class', () => {
it('can call an instance method', () => {
class TestClass {
Expand Down Expand Up @@ -169,6 +168,8 @@ describe('Testing the mocking of a class', () => {

mockFn.mockRestore();
expect(testClassInstance.testMethod).toBe('testMethod');
// eslint-disable-next-line no-prototype-builtins
expect(TestClass.prototype.hasOwnProperty('testMethod')).toBe(false);
});

it('can write a value to an instance setter', () => {
Expand Down Expand Up @@ -215,6 +216,8 @@ describe('Testing the mocking of a class', () => {
mocktestMethod.mockRestore();
testClassInstance.testMethod = '';
expect(mocktestMethod).toHaveBeenCalledTimes(0);
// eslint-disable-next-line no-prototype-builtins
expect(TestClass.prototype.hasOwnProperty('testMethod')).toBe(false);
});

it('can call a static method', () => {
Expand Down Expand Up @@ -253,6 +256,8 @@ describe('Testing the mocking of a class', () => {

mockFn.mockRestore();
expect(TestClass.testMethod()).toBe('testMethod');
// eslint-disable-next-line no-prototype-builtins
expect(TestClass.hasOwnProperty('testMethod')).toBe(false);
});

it('can call a static method named "get"', () => {
Expand Down Expand Up @@ -363,6 +368,8 @@ describe('Testing the mocking of a class', () => {

mockFn.mockRestore();
expect(TestClass.testMethod).toBe('testMethod');
// eslint-disable-next-line no-prototype-builtins
expect(TestClass.hasOwnProperty('testMethod')).toBe(false);
});

it('can write a value to a static setter', () => {
Expand Down Expand Up @@ -402,5 +409,10 @@ describe('Testing the mocking of a class', () => {
});
TestClass.testMethod = '';
expect(mocktestMethod).toHaveBeenCalledTimes(1);

mocktestMethod.mockRestore();
expect(mocktestMethod).toHaveBeenCalledTimes(0);
// eslint-disable-next-line no-prototype-builtins
expect(TestClass.hasOwnProperty('testMethod')).toBe(false);
});
});