From a546fc7d99494e43472888436fd880ca9b0df26b Mon Sep 17 00:00:00 2001 From: Peter Staples Date: Sat, 15 Oct 2022 00:35:21 +0100 Subject: [PATCH] bug-13140-001 - Added a spyOn test for class exports exposed through getters --- .../src/__tests__/class-mocks.test.ts | 30 +++++++++++++++++++ packages/jest-mock/src/index.ts | 5 ++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/jest-mock/src/__tests__/class-mocks.test.ts b/packages/jest-mock/src/__tests__/class-mocks.test.ts index e6635f8f03c0..8cc861fd20bb 100644 --- a/packages/jest-mock/src/__tests__/class-mocks.test.ts +++ b/packages/jest-mock/src/__tests__/class-mocks.test.ts @@ -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(); diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index f4c9b5776034..775ec9a173ac 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -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`); } @@ -1222,7 +1222,6 @@ export class ModuleMocker { delete object[methodKey]; } }); - descriptor.get = mock; mock.mockImplementation(function (this: unknown) { return originalAccessor.call(this); @@ -1238,7 +1237,6 @@ export class ModuleMocker { delete object[methodKey]; } }); - descriptor.set = mock; mock.mockImplementation(function (this: unknown) { return originalAccessor.call(this, arguments[0]); @@ -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 {