From 1f3f56a71dc867ec5913e7f10969688386628d31 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Fri, 24 Feb 2023 13:49:00 +0100 Subject: [PATCH] Test for overeager hoisting in Babel plugin --- .../__snapshots__/hoistPlugin.test.ts.snap | 56 +++++++++++++++++++ .../src/__tests__/hoistPlugin.test.ts | 26 +++++++++ 2 files changed, 82 insertions(+) diff --git a/packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap b/packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap index 29867c2c216c..0e87b272be52 100644 --- a/packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap +++ b/packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap @@ -230,3 +230,59 @@ function _getJestObj() { } `; + +exports[`babel-plugin-jest-hoist 11. jest.spyOn call on the imported module: 11. jest.spyOn call on the imported module 1`] = ` + +jest.mock('some-module', () => { + const module = jest.requireActual('some-module'); + jest.spyOn(module, 'add'); + return module; +}); + + + ↓ ↓ ↓ ↓ ↓ ↓ + +_getJestObj().mock('some-module', () => { + _getJestObj().spyOn(module, 'add'); + const module = jest.requireActual('some-module'); + return module; +}); +function _getJestObj() { + const {jest} = require('@jest/globals'); + _getJestObj = () => jest; + return jest; +} + +`; + +exports[`babel-plugin-jest-hoist 12. jest.spyOn call in class constructor: 12. jest.spyOn call in class constructor 1`] = ` + +jest.mock('some-module', () => { + const Actual = jest.requireActual('some-module'); + return class Mocked extends Actual { + constructor() { + super(); + jest.spyOn(module, 'add'); + } + }; +}); + + + ↓ ↓ ↓ ↓ ↓ ↓ + +_getJestObj().mock('some-module', () => { + const Actual = jest.requireActual('some-module'); + return class Mocked extends Actual { + constructor() { + _getJestObj().spyOn(module, 'add'); + super(); + } + }; +}); +function _getJestObj() { + const {jest} = require('@jest/globals'); + _getJestObj = () => jest; + return jest; +} + +`; diff --git a/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts b/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts index 7828b5095307..513b923a50f0 100644 --- a/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts +++ b/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts @@ -151,6 +151,32 @@ pluginTester({ formatResult, snapshot: true, }, + 'jest.spyOn call on the imported module': { + code: formatResult(` + jest.mock('some-module', () => { + const module = jest.requireActual('some-module'); + jest.spyOn(module, 'add'); + return module; + }); + `), + formatResult, + snapshot: true, + }, + 'jest.spyOn call in class constructor': { + code: formatResult(` + jest.mock('some-module', () => { + const Actual = jest.requireActual('some-module'); + return class Mocked extends Actual { + constructor() { + super(); + jest.spyOn(module, 'add'); + } + }; + }); + `), + formatResult, + snapshot: true, + }, }, /* eslint-enable */ });