Skip to content

Commit

Permalink
Revert "fix: mocking of getters/setters on automatically mocked class…
Browse files Browse the repository at this point in the history
…es (jestjs#13398)"

This reverts commit f126336.
  • Loading branch information
SimenB committed Oct 18, 2022
1 parent e61ea77 commit f21657e
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 393 deletions.
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';
};
92 changes: 0 additions & 92 deletions packages/jest-mock/src/__tests__/class-mocks-dual-import.test.ts
Expand Up @@ -35,96 +35,4 @@ 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);
});
});
141 changes: 36 additions & 105 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,65 +98,17 @@ 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);

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(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 call a static method - Auto-mocked class', () => {
const mockTestMethod = jest
.spyOn(SuperTestClass, 'staticTestMethod')
Expand All @@ -148,11 +123,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 +145,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,57 +169,13 @@ 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(mockTestMethod).toHaveBeenCalledTimes(1);

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(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(testTypes.TestClass.set()).toBe('mockTestMethod');
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 f21657e

Please sign in to comment.