Skip to content

Commit

Permalink
bug-13140-001 - Added a spyOn test for class exports exposed through …
Browse files Browse the repository at this point in the history
…getters
  • Loading branch information
staplespeter committed Oct 14, 2022
1 parent 780dc05 commit a546fc7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
30 changes: 30 additions & 0 deletions packages/jest-mock/src/__tests__/class-mocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,40 @@
* LICENSE file in the root directory of this source tree.
*
*/
import {createTestScheduler} from '@jest/core';
import * as reporters from '@jest/reporters';
import {makeGlobalConfig} from '@jest/test-utils';
import * as testTypes from './__fixtures__/class-mocks-types';
import * as m from './__fixtures__/index';

describe('Testing the mocking of exported functions', () => {
test('works with default value', async () => {
jest.spyOn(reporters, 'CoverageReporter');
jest.spyOn(reporters, 'DefaultReporter');
jest.spyOn(reporters, 'GitHubActionsReporter');
jest.spyOn(reporters, 'NotifyReporter');
jest.spyOn(reporters, 'SummaryReporter');
jest.spyOn(reporters, 'VerboseReporter');
jest.spyOn(reporters, 'CoverageReporter');

await createTestScheduler(
makeGlobalConfig({
reporters: undefined,
}),
{
firstRun: true,
previousSuccess: false,
},
);

expect(reporters.DefaultReporter).toHaveBeenCalledTimes(1);
expect(reporters.VerboseReporter).toHaveBeenCalledTimes(0);
expect(reporters.GitHubActionsReporter).toHaveBeenCalledTimes(0);
expect(reporters.NotifyReporter).toHaveBeenCalledTimes(0);
expect(reporters.CoverageReporter).toHaveBeenCalledTimes(0);
expect(reporters.SummaryReporter).toHaveBeenCalledTimes(1);
});

test('adds 1 + 2 to equal 3', () => {
const spy = jest.spyOn(m, 'f2').mockImplementationOnce(jest.fn());
m.f2();
Expand Down
5 changes: 2 additions & 3 deletions packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ export class ModuleMocker {
if (!descriptor) {
throw new Error(`${String(methodKey)} property does not exist`);
}
if (!descriptor.configurable) {
if (!accessType && !descriptor.get && !descriptor.configurable) {
throw new Error(`${String(methodKey)} is not declared configurable`);
}

Expand Down Expand Up @@ -1222,7 +1222,6 @@ export class ModuleMocker {
delete object[methodKey];
}
});

descriptor.get = mock;
mock.mockImplementation(function (this: unknown) {
return originalAccessor.call(this);
Expand All @@ -1238,7 +1237,6 @@ export class ModuleMocker {
delete object[methodKey];
}
});

descriptor.set = mock;
mock.mockImplementation(function (this: unknown) {
return originalAccessor.call(this, arguments[0]);
Expand All @@ -1250,6 +1248,7 @@ export class ModuleMocker {
descriptor!.get = originalGet;
Object.defineProperty(object, methodKey, descriptor!);
});
descriptor.configurable = true;
descriptor.get = () => mock;
Object.defineProperty(object, methodKey, descriptor);
} else {
Expand Down

0 comments on commit a546fc7

Please sign in to comment.