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 #13398

Merged
merged 8 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export default {
'/packages/jest-haste-map/src/__tests__/haste_impl.js',
'/packages/jest-haste-map/src/__tests__/dependencyExtractor.js',
'/packages/jest-haste-map/src/__tests__/test_dotfiles_root/',
'/packages/jest-mock/src/__tests__/class-mocks-types.ts',
'/packages/jest-mock/src/__tests__/TestClass.ts',
'/packages/jest-mock/src/__tests__/SuperTestClass.ts',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor detail. Could you move these files to /packages/jest-mock/src/__tests__/__fixtures__/? Recently I added /__fixtures__/ (see above) to this list to make it less noisy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, though I cannot see the /packages/jest-mock/src/__tests__/__fixtures__/ folder. The packages/jest-mock/__typetests__/__fixtures__/ folder does not exist either though I would have expected this to be a candidate for this reorg. Have I missed a commit/made a bad merge?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no folder at the moment. Just create it (;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to say that if you create src/__tests__/__fixtures__/ folder it will get ignored, because __fixtures__ was recently added to the ignore list. All would work without tweaking Jest config. That was the idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, I interpreted your initial comment as being that you had already done so, somewhere.

staplespeter marked this conversation as resolved.
Show resolved Hide resolved
'/packages/jest-repl/src/__tests__/test_root',
'/packages/jest-runtime/src/__tests__/defaultResolver.js',
'/packages/jest-runtime/src/__tests__/module_dir/',
Expand Down
36 changes: 36 additions & 0 deletions packages/jest-mock/src/__tests__/SuperTestClass.ts
Original file line number Diff line number Diff line change
@@ -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__/TestClass.ts
Original file line number Diff line number Diff line change
@@ -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 {}
130 changes: 130 additions & 0 deletions packages/jest-mock/src/__tests__/class-mocks-dual-import.test.ts
Original file line number Diff line number Diff line change
@@ -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 './SuperTestClass';
import TestClass from './TestClass';
jest.mock('./SuperTestClass');
jest.mock('./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);
});
});
Loading