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 14 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
13 changes: 13 additions & 0 deletions packages/jest-mock/src/__tests__/__fixtures__/class-mocks-types.ts
Expand Up @@ -52,3 +52,16 @@ export default class SuperTestClass {
}

export class TestClass extends SuperTestClass {}

export function testFunction1() {
return 'testFunction1';
}

function testFunction() {
return 'testFunction2';
}
export const testFunction2 = testFunction;

export const testFunction3 = () => {
return 'testFunction3';
};
67 changes: 46 additions & 21 deletions packages/jest-mock/src/__tests__/class-mocks-single-import.test.ts
Expand Up @@ -6,9 +6,32 @@
*
*/

import SuperTestClass, {TestClass} from './__fixtures__/class-mocks-types';
import SuperTestClass, * as testTypes from './__fixtures__/class-mocks-types';
jest.mock('./__fixtures__/class-mocks-types');

describe('Testing the mocking of exported functions', () => {
it('can mock a directly exported function', () => {
jest.spyOn(testTypes, 'testFunction1').mockImplementation(() => {
return 'mockTestFunction';
});
expect(testTypes.testFunction1()).toBe('mockTestFunction');
});

it('can mock an indirectly exported function', () => {
jest.spyOn(testTypes, 'testFunction2').mockImplementation(() => {
return 'mockTestFunction';
});
expect(testTypes.testFunction2()).toBe('mockTestFunction');
});

it('can mock an indirectly exported anonymous function', () => {
jest.spyOn(testTypes, 'testFunction3').mockImplementation(() => {
return 'mockTestFunction';
});
expect(testTypes.testFunction3()).toBe('mockTestFunction');
});
});

describe('Testing the mocking of a class hierarchy defined in a single import', () => {
it('can call an instance method - Auto-mocked class', () => {
const mockTestMethod = jest
Expand All @@ -25,11 +48,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',

it('can call a superclass instance method - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(TestClass.prototype, 'testMethod')
.spyOn(testTypes.TestClass.prototype, 'testMethod')
.mockImplementation(() => {
return 'mockTestMethod';
});
const testClassInstance = new TestClass();
const testClassInstance = new testTypes.TestClass();
expect(testClassInstance.testMethod()).toBe('mockTestMethod');
expect(mockTestMethod).toHaveBeenCalledTimes(1);
});
Expand All @@ -49,11 +72,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',

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

Expand All @@ -75,11 +98,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',

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

Expand All @@ -101,11 +124,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',

it('can read a value from a superclass instance getter - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(TestClass.prototype, 'testAccessor', 'get')
.spyOn(testTypes.TestClass.prototype, 'testAccessor', 'get')
.mockImplementation(() => {
return 'mockTestAccessor';
});
const testClassInstance = new TestClass();
const testClassInstance = new testTypes.TestClass();
expect(testClassInstance.testAccessor).toBe('mockTestAccessor');
expect(mockTestMethod).toHaveBeenCalledTimes(1);
});
Expand All @@ -125,11 +148,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',

it('can write a value to a superclass instance setter - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(TestClass.prototype, 'testAccessor', 'set')
.spyOn(testTypes.TestClass.prototype, 'testAccessor', 'set')
.mockImplementation((_x: string) => {
return () => {};
});
const testClassInstance = new TestClass();
const testClassInstance = new testTypes.TestClass();
testClassInstance.testAccessor = '';
expect(mockTestMethod).toHaveBeenCalledTimes(1);
});
Expand All @@ -148,11 +171,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',

it('can call a superclass static method - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(TestClass, 'staticTestMethod')
.spyOn(testTypes.TestClass, 'staticTestMethod')
.mockImplementation(() => {
return 'mockTestMethod';
});
expect(TestClass.staticTestMethod()).toBe('mockTestMethod');
expect(testTypes.TestClass.staticTestMethod()).toBe('mockTestMethod');
expect(mockTestMethod).toHaveBeenCalledTimes(1);
});

Expand All @@ -170,11 +193,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',

it('can call a superclass static method named "get" - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(TestClass, 'get')
.spyOn(testTypes.TestClass, 'get')
.mockImplementation(() => {
return 'mockTestMethod';
});
expect(TestClass.get()).toBe('mockTestMethod');
expect(testTypes.TestClass.get()).toBe('mockTestMethod');
expect(mockTestMethod).toHaveBeenCalledTimes(1);

mockTestMethod.mockClear();
Expand All @@ -194,11 +217,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',

it('can call a superclass static method named "set" - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(TestClass, 'set')
.spyOn(testTypes.TestClass, 'set')
.mockImplementation(() => {
return 'mockTestMethod';
});
expect(TestClass.set()).toBe('mockTestMethod');
expect(testTypes.TestClass.set()).toBe('mockTestMethod');
expect(mockTestMethod).toHaveBeenCalledTimes(1);

mockTestMethod.mockClear();
Expand All @@ -218,11 +241,13 @@ describe('Testing the mocking of a class hierarchy defined in a single import',

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

Expand All @@ -240,11 +265,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',

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