diff --git a/e2e/__cases__/hoisting/__test_modules__/Mocked.ts b/e2e/__cases__/hoisting/__test_modules__/Mocked.ts new file mode 100644 index 0000000000..952ceeb25e --- /dev/null +++ b/e2e/__cases__/hoisting/__test_modules__/Mocked.ts @@ -0,0 +1,7 @@ +export default class Mocked { + readonly isMocked: boolean + + constructor() { + this.isMocked = true; + } +} diff --git a/e2e/__cases__/hoisting/__test_modules__/Unmocked.ts b/e2e/__cases__/hoisting/__test_modules__/Unmocked.ts new file mode 100644 index 0000000000..c90172a18e --- /dev/null +++ b/e2e/__cases__/hoisting/__test_modules__/Unmocked.ts @@ -0,0 +1,7 @@ +export default class Unmocked { + readonly isUnmocked: boolean + + constructor() { + this.isUnmocked = true; + } +} diff --git a/e2e/__cases__/hoisting/__test_modules__/a.ts b/e2e/__cases__/hoisting/__test_modules__/a.ts new file mode 100644 index 0000000000..986d8bec6c --- /dev/null +++ b/e2e/__cases__/hoisting/__test_modules__/a.ts @@ -0,0 +1 @@ +export default () => 'unmocked'; diff --git a/e2e/__cases__/hoisting/__test_modules__/b.ts b/e2e/__cases__/hoisting/__test_modules__/b.ts new file mode 100644 index 0000000000..986d8bec6c --- /dev/null +++ b/e2e/__cases__/hoisting/__test_modules__/b.ts @@ -0,0 +1 @@ +export default () => 'unmocked'; diff --git a/e2e/__cases__/hoisting/__test_modules__/c.ts b/e2e/__cases__/hoisting/__test_modules__/c.ts new file mode 100644 index 0000000000..986d8bec6c --- /dev/null +++ b/e2e/__cases__/hoisting/__test_modules__/c.ts @@ -0,0 +1 @@ +export default () => 'unmocked'; diff --git a/e2e/__cases__/hoisting/__test_modules__/d.ts b/e2e/__cases__/hoisting/__test_modules__/d.ts new file mode 100644 index 0000000000..986d8bec6c --- /dev/null +++ b/e2e/__cases__/hoisting/__test_modules__/d.ts @@ -0,0 +1 @@ +export default () => 'unmocked'; diff --git a/e2e/__cases__/hoisting/__test_modules__/e.ts b/e2e/__cases__/hoisting/__test_modules__/e.ts new file mode 100644 index 0000000000..986d8bec6c --- /dev/null +++ b/e2e/__cases__/hoisting/__test_modules__/e.ts @@ -0,0 +1 @@ +export default () => 'unmocked'; diff --git a/e2e/__cases__/hoisting/disable-automock/disable-automock.spec.ts b/e2e/__cases__/hoisting/disable-automock/disable-automock.spec.ts deleted file mode 100644 index 6376ac0970..0000000000 --- a/e2e/__cases__/hoisting/disable-automock/disable-automock.spec.ts +++ /dev/null @@ -1,9 +0,0 @@ -import hello from './disable-automock' - -jest.disableAutomock() - -test('original implementation', () => { - // now we have the original implementation, - // even if we set the automocking in a jest configuration - expect(hello()).toBe('hi!') -}) diff --git a/e2e/__cases__/hoisting/disable-automock/disable-automock.ts b/e2e/__cases__/hoisting/disable-automock/disable-automock.ts deleted file mode 100644 index b1a4ec3c7d..0000000000 --- a/e2e/__cases__/hoisting/disable-automock/disable-automock.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default function() { - return 'hi!' -} diff --git a/e2e/__cases__/hoisting/enable-automock/enable-automock.spec.ts b/e2e/__cases__/hoisting/enable-automock/enable-automock.spec.ts deleted file mode 100644 index e1fdcb8234..0000000000 --- a/e2e/__cases__/hoisting/enable-automock/enable-automock.spec.ts +++ /dev/null @@ -1,9 +0,0 @@ -jest.enableAutomock() - -import hello from './enable-automock' - -test('original implementation', () => { - // now we have the mocked implementation, - // @ts-expect-error - expect(hello._isMockFunction).toBeTruthy() -}) diff --git a/e2e/__cases__/hoisting/enable-automock/enable-automock.ts b/e2e/__cases__/hoisting/enable-automock/enable-automock.ts deleted file mode 100644 index b1a4ec3c7d..0000000000 --- a/e2e/__cases__/hoisting/enable-automock/enable-automock.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default function() { - return 'hi!' -} diff --git a/e2e/__cases__/hoisting/general-hoisting.spec.ts b/e2e/__cases__/hoisting/general-hoisting.spec.ts new file mode 100644 index 0000000000..10ca24aea6 --- /dev/null +++ b/e2e/__cases__/hoisting/general-hoisting.spec.ts @@ -0,0 +1,49 @@ +import Unmocked from './__test_modules__/Unmocked' +import Mocked from './__test_modules__/Mocked' +import b from './__test_modules__/b' +import c from './__test_modules__/c' +import d from './__test_modules__/d' + +// These will all be hoisted above imports +jest.deepUnmock('./__test_modules__/Unmocked') +jest.unmock('./__test_modules__/c').unmock('./__test_modules__/d') + +let e: any; +(function () { + const _getJestObj = 42; + e = require('./__test_modules__/e').default; + // hoisted to the top of the function scope + jest.unmock('./__test_modules__/e') +})(); + +// These will not be hoisted +jest.unmock('./__test_modules__/a').dontMock('./__test_modules__/b') +jest.unmock('./__test_modules__/' + 'a') +jest.dontMock('./__test_modules__/Mocked') + +it('hoists unmocked modules before imports', () => { + // @ts-expect-error + expect(Unmocked._isMockFunction).toBeUndefined() + expect(new Unmocked().isUnmocked).toEqual(true) + + // @ts-expect-error + expect(c._isMockFunction).toBeUndefined() + expect(c()).toEqual('unmocked') + + // @ts-expect-error + expect(d._isMockFunction).toBeUndefined() + expect(d()).toEqual('unmocked') + + expect(e._isMock).toBe(undefined) + expect(e()).toEqual('unmocked') +}); + +it('does not hoist dontMock calls before imports', () => { + // @ts-expect-error + expect(Mocked._isMockFunction).toBe(true) + expect(new Mocked().isMocked).toEqual(undefined) + + // @ts-expect-error + expect(b._isMockFunction).toBe(true) + expect(b()).toEqual(undefined) +}); diff --git a/e2e/__cases__/hoisting/import-jest.spec.ts b/e2e/__cases__/hoisting/import-jest.spec.ts new file mode 100644 index 0000000000..dd30ace412 --- /dev/null +++ b/e2e/__cases__/hoisting/import-jest.spec.ts @@ -0,0 +1,37 @@ +import {jest} from '@jest/globals' +import {jest as aliasedJest} from '@jest/globals' +import * as JestGlobals from '@jest/globals' + +import a from './__test_modules__/a'; +import b from './__test_modules__/b'; +import c from './__test_modules__/c'; + +// These will be hoisted above imports +jest.unmock('./__test_modules__/a') +aliasedJest.unmock('./__test_modules__/b') +JestGlobals.jest.unmock('./__test_modules__/c') + +// tests + +test('named import', () => { + // @ts-expect-error + expect(a._isMockFunction).toBeUndefined() + expect(a()).toBe('unmocked') +}); + +test('aliased named import', () => { + // @ts-expect-error TODO: fix aliased named import + expect(b._isMockFunction).toBe(true) + expect(b()).toBeUndefined() + // expect(b._isMockFunction).toBe(undefined) + // expect(b()).toBe('unmocked') +}); + +test('namespace import', () => { + // @ts-expect-error TODO: fix namespace import + expect(c._isMockFunction).toBe(true) + expect(c()).toBeUndefined() + // expect(c._isMockFunction).toBe(undefined) + // expect(c()).toBe('unmocked') +}); + diff --git a/e2e/__cases__/hoisting/mock-unmock/mock-unmock.spec.ts b/e2e/__cases__/hoisting/mock-unmock/mock-unmock.spec.ts deleted file mode 100644 index 4674c3d557..0000000000 --- a/e2e/__cases__/hoisting/mock-unmock/mock-unmock.spec.ts +++ /dev/null @@ -1,13 +0,0 @@ -import hello from './mock-unmock' - -jest.mock('./mock-unmock') - -const original = jest.requireActual('./mock-unmock').default -it('should have been mocked', () => { - const msg = hello() - expect(hello).not.toBe(original) - expect(msg).toBeUndefined() - expect(hello).toHaveProperty('mock') - expect(require('foo')).toBe('bar') - jest.mock('foo', () => 'bar', { virtual: true }) -}) diff --git a/e2e/__cases__/hoisting/mock-unmock/mock-unmock.ts b/e2e/__cases__/hoisting/mock-unmock/mock-unmock.ts deleted file mode 100644 index bffa1c5a4a..0000000000 --- a/e2e/__cases__/hoisting/mock-unmock/mock-unmock.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default function() { - return 'hi!'; -} diff --git a/e2e/__helpers__/test-case/types.ts b/e2e/__helpers__/test-case/types.ts index 2c987bf311..e568d9e4c6 100644 --- a/e2e/__helpers__/test-case/types.ts +++ b/e2e/__helpers__/test-case/types.ts @@ -9,7 +9,7 @@ export interface RunTestOptions { env?: Record inject?: (() => any) | string writeIo?: boolean - jestConfig?: Config.ProjectConfig | any + jestConfig?: Config.ProjectConfig | Record tsJestConfig?: TsJestGlobalOptions noCache?: boolean jestConfigPath?: string diff --git a/e2e/__templates__/with-babel-7-string-config/babel.config.js b/e2e/__templates__/with-babel-7-string-config/babel.config.js index caeabae37f..5823621870 100644 --- a/e2e/__templates__/with-babel-7-string-config/babel.config.js +++ b/e2e/__templates__/with-babel-7-string-config/babel.config.js @@ -1,5 +1,5 @@ module.exports = { presets: [ - "@babel/preset-env" - ] + '@babel/preset-env', + ], } diff --git a/e2e/__templates__/with-babel-7/babel.config.js b/e2e/__templates__/with-babel-7/babel.config.js index caeabae37f..5823621870 100644 --- a/e2e/__templates__/with-babel-7/babel.config.js +++ b/e2e/__templates__/with-babel-7/babel.config.js @@ -1,5 +1,5 @@ module.exports = { presets: [ - "@babel/preset-env" - ] + '@babel/preset-env', + ], } diff --git a/e2e/__tests__/__snapshots__/hoisting.test.ts.snap b/e2e/__tests__/__snapshots__/hoisting.test.ts.snap index f4ca916a1e..5265ec87b1 100644 --- a/e2e/__tests__/__snapshots__/hoisting.test.ts.snap +++ b/e2e/__tests__/__snapshots__/hoisting.test.ts.snap @@ -1,588 +1,50 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Hoisting jest.disableAutomock() should pass using template "default": io-disableAutomock 1`] = ` - ===[ FILE: disable-automock.spec.ts ]=========================================== - "use strict"; - Object.defineProperty(exports, "__esModule", { value: true }); - jest.disableAutomock(); - var disable_automock_1 = require("./disable-automock"); - test('original implementation', function () { - // now we have the original implementation, - // even if we set the automocking in a jest configuration - expect(disable_automock_1.default()).toBe('hi!'); - }); - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiPGN3ZD4vZGlzYWJsZS1hdXRvbW9jay5zcGVjLnRzIiwibWFwcGluZ3MiOiI7O0FBRUEsSUFBSSxDQUFDLGVBQWUsRUFBRSxDQUFBO0FBRnRCLHVEQUFzQztBQUl0QyxJQUFJLENBQUMseUJBQXlCLEVBQUU7SUFDOUIsMkNBQTJDO0lBQzNDLHlEQUF5RDtJQUN6RCxNQUFNLENBQUMsMEJBQUssRUFBRSxDQUFDLENBQUMsSUFBSSxDQUFDLEtBQUssQ0FBQyxDQUFBO0FBQzdCLENBQUMsQ0FBQyxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbIjxjd2Q+L2Rpc2FibGUtYXV0b21vY2suc3BlYy50cyJdLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgaGVsbG8gZnJvbSAnLi9kaXNhYmxlLWF1dG9tb2NrJ1xuXG5qZXN0LmRpc2FibGVBdXRvbW9jaygpXG5cbnRlc3QoJ29yaWdpbmFsIGltcGxlbWVudGF0aW9uJywgKCkgPT4ge1xuICAvLyBub3cgd2UgaGF2ZSB0aGUgb3JpZ2luYWwgaW1wbGVtZW50YXRpb24sXG4gIC8vIGV2ZW4gaWYgd2Ugc2V0IHRoZSBhdXRvbW9ja2luZyBpbiBhIGplc3QgY29uZmlndXJhdGlvblxuICBleHBlY3QoaGVsbG8oKSkudG9CZSgnaGkhJylcbn0pXG4iXSwidmVyc2lvbiI6M30= - ===[ INLINE SOURCE MAPS ]======================================================= - file: /disable-automock.spec.ts - mappings: >- - ;;AAEA,IAAI,CAAC,eAAe,EAAE,CAAA;AAFtB,uDAAsC;AAItC,IAAI,CAAC,yBAAyB,EAAE;IAC9B,2CAA2C;IAC3C,yDAAyD;IACzD,MAAM,CAAC,0BAAK,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;AAC7B,CAAC,CAAC,CAAA - names: [] - sources: - - /disable-automock.spec.ts - sourcesContent: - - | - import hello from './disable-automock' - - jest.disableAutomock() - - test('original implementation', () => { - // now we have the original implementation, - // even if we set the automocking in a jest configuration - expect(hello()).toBe('hi!') - }) - version: 3 - ================================================================================ -`; - -exports[`Hoisting jest.disableAutomock() should pass using template "default": output-disableAutomock 1`] = ` +exports[`Hoisting should pass using template "default": should pass using template "default" 1`] = ` √ jest --no-cache ↳ exit code: 0 ===[ STDOUT ]=================================================================== ===[ STDERR ]=================================================================== - PASS ./disable-automock.spec.ts - √ original implementation + PASS ./general-hoisting.spec.ts + PASS ./import-jest.spec.ts - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total + Test Suites: 2 passed, 2 total + Tests: 5 passed, 5 total Snapshots: 0 total Time: XXs Ran all test suites. ================================================================================ `; -exports[`Hoisting jest.disableAutomock() should pass using template "with-babel-7": io-disableAutomock 1`] = ` - ===[ FILE: disable-automock.spec.ts ]=========================================== - "use strict"; - - _getJestObj().disableAutomock(); - - function _getJestObj() { - var _require = require("@jest/globals"), - jest = _require.jest; - - _getJestObj = function _getJestObj() { - return jest; - }; - - return jest; - } - - Object.defineProperty(exports, "__esModule", { - value: true - }); - - var disable_automock_1 = require("./disable-automock"); - - test('original implementation', function () { - // now we have the original implementation, - // even if we set the automocking in a jest configuration - expect(disable_automock_1["default"]()).toBe('hi!'); - }); - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJtYXBwaW5ncyI6Ijs7QUFFQSxjQUFLLGVBQUw7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBRkEsSUFBQSxrQkFBQSxHQUFBLE9BQUEsQ0FBQSxvQkFBQSxDQUFBOztBQUlBLElBQUksQ0FBQyx5QkFBRCxFQUE0QixZQUFBO0FBQzlCO0FBQ0E7QUFDQSxFQUFBLE1BQU0sQ0FBQyxrQkFBQSxXQUFBLEVBQUQsQ0FBTixDQUFnQixJQUFoQixDQUFxQixLQUFyQjtBQUNELENBSkcsQ0FBSiIsIm5hbWVzIjpbXSwic291cmNlcyI6WyI8Y3dkPi9kaXNhYmxlLWF1dG9tb2NrLnNwZWMudHMiXSwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IGhlbGxvIGZyb20gJy4vZGlzYWJsZS1hdXRvbW9jaydcblxuamVzdC5kaXNhYmxlQXV0b21vY2soKVxuXG50ZXN0KCdvcmlnaW5hbCBpbXBsZW1lbnRhdGlvbicsICgpID0+IHtcbiAgLy8gbm93IHdlIGhhdmUgdGhlIG9yaWdpbmFsIGltcGxlbWVudGF0aW9uLFxuICAvLyBldmVuIGlmIHdlIHNldCB0aGUgYXV0b21vY2tpbmcgaW4gYSBqZXN0IGNvbmZpZ3VyYXRpb25cbiAgZXhwZWN0KGhlbGxvKCkpLnRvQmUoJ2hpIScpXG59KVxuIl0sInZlcnNpb24iOjN9 - ===[ INLINE SOURCE MAPS ]======================================================= - mappings: >- - ;;AAEA,cAAK,eAAL;;;;;;;;;;;;;;;;;AAFA,IAAA,kBAAA,GAAA,OAAA,CAAA,oBAAA,CAAA;;AAIA,IAAI,CAAC,yBAAD,EAA4B,YAAA;AAC9B;AACA;AACA,EAAA,MAAM,CAAC,kBAAA,WAAA,EAAD,CAAN,CAAgB,IAAhB,CAAqB,KAArB;AACD,CAJG,CAAJ - names: [] - sources: - - /disable-automock.spec.ts - sourcesContent: - - | - import hello from './disable-automock' - - jest.disableAutomock() - - test('original implementation', () => { - // now we have the original implementation, - // even if we set the automocking in a jest configuration - expect(hello()).toBe('hi!') - }) - version: 3 - ================================================================================ -`; - -exports[`Hoisting jest.disableAutomock() should pass using template "with-babel-7": output-disableAutomock 1`] = ` - √ jest --no-cache - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./disable-automock.spec.ts - √ original implementation - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`Hoisting jest.disableAutomock() should pass using template "with-babel-7-string-config": io-disableAutomock 1`] = ` - ===[ FILE: disable-automock.spec.ts ]=========================================== - "use strict"; - - _getJestObj().disableAutomock(); - - function _getJestObj() { - var _require = require("@jest/globals"), - jest = _require.jest; - - _getJestObj = function _getJestObj() { - return jest; - }; - - return jest; - } - - Object.defineProperty(exports, "__esModule", { - value: true - }); - - var disable_automock_1 = require("./disable-automock"); - - test('original implementation', function () { - // now we have the original implementation, - // even if we set the automocking in a jest configuration - expect(disable_automock_1["default"]()).toBe('hi!'); - }); - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJtYXBwaW5ncyI6Ijs7QUFFQSxjQUFLLGVBQUw7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBRkEsSUFBQSxrQkFBQSxHQUFBLE9BQUEsQ0FBQSxvQkFBQSxDQUFBOztBQUlBLElBQUksQ0FBQyx5QkFBRCxFQUE0QixZQUFBO0FBQzlCO0FBQ0E7QUFDQSxFQUFBLE1BQU0sQ0FBQyxrQkFBQSxXQUFBLEVBQUQsQ0FBTixDQUFnQixJQUFoQixDQUFxQixLQUFyQjtBQUNELENBSkcsQ0FBSiIsIm5hbWVzIjpbXSwic291cmNlcyI6WyI8Y3dkPi9kaXNhYmxlLWF1dG9tb2NrLnNwZWMudHMiXSwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IGhlbGxvIGZyb20gJy4vZGlzYWJsZS1hdXRvbW9jaydcblxuamVzdC5kaXNhYmxlQXV0b21vY2soKVxuXG50ZXN0KCdvcmlnaW5hbCBpbXBsZW1lbnRhdGlvbicsICgpID0+IHtcbiAgLy8gbm93IHdlIGhhdmUgdGhlIG9yaWdpbmFsIGltcGxlbWVudGF0aW9uLFxuICAvLyBldmVuIGlmIHdlIHNldCB0aGUgYXV0b21vY2tpbmcgaW4gYSBqZXN0IGNvbmZpZ3VyYXRpb25cbiAgZXhwZWN0KGhlbGxvKCkpLnRvQmUoJ2hpIScpXG59KVxuIl0sInZlcnNpb24iOjN9 - ===[ INLINE SOURCE MAPS ]======================================================= - mappings: >- - ;;AAEA,cAAK,eAAL;;;;;;;;;;;;;;;;;AAFA,IAAA,kBAAA,GAAA,OAAA,CAAA,oBAAA,CAAA;;AAIA,IAAI,CAAC,yBAAD,EAA4B,YAAA;AAC9B;AACA;AACA,EAAA,MAAM,CAAC,kBAAA,WAAA,EAAD,CAAN,CAAgB,IAAhB,CAAqB,KAArB;AACD,CAJG,CAAJ - names: [] - sources: - - /disable-automock.spec.ts - sourcesContent: - - | - import hello from './disable-automock' - - jest.disableAutomock() - - test('original implementation', () => { - // now we have the original implementation, - // even if we set the automocking in a jest configuration - expect(hello()).toBe('hi!') - }) - version: 3 - ================================================================================ -`; - -exports[`Hoisting jest.disableAutomock() should pass using template "with-babel-7-string-config": output-disableAutomock 1`] = ` +exports[`Hoisting should pass using template "with-babel-7": should pass using template "with-babel-7" 1`] = ` √ jest --no-cache ↳ exit code: 0 ===[ STDOUT ]=================================================================== ===[ STDERR ]=================================================================== - PASS ./disable-automock.spec.ts - √ original implementation + PASS ./general-hoisting.spec.ts + PASS ./import-jest.spec.ts - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total + Test Suites: 2 passed, 2 total + Tests: 5 passed, 5 total Snapshots: 0 total Time: XXs Ran all test suites. ================================================================================ `; -exports[`Hoisting jest.enableAutomock() should pass using template "default": io-enableAutomock 1`] = ` - ===[ FILE: enable-automock.spec.ts ]============================================ - "use strict"; - Object.defineProperty(exports, "__esModule", { value: true }); - jest.enableAutomock(); - var enable_automock_1 = require("./enable-automock"); - test('original implementation', function () { - // now we have the mocked implementation, - // @ts-expect-error - expect(enable_automock_1.default._isMockFunction).toBeTruthy(); - }); - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiPGN3ZD4vZW5hYmxlLWF1dG9tb2NrLnNwZWMudHMiLCJtYXBwaW5ncyI6Ijs7QUFBQSxJQUFJLENBQUMsY0FBYyxFQUFFLENBQUE7QUFFckIscURBQXFDO0FBRXJDLElBQUksQ0FBQyx5QkFBeUIsRUFBRTtJQUM5Qix5Q0FBeUM7SUFDekMsbUJBQW1CO0lBQ25CLE1BQU0sQ0FBQyx5QkFBSyxDQUFDLGVBQWUsQ0FBQyxDQUFDLFVBQVUsRUFBRSxDQUFBO0FBQzVDLENBQUMsQ0FBQyxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbIjxjd2Q+L2VuYWJsZS1hdXRvbW9jay5zcGVjLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImplc3QuZW5hYmxlQXV0b21vY2soKVxuXG5pbXBvcnQgaGVsbG8gZnJvbSAnLi9lbmFibGUtYXV0b21vY2snXG5cbnRlc3QoJ29yaWdpbmFsIGltcGxlbWVudGF0aW9uJywgKCkgPT4ge1xuICAvLyBub3cgd2UgaGF2ZSB0aGUgbW9ja2VkIGltcGxlbWVudGF0aW9uLFxuICAvLyBAdHMtZXhwZWN0LWVycm9yXG4gIGV4cGVjdChoZWxsby5faXNNb2NrRnVuY3Rpb24pLnRvQmVUcnV0aHkoKVxufSlcbiJdLCJ2ZXJzaW9uIjozfQ== - ===[ INLINE SOURCE MAPS ]======================================================= - file: /enable-automock.spec.ts - mappings: >- - ;;AAAA,IAAI,CAAC,cAAc,EAAE,CAAA;AAErB,qDAAqC;AAErC,IAAI,CAAC,yBAAyB,EAAE;IAC9B,yCAAyC;IACzC,mBAAmB;IACnB,MAAM,CAAC,yBAAK,CAAC,eAAe,CAAC,CAAC,UAAU,EAAE,CAAA;AAC5C,CAAC,CAAC,CAAA - names: [] - sources: - - /enable-automock.spec.ts - sourcesContent: - - | - jest.enableAutomock() - - import hello from './enable-automock' - - test('original implementation', () => { - // now we have the mocked implementation, - // @ts-expect-error - expect(hello._isMockFunction).toBeTruthy() - }) - version: 3 - ================================================================================ -`; - -exports[`Hoisting jest.enableAutomock() should pass using template "default": output-enableAutomock 1`] = ` - √ jest --no-cache - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./enable-automock.spec.ts - √ original implementation - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`Hoisting jest.enableAutomock() should pass using template "with-babel-7": io-enableAutomock 1`] = ` - ===[ FILE: enable-automock.spec.ts ]============================================ - "use strict"; - - _getJestObj().enableAutomock(); - - function _getJestObj() { - var _require = require("@jest/globals"), - jest = _require.jest; - - _getJestObj = function _getJestObj() { - return jest; - }; - - return jest; - } - - Object.defineProperty(exports, "__esModule", { - value: true - }); - - var enable_automock_1 = require("./enable-automock"); - - test('original implementation', function () { - // now we have the mocked implementation, - // @ts-expect-error - expect(enable_automock_1["default"]._isMockFunction).toBeTruthy(); - }); - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJtYXBwaW5ncyI6Ijs7QUFBQSxjQUFLLGNBQUw7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBRUEsSUFBQSxpQkFBQSxHQUFBLE9BQUEsQ0FBQSxtQkFBQSxDQUFBOztBQUVBLElBQUksQ0FBQyx5QkFBRCxFQUE0QixZQUFBO0FBQzlCO0FBQ0E7QUFDQSxFQUFBLE1BQU0sQ0FBQyxpQkFBQSxXQUFBLENBQU0sZUFBUCxDQUFOLENBQThCLFVBQTlCO0FBQ0QsQ0FKRyxDQUFKIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbIjxjd2Q+L2VuYWJsZS1hdXRvbW9jay5zcGVjLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImplc3QuZW5hYmxlQXV0b21vY2soKVxuXG5pbXBvcnQgaGVsbG8gZnJvbSAnLi9lbmFibGUtYXV0b21vY2snXG5cbnRlc3QoJ29yaWdpbmFsIGltcGxlbWVudGF0aW9uJywgKCkgPT4ge1xuICAvLyBub3cgd2UgaGF2ZSB0aGUgbW9ja2VkIGltcGxlbWVudGF0aW9uLFxuICAvLyBAdHMtZXhwZWN0LWVycm9yXG4gIGV4cGVjdChoZWxsby5faXNNb2NrRnVuY3Rpb24pLnRvQmVUcnV0aHkoKVxufSlcbiJdLCJ2ZXJzaW9uIjozfQ== - ===[ INLINE SOURCE MAPS ]======================================================= - mappings: >- - ;;AAAA,cAAK,cAAL;;;;;;;;;;;;;;;;;AAEA,IAAA,iBAAA,GAAA,OAAA,CAAA,mBAAA,CAAA;;AAEA,IAAI,CAAC,yBAAD,EAA4B,YAAA;AAC9B;AACA;AACA,EAAA,MAAM,CAAC,iBAAA,WAAA,CAAM,eAAP,CAAN,CAA8B,UAA9B;AACD,CAJG,CAAJ - names: [] - sources: - - /enable-automock.spec.ts - sourcesContent: - - | - jest.enableAutomock() - - import hello from './enable-automock' - - test('original implementation', () => { - // now we have the mocked implementation, - // @ts-expect-error - expect(hello._isMockFunction).toBeTruthy() - }) - version: 3 - ================================================================================ -`; - -exports[`Hoisting jest.enableAutomock() should pass using template "with-babel-7": output-enableAutomock 1`] = ` - √ jest --no-cache - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./enable-automock.spec.ts - √ original implementation - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`Hoisting jest.enableAutomock() should pass using template "with-babel-7-string-config": io-enableAutomock 1`] = ` - ===[ FILE: enable-automock.spec.ts ]============================================ - "use strict"; - - _getJestObj().enableAutomock(); - - function _getJestObj() { - var _require = require("@jest/globals"), - jest = _require.jest; - - _getJestObj = function _getJestObj() { - return jest; - }; - - return jest; - } - - Object.defineProperty(exports, "__esModule", { - value: true - }); - - var enable_automock_1 = require("./enable-automock"); - - test('original implementation', function () { - // now we have the mocked implementation, - // @ts-expect-error - expect(enable_automock_1["default"]._isMockFunction).toBeTruthy(); - }); - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJtYXBwaW5ncyI6Ijs7QUFBQSxjQUFLLGNBQUw7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBRUEsSUFBQSxpQkFBQSxHQUFBLE9BQUEsQ0FBQSxtQkFBQSxDQUFBOztBQUVBLElBQUksQ0FBQyx5QkFBRCxFQUE0QixZQUFBO0FBQzlCO0FBQ0E7QUFDQSxFQUFBLE1BQU0sQ0FBQyxpQkFBQSxXQUFBLENBQU0sZUFBUCxDQUFOLENBQThCLFVBQTlCO0FBQ0QsQ0FKRyxDQUFKIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbIjxjd2Q+L2VuYWJsZS1hdXRvbW9jay5zcGVjLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImplc3QuZW5hYmxlQXV0b21vY2soKVxuXG5pbXBvcnQgaGVsbG8gZnJvbSAnLi9lbmFibGUtYXV0b21vY2snXG5cbnRlc3QoJ29yaWdpbmFsIGltcGxlbWVudGF0aW9uJywgKCkgPT4ge1xuICAvLyBub3cgd2UgaGF2ZSB0aGUgbW9ja2VkIGltcGxlbWVudGF0aW9uLFxuICAvLyBAdHMtZXhwZWN0LWVycm9yXG4gIGV4cGVjdChoZWxsby5faXNNb2NrRnVuY3Rpb24pLnRvQmVUcnV0aHkoKVxufSlcbiJdLCJ2ZXJzaW9uIjozfQ== - ===[ INLINE SOURCE MAPS ]======================================================= - mappings: >- - ;;AAAA,cAAK,cAAL;;;;;;;;;;;;;;;;;AAEA,IAAA,iBAAA,GAAA,OAAA,CAAA,mBAAA,CAAA;;AAEA,IAAI,CAAC,yBAAD,EAA4B,YAAA;AAC9B;AACA;AACA,EAAA,MAAM,CAAC,iBAAA,WAAA,CAAM,eAAP,CAAN,CAA8B,UAA9B;AACD,CAJG,CAAJ - names: [] - sources: - - /enable-automock.spec.ts - sourcesContent: - - | - jest.enableAutomock() - - import hello from './enable-automock' - - test('original implementation', () => { - // now we have the mocked implementation, - // @ts-expect-error - expect(hello._isMockFunction).toBeTruthy() - }) - version: 3 - ================================================================================ -`; - -exports[`Hoisting jest.enableAutomock() should pass using template "with-babel-7-string-config": output-enableAutomock 1`] = ` - √ jest --no-cache - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./enable-automock.spec.ts - √ original implementation - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`Hoisting jest.mock() & jest.unmock() should pass using template "default": io-mockUnmock 1`] = ` - ===[ FILE: mock-unmock.spec.ts ]================================================ - "use strict"; - Object.defineProperty(exports, "__esModule", { value: true }); - jest.mock('./mock-unmock'); - var mock_unmock_1 = require("./mock-unmock"); - var original = jest.requireActual('./mock-unmock').default; - it('should have been mocked', function () { - jest.mock('foo', function () { return 'bar'; }, { virtual: true }); - var msg = mock_unmock_1.default(); - expect(mock_unmock_1.default).not.toBe(original); - expect(msg).toBeUndefined(); - expect(mock_unmock_1.default).toHaveProperty('mock'); - expect(require('foo')).toBe('bar'); - }); - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiPGN3ZD4vbW9jay11bm1vY2suc3BlYy50cyIsIm1hcHBpbmdzIjoiOztBQUVBLElBQUksQ0FBQyxJQUFJLENBQUMsZUFBZSxDQUFDLENBQUE7QUFGMUIsNkNBQWlDO0FBSWpDLElBQU0sUUFBUSxHQUFHLElBQUksQ0FBQyxhQUFhLENBQUMsZUFBZSxDQUFDLENBQUMsT0FBTyxDQUFBO0FBQzVELEVBQUUsQ0FBQyx5QkFBeUIsRUFBRTtJQU01QixJQUFJLENBQUMsSUFBSSxDQUFDLEtBQUssRUFBRSxjQUFNLE9BQUEsS0FBSyxFQUFMLENBQUssRUFBRSxFQUFFLE9BQU8sRUFBRSxJQUFJLEVBQUUsQ0FBQyxDQUFBO0lBTGhELElBQU0sR0FBRyxHQUFHLHFCQUFLLEVBQUUsQ0FBQTtJQUNuQixNQUFNLENBQUMscUJBQUssQ0FBQyxDQUFDLEdBQUcsQ0FBQyxJQUFJLENBQUMsUUFBUSxDQUFDLENBQUE7SUFDaEMsTUFBTSxDQUFDLEdBQUcsQ0FBQyxDQUFDLGFBQWEsRUFBRSxDQUFBO0lBQzNCLE1BQU0sQ0FBQyxxQkFBSyxDQUFDLENBQUMsY0FBYyxDQUFDLE1BQU0sQ0FBQyxDQUFBO0lBQ3BDLE1BQU0sQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUMsQ0FBQyxJQUFJLENBQUMsS0FBSyxDQUFDLENBQUE7Q0FFbkMsQ0FBQyxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbIjxjd2Q+L21vY2stdW5tb2NrLnNwZWMudHMiXSwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IGhlbGxvIGZyb20gJy4vbW9jay11bm1vY2snXG5cbmplc3QubW9jaygnLi9tb2NrLXVubW9jaycpXG5cbmNvbnN0IG9yaWdpbmFsID0gamVzdC5yZXF1aXJlQWN0dWFsKCcuL21vY2stdW5tb2NrJykuZGVmYXVsdFxuaXQoJ3Nob3VsZCBoYXZlIGJlZW4gbW9ja2VkJywgKCkgPT4ge1xuICBjb25zdCBtc2cgPSBoZWxsbygpXG4gIGV4cGVjdChoZWxsbykubm90LnRvQmUob3JpZ2luYWwpXG4gIGV4cGVjdChtc2cpLnRvQmVVbmRlZmluZWQoKVxuICBleHBlY3QoaGVsbG8pLnRvSGF2ZVByb3BlcnR5KCdtb2NrJylcbiAgZXhwZWN0KHJlcXVpcmUoJ2ZvbycpKS50b0JlKCdiYXInKVxuICBqZXN0Lm1vY2soJ2ZvbycsICgpID0+ICdiYXInLCB7IHZpcnR1YWw6IHRydWUgfSlcbn0pXG4iXSwidmVyc2lvbiI6M30= - ===[ INLINE SOURCE MAPS ]======================================================= - file: /mock-unmock.spec.ts - mappings: >- - ;;AAEA,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,CAAA;AAF1B,6CAAiC;AAIjC,IAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC,OAAO,CAAA;AAC5D,EAAE,CAAC,yBAAyB,EAAE;IAM5B,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,cAAM,OAAA,KAAK,EAAL,CAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAA;IALhD,IAAM,GAAG,GAAG,qBAAK,EAAE,CAAA;IACnB,MAAM,CAAC,qBAAK,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IAChC,MAAM,CAAC,GAAG,CAAC,CAAC,aAAa,EAAE,CAAA;IAC3B,MAAM,CAAC,qBAAK,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,CAAA;IACpC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;CAEnC,CAAC,CAAA - names: [] - sources: - - /mock-unmock.spec.ts - sourcesContent: - - | - import hello from './mock-unmock' - - jest.mock('./mock-unmock') - - const original = jest.requireActual('./mock-unmock').default - it('should have been mocked', () => { - const msg = hello() - expect(hello).not.toBe(original) - expect(msg).toBeUndefined() - expect(hello).toHaveProperty('mock') - expect(require('foo')).toBe('bar') - jest.mock('foo', () => 'bar', { virtual: true }) - }) - version: 3 - ================================================================================ -`; - -exports[`Hoisting jest.mock() & jest.unmock() should pass using template "default": output-mockUnmock 1`] = ` - √ jest --no-cache - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./mock-unmock.spec.ts - √ should have been mocked - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-7": io-mockUnmock 1`] = ` - ===[ FILE: mock-unmock.spec.ts ]================================================ - "use strict"; - - _getJestObj().mock('./mock-unmock'); - - function _getJestObj() { - var _require = require("@jest/globals"), - jest = _require.jest; - - _getJestObj = function _getJestObj() { - return jest; - }; - - return jest; - } - - Object.defineProperty(exports, "__esModule", { - value: true - }); - - var mock_unmock_1 = require("./mock-unmock"); - - var original = jest.requireActual('./mock-unmock')["default"]; - it('should have been mocked', function () { - _getJestObj().mock('foo', function () { - return 'bar'; - }, { - virtual: true - }); - - var msg = mock_unmock_1["default"](); - expect(mock_unmock_1["default"]).not.toBe(original); - expect(msg).toBeUndefined(); - expect(mock_unmock_1["default"]).toHaveProperty('mock'); - expect(require('foo')).toBe('bar'); - }); - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJtYXBwaW5ncyI6Ijs7QUFFQSxjQUFLLElBQUwsQ0FBVSxlQUFWOzs7Ozs7Ozs7Ozs7Ozs7OztBQUZBLElBQUEsYUFBQSxHQUFBLE9BQUEsQ0FBQSxlQUFBLENBQUE7O0FBSUEsSUFBTSxRQUFRLEdBQUcsSUFBSSxDQUFDLGFBQUwsQ0FBbUIsZUFBbkIsWUFBakI7QUFDQSxFQUFFLENBQUMseUJBQUQsRUFBNEIsWUFBQTtBQU01QixnQkFBSyxJQUFMLENBQVUsS0FBVixFQUFpQixZQUFBO0FBQU0sV0FBQSxLQUFBO0FBQUssR0FBNUIsRUFBOEI7QUFBRSxJQUFBLE9BQU8sRUFBRTtBQUFYLEdBQTlCOztBQUxBLE1BQU0sR0FBRyxHQUFHLGFBQUEsV0FBQSxFQUFaO0FBQ0EsRUFBQSxNQUFNLENBQUMsYUFBQSxXQUFELENBQU4sQ0FBYyxHQUFkLENBQWtCLElBQWxCLENBQXVCLFFBQXZCO0FBQ0EsRUFBQSxNQUFNLENBQUMsR0FBRCxDQUFOLENBQVksYUFBWjtBQUNBLEVBQUEsTUFBTSxDQUFDLGFBQUEsV0FBRCxDQUFOLENBQWMsY0FBZCxDQUE2QixNQUE3QjtBQUNBLEVBQUEsTUFBTSxDQUFDLE9BQU8sQ0FBQyxLQUFELENBQVIsQ0FBTixDQUF1QixJQUF2QixDQUE0QixLQUE1QjtBQUVELENBUEMsQ0FBRiIsIm5hbWVzIjpbXSwic291cmNlcyI6WyI8Y3dkPi9tb2NrLXVubW9jay5zcGVjLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBoZWxsbyBmcm9tICcuL21vY2stdW5tb2NrJ1xuXG5qZXN0Lm1vY2soJy4vbW9jay11bm1vY2snKVxuXG5jb25zdCBvcmlnaW5hbCA9IGplc3QucmVxdWlyZUFjdHVhbCgnLi9tb2NrLXVubW9jaycpLmRlZmF1bHRcbml0KCdzaG91bGQgaGF2ZSBiZWVuIG1vY2tlZCcsICgpID0+IHtcbiAgY29uc3QgbXNnID0gaGVsbG8oKVxuICBleHBlY3QoaGVsbG8pLm5vdC50b0JlKG9yaWdpbmFsKVxuICBleHBlY3QobXNnKS50b0JlVW5kZWZpbmVkKClcbiAgZXhwZWN0KGhlbGxvKS50b0hhdmVQcm9wZXJ0eSgnbW9jaycpXG4gIGV4cGVjdChyZXF1aXJlKCdmb28nKSkudG9CZSgnYmFyJylcbiAgamVzdC5tb2NrKCdmb28nLCAoKSA9PiAnYmFyJywgeyB2aXJ0dWFsOiB0cnVlIH0pXG59KVxuIl0sInZlcnNpb24iOjN9 - ===[ INLINE SOURCE MAPS ]======================================================= - mappings: >- - ;;AAEA,cAAK,IAAL,CAAU,eAAV;;;;;;;;;;;;;;;;;AAFA,IAAA,aAAA,GAAA,OAAA,CAAA,eAAA,CAAA;;AAIA,IAAM,QAAQ,GAAG,IAAI,CAAC,aAAL,CAAmB,eAAnB,YAAjB;AACA,EAAE,CAAC,yBAAD,EAA4B,YAAA;AAM5B,gBAAK,IAAL,CAAU,KAAV,EAAiB,YAAA;AAAM,WAAA,KAAA;AAAK,GAA5B,EAA8B;AAAE,IAAA,OAAO,EAAE;AAAX,GAA9B;;AALA,MAAM,GAAG,GAAG,aAAA,WAAA,EAAZ;AACA,EAAA,MAAM,CAAC,aAAA,WAAD,CAAN,CAAc,GAAd,CAAkB,IAAlB,CAAuB,QAAvB;AACA,EAAA,MAAM,CAAC,GAAD,CAAN,CAAY,aAAZ;AACA,EAAA,MAAM,CAAC,aAAA,WAAD,CAAN,CAAc,cAAd,CAA6B,MAA7B;AACA,EAAA,MAAM,CAAC,OAAO,CAAC,KAAD,CAAR,CAAN,CAAuB,IAAvB,CAA4B,KAA5B;AAED,CAPC,CAAF - names: [] - sources: - - /mock-unmock.spec.ts - sourcesContent: - - | - import hello from './mock-unmock' - - jest.mock('./mock-unmock') - - const original = jest.requireActual('./mock-unmock').default - it('should have been mocked', () => { - const msg = hello() - expect(hello).not.toBe(original) - expect(msg).toBeUndefined() - expect(hello).toHaveProperty('mock') - expect(require('foo')).toBe('bar') - jest.mock('foo', () => 'bar', { virtual: true }) - }) - version: 3 - ================================================================================ -`; - -exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-7": output-mockUnmock 1`] = ` - √ jest --no-cache - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./mock-unmock.spec.ts - √ should have been mocked - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-7-string-config": io-mockUnmock 1`] = ` - ===[ FILE: mock-unmock.spec.ts ]================================================ - "use strict"; - - _getJestObj().mock('./mock-unmock'); - - function _getJestObj() { - var _require = require("@jest/globals"), - jest = _require.jest; - - _getJestObj = function _getJestObj() { - return jest; - }; - - return jest; - } - - Object.defineProperty(exports, "__esModule", { - value: true - }); - - var mock_unmock_1 = require("./mock-unmock"); - - var original = jest.requireActual('./mock-unmock')["default"]; - it('should have been mocked', function () { - _getJestObj().mock('foo', function () { - return 'bar'; - }, { - virtual: true - }); - - var msg = mock_unmock_1["default"](); - expect(mock_unmock_1["default"]).not.toBe(original); - expect(msg).toBeUndefined(); - expect(mock_unmock_1["default"]).toHaveProperty('mock'); - expect(require('foo')).toBe('bar'); - }); - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJtYXBwaW5ncyI6Ijs7QUFFQSxjQUFLLElBQUwsQ0FBVSxlQUFWOzs7Ozs7Ozs7Ozs7Ozs7OztBQUZBLElBQUEsYUFBQSxHQUFBLE9BQUEsQ0FBQSxlQUFBLENBQUE7O0FBSUEsSUFBTSxRQUFRLEdBQUcsSUFBSSxDQUFDLGFBQUwsQ0FBbUIsZUFBbkIsWUFBakI7QUFDQSxFQUFFLENBQUMseUJBQUQsRUFBNEIsWUFBQTtBQU01QixnQkFBSyxJQUFMLENBQVUsS0FBVixFQUFpQixZQUFBO0FBQU0sV0FBQSxLQUFBO0FBQUssR0FBNUIsRUFBOEI7QUFBRSxJQUFBLE9BQU8sRUFBRTtBQUFYLEdBQTlCOztBQUxBLE1BQU0sR0FBRyxHQUFHLGFBQUEsV0FBQSxFQUFaO0FBQ0EsRUFBQSxNQUFNLENBQUMsYUFBQSxXQUFELENBQU4sQ0FBYyxHQUFkLENBQWtCLElBQWxCLENBQXVCLFFBQXZCO0FBQ0EsRUFBQSxNQUFNLENBQUMsR0FBRCxDQUFOLENBQVksYUFBWjtBQUNBLEVBQUEsTUFBTSxDQUFDLGFBQUEsV0FBRCxDQUFOLENBQWMsY0FBZCxDQUE2QixNQUE3QjtBQUNBLEVBQUEsTUFBTSxDQUFDLE9BQU8sQ0FBQyxLQUFELENBQVIsQ0FBTixDQUF1QixJQUF2QixDQUE0QixLQUE1QjtBQUVELENBUEMsQ0FBRiIsIm5hbWVzIjpbXSwic291cmNlcyI6WyI8Y3dkPi9tb2NrLXVubW9jay5zcGVjLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBoZWxsbyBmcm9tICcuL21vY2stdW5tb2NrJ1xuXG5qZXN0Lm1vY2soJy4vbW9jay11bm1vY2snKVxuXG5jb25zdCBvcmlnaW5hbCA9IGplc3QucmVxdWlyZUFjdHVhbCgnLi9tb2NrLXVubW9jaycpLmRlZmF1bHRcbml0KCdzaG91bGQgaGF2ZSBiZWVuIG1vY2tlZCcsICgpID0+IHtcbiAgY29uc3QgbXNnID0gaGVsbG8oKVxuICBleHBlY3QoaGVsbG8pLm5vdC50b0JlKG9yaWdpbmFsKVxuICBleHBlY3QobXNnKS50b0JlVW5kZWZpbmVkKClcbiAgZXhwZWN0KGhlbGxvKS50b0hhdmVQcm9wZXJ0eSgnbW9jaycpXG4gIGV4cGVjdChyZXF1aXJlKCdmb28nKSkudG9CZSgnYmFyJylcbiAgamVzdC5tb2NrKCdmb28nLCAoKSA9PiAnYmFyJywgeyB2aXJ0dWFsOiB0cnVlIH0pXG59KVxuIl0sInZlcnNpb24iOjN9 - ===[ INLINE SOURCE MAPS ]======================================================= - mappings: >- - ;;AAEA,cAAK,IAAL,CAAU,eAAV;;;;;;;;;;;;;;;;;AAFA,IAAA,aAAA,GAAA,OAAA,CAAA,eAAA,CAAA;;AAIA,IAAM,QAAQ,GAAG,IAAI,CAAC,aAAL,CAAmB,eAAnB,YAAjB;AACA,EAAE,CAAC,yBAAD,EAA4B,YAAA;AAM5B,gBAAK,IAAL,CAAU,KAAV,EAAiB,YAAA;AAAM,WAAA,KAAA;AAAK,GAA5B,EAA8B;AAAE,IAAA,OAAO,EAAE;AAAX,GAA9B;;AALA,MAAM,GAAG,GAAG,aAAA,WAAA,EAAZ;AACA,EAAA,MAAM,CAAC,aAAA,WAAD,CAAN,CAAc,GAAd,CAAkB,IAAlB,CAAuB,QAAvB;AACA,EAAA,MAAM,CAAC,GAAD,CAAN,CAAY,aAAZ;AACA,EAAA,MAAM,CAAC,aAAA,WAAD,CAAN,CAAc,cAAd,CAA6B,MAA7B;AACA,EAAA,MAAM,CAAC,OAAO,CAAC,KAAD,CAAR,CAAN,CAAuB,IAAvB,CAA4B,KAA5B;AAED,CAPC,CAAF - names: [] - sources: - - /mock-unmock.spec.ts - sourcesContent: - - | - import hello from './mock-unmock' - - jest.mock('./mock-unmock') - - const original = jest.requireActual('./mock-unmock').default - it('should have been mocked', () => { - const msg = hello() - expect(hello).not.toBe(original) - expect(msg).toBeUndefined() - expect(hello).toHaveProperty('mock') - expect(require('foo')).toBe('bar') - jest.mock('foo', () => 'bar', { virtual: true }) - }) - version: 3 - ================================================================================ -`; - -exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-7-string-config": output-mockUnmock 1`] = ` +exports[`Hoisting should pass using template "with-babel-7-string-config": should pass using template "with-babel-7-string-config" 1`] = ` √ jest --no-cache ↳ exit code: 0 ===[ STDOUT ]=================================================================== ===[ STDERR ]=================================================================== - PASS ./mock-unmock.spec.ts - √ should have been mocked + PASS ./general-hoisting.spec.ts + PASS ./import-jest.spec.ts - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total + Test Suites: 2 passed, 2 total + Tests: 5 passed, 5 total Snapshots: 0 total Time: XXs Ran all test suites. diff --git a/e2e/__tests__/hoisting.test.ts b/e2e/__tests__/hoisting.test.ts index 71433eae90..85a8523387 100644 --- a/e2e/__tests__/hoisting.test.ts +++ b/e2e/__tests__/hoisting.test.ts @@ -2,49 +2,18 @@ import { allValidPackageSets } from '../__helpers__/templates' import { configureTestCase } from '../__helpers__/test-case' describe('Hoisting', () => { - describe('jest.mock() & jest.unmock()', () => { - const testCase = configureTestCase('hoisting/mock-unmock', { - writeIo: true, - }) - - testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => { - it(testLabel, () => { - const result = runTest() - expect(result.status).toBe(0) - expect(result).toMatchSnapshot('output-mockUnmock') - expect(result.ioFor('mock-unmock.spec.ts')).toMatchSnapshot('io-mockUnmock') - }) - }) + const testCase = configureTestCase('hoisting', { + writeIo: true, + jestConfig: { + automock: true, + }, }) - describe('jest.enableAutomock()', () => { - const testCase = configureTestCase('hoisting/enable-automock', { writeIo: true }) - - testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => { - it(testLabel, () => { - const result = runTest() - expect(result.status).toBe(0) - expect(result).toMatchSnapshot('output-enableAutomock') - expect(result.ioFor('enable-automock.spec.ts')).toMatchSnapshot('io-enableAutomock') - }) - }) - }) - - describe('jest.disableAutomock()', () => { - const testCase = configureTestCase('hoisting/disable-automock', { - writeIo: true, - jestConfig: { - automock: true, - } - }) - - testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => { - it(testLabel, () => { - const result = runTest() - expect(result.status).toBe(0) - expect(result).toMatchSnapshot('output-disableAutomock') - expect(result.ioFor('disable-automock.spec.ts')).toMatchSnapshot('io-disableAutomock') - }) + testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => { + it(testLabel, () => { + const result = runTest() + expect(result.status).toBe(0) + expect(result).toMatchSnapshot(testLabel) }) }) }) diff --git a/src/config/__snapshots__/config-set.spec.ts.snap b/src/config/__snapshots__/config-set.spec.ts.snap index f800485265..0c6601dde1 100644 --- a/src/config/__snapshots__/config-set.spec.ts.snap +++ b/src/config/__snapshots__/config-set.spec.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`cacheKey should be a string 1`] = `"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"transformers\\":[\\"hoisting-jest-mock@1\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":{},\\"tsConfig\\":{\\"kind\\":\\"file\\",\\"value\\":\\"\\"}},\\"tsconfig\\":{\\"options\\":{\\"configFilePath\\":\\"\\",\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1,\\"types\\":[]},\\"raw\\":{\\"compileOnSave\\":false,\\"compilerOptions\\":{\\"composite\\":true,\\"declaration\\":true,\\"types\\":[]},\\"exclude\\":[\\"foo/**/*\\"],\\"include\\":[\\"bar/**/*\\"]}}}"`; +exports[`cacheKey should be a string 1`] = `"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"transformers\\":[\\"hoisting-jest-mock@2\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":{},\\"tsConfig\\":{\\"kind\\":\\"file\\",\\"value\\":\\"\\"}},\\"tsconfig\\":{\\"options\\":{\\"configFilePath\\":\\"\\",\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1,\\"types\\":[]},\\"raw\\":{\\"compileOnSave\\":false,\\"compilerOptions\\":{\\"composite\\":true,\\"declaration\\":true,\\"types\\":[]},\\"exclude\\":[\\"foo/**/*\\"],\\"include\\":[\\"bar/**/*\\"]}}}"`; exports[`isTestFile should return a boolean value whether the file matches test pattern 1`] = `true`; @@ -21,7 +21,7 @@ Object { "name": undefined, }, "transformers": Array [ - "hoisting-jest-mock@1", + "hoisting-jest-mock@2", ], "tsJest": Object { "babelConfig": undefined, diff --git a/src/transformers/__snapshots__/hoist-jest.spec.ts.snap b/src/transformers/__snapshots__/hoist-jest.spec.ts.snap new file mode 100644 index 0000000000..2042de6e2a --- /dev/null +++ b/src/transformers/__snapshots__/hoist-jest.spec.ts.snap @@ -0,0 +1,49 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`hoisting should hoist correctly jest methods 1`] = ` +"\\"use strict\\"; +Object.defineProperty(exports, \\"__esModule\\", { value: true }); +// These will all be hoisted above imports +jest.deepUnmock('./__test_modules__/Unmocked'); +jest.unmock('./__test_modules__/c').unmock('./__test_modules__/d'); +jest.unmock('./__test_modules__/' + 'a'); +var Unmocked_1 = require(\\"./__test_modules__/Unmocked\\"); +var a_1 = require(\\"./__test_modules__/a\\"); +var b_1 = require(\\"./__test_modules__/b\\"); +var c_1 = require(\\"./__test_modules__/c\\"); +var d_1 = require(\\"./__test_modules__/d\\"); +// These will not be hoisted +jest.unmock('./__test_modules__/a').dontMock('./__test_modules__/b'); +console.log(Unmocked_1.default); +console.log(a_1.default); +console.log(b_1.default); +console.log(c_1.default); +console.log(d_1.default); +" +`; + +exports[`hoisting should hoist correctly jest methods 2`] = ` +"\\"use strict\\"; +Object.defineProperty(exports, \\"__esModule\\", { value: true }); +var globals_1 = require(\\"@jest/globals\\"); +var globals_2 = require(\\"@jest/globals\\"); +var JestGlobals = require(\\"@jest/globals\\"); +// These will be hoisted above imports +globals_1.jest.unmock('../__test_modules__/a'); +var a_1 = require(\\"../__test_modules__/a\\"); +var b_1 = require(\\"../__test_modules__/b\\"); +var c_1 = require(\\"../__test_modules__/c\\"); +var d_1 = require(\\"../__test_modules__/d\\"); +globals_2.jest.unmock('../__test_modules__/b'); +JestGlobals.jest.unmock('../__test_modules__/c'); +// These will not be hoisted above imports +{ + jest_1.unmock('../__test_modules__/d'); + var jest_1 = { unmock: function () { } }; +} +console.log(a_1.default); +console.log(b_1.default); +console.log(c_1.default); +console.log(d_1.default); +" +`; diff --git a/src/transformers/hoist-jest.spec.ts b/src/transformers/hoist-jest.spec.ts index 741352cbf7..7f5a9c9bc8 100644 --- a/src/transformers/hoist-jest.spec.ts +++ b/src/transformers/hoist-jest.spec.ts @@ -3,39 +3,57 @@ import * as tsc from 'typescript' import * as hoist from './hoist-jest' -const CODE_WITH_HOISTING = ` -const foo = 'foo' -console.log(foo) -jest.enableAutomock() -jest.disableAutomock() -jest.mock('./foo') -jest.mock('./foo/bar', () => 'bar') -jest.unmock('./bar/foo').dontMock('./bar/bar') -jest.deepUnmock('./foo') -jest.mock('./foo').mock('./bar') -const func = () => { - const bar = 'bar' - console.log(bar) - jest.unmock('./foo') - jest.mock('./bar') - jest.mock('./bar/foo', () => 'foo') - jest.unmock('./foo/bar') - jest.unmock('./bar/foo').dontMock('./bar/bar') - jest.deepUnmock('./bar') - jest.mock('./foo').mock('./bar') -} -const func2 = () => { - const bar = 'bar' - console.log(bar) - jest.mock('./bar') - jest.unmock('./foo/bar') - jest.mock('./bar/foo', () => 'foo') - jest.unmock('./foo') - jest.unmock('./bar/foo').dontMock('./bar/bar') - jest.deepUnmock('./bar') - jest.mock('./foo').mock('./bar') -} +const CODE_WITH_HOISTING_NO_JEST_GLOBALS = ` +import Unmocked from './__test_modules__/Unmocked' +import a from './__test_modules__/a' +import b from './__test_modules__/b' +import c from './__test_modules__/c' +import d from './__test_modules__/d' + +// These will all be hoisted above imports +jest.deepUnmock('./__test_modules__/Unmocked') +jest.unmock('./__test_modules__/c').unmock('./__test_modules__/d') + +// These will not be hoisted +jest.unmock('./__test_modules__/a').dontMock('./__test_modules__/b') +jest.unmock('./__test_modules__/' + 'a') + +console.log(Unmocked) +console.log(a) +console.log(b) +console.log(c) +console.log(d) +` +const CODE_WITH_HOISTING_HAS_JEST_GLOBALS = ` + import a from '../__test_modules__/a'; + import b from '../__test_modules__/b'; + + import {jest} from '@jest/globals'; + import {jest as aliasedJest} from '@jest/globals'; + import * as JestGlobals from '@jest/globals'; + + import c from '../__test_modules__/c'; + import d from '../__test_modules__/d'; + + // These will be hoisted above imports + + jest.unmock('../__test_modules__/a'); + aliasedJest.unmock('../__test_modules__/b'); + JestGlobals.jest.unmock('../__test_modules__/c'); + + // These will not be hoisted above imports + + { + const jest = {unmock: () => {}}; + jest.unmock('../__test_modules__/d'); + } + + console.log(a) + console.log(b) + console.log(c) + console.log(d) ` + const logger = testing.createLoggerMock() const createFactory = () => hoist.factory({ logger, compilerModule: tsc } as any) const transpile = (source: string) => tsc.transpileModule(source, { transformers: { before: [createFactory()] } }) @@ -48,41 +66,13 @@ describe('hoisting', () => { expect(typeof hoist.factory).toBe('function') }) - it('should hoist jest.mock(), unmock(), disableAutomock() and enableAutomock()', () => { - const out = transpile(CODE_WITH_HOISTING) - expect(out.outputText).toMatchInlineSnapshot(` - "jest.enableAutomock(); - jest.disableAutomock(); - jest.mock('./foo'); - jest.mock('./foo/bar', function () { return 'bar'; }); - jest.deepUnmock('./foo'); - jest.mock('./foo').mock('./bar'); - var foo = 'foo'; - console.log(foo); - jest.unmock('./bar/foo').dontMock('./bar/bar'); - var func = function () { - jest.unmock('./foo'); - jest.mock('./bar'); - jest.mock('./bar/foo', function () { return 'foo'; }); - jest.unmock('./foo/bar'); - jest.deepUnmock('./bar'); - jest.mock('./foo').mock('./bar'); - var bar = 'bar'; - console.log(bar); - jest.unmock('./bar/foo').dontMock('./bar/bar'); - }; - var func2 = function () { - jest.mock('./bar'); - jest.unmock('./foo/bar'); - jest.mock('./bar/foo', function () { return 'foo'; }); - jest.unmock('./foo'); - jest.deepUnmock('./bar'); - jest.mock('./foo').mock('./bar'); - var bar = 'bar'; - console.log(bar); - jest.unmock('./bar/foo').dontMock('./bar/bar'); - }; - " - `) - }) + // TODO: import alias and import * are not hoisted correctly yet, will need to fix + it.each([CODE_WITH_HOISTING_NO_JEST_GLOBALS, CODE_WITH_HOISTING_HAS_JEST_GLOBALS])( + 'should hoist correctly jest methods', + (data) => { + const out = transpile(data) + + expect(out.outputText).toMatchSnapshot() + }, + ) }) diff --git a/src/transformers/hoist-jest.ts b/src/transformers/hoist-jest.ts index 0839c2bbe5..34e906bba0 100644 --- a/src/transformers/hoist-jest.ts +++ b/src/transformers/hoist-jest.ts @@ -17,7 +17,8 @@ import type { ConfigSet } from '../config/config-set' * What methods of `jest` should we hoist */ const HOIST_METHODS = ['mock', 'unmock', 'enableAutomock', 'disableAutomock', 'deepUnmock'] - +const JEST_GLOBAL_NAME = 'jest' +const JEST_GLOBALS_MODULE_NAME = '@jest/globals' /** * @internal */ @@ -26,12 +27,11 @@ export const name = 'hoisting-jest-mock' /** * @internal */ -export const version = 1 +export const version = 2 /** * The factory of hoisting transformer factory * - * @param cs Current jest configuration-set * @internal */ export function factory(cs: ConfigSet): (ctx: TransformationContext) => Transformer { @@ -47,25 +47,29 @@ export function factory(cs: ConfigSet): (ctx: TransformationContext) => Transfor ts.isCallExpression(expression) && ts.isPropertyAccessExpression(expression.expression) && HOIST_METHODS.includes(expression.expression.name.text) && - ((ts.isIdentifier(expression.expression.expression) && expression.expression.expression.text === 'jest') || + ((ts.isIdentifier(expression.expression.expression) && + expression.expression.expression.text === JEST_GLOBAL_NAME) || shouldHoistExpression(expression.expression.expression)) ) } /** * Checks whether given node is a statement that we need to hoist - * - * @param node The node to test */ function shouldHoistNode(node: Node): node is ExpressionStatement { return ts.isExpressionStatement(node) && shouldHoistExpression(node.expression) } + function isJestGlobalImport(node: Statement): boolean { + return ( + ts.isImportDeclaration(node) && + ts.isStringLiteral(node.moduleSpecifier) && + node.moduleSpecifier.text === JEST_GLOBALS_MODULE_NAME + ) + } + /** * Create a source file visitor which will visit all nodes in a source file - * - * @param ctx The typescript transformation context - * @param sf The owning source file */ function createVisitor(ctx: TransformationContext, _: SourceFile) { /** @@ -104,8 +108,6 @@ export function factory(cs: ConfigSet): (ctx: TransformationContext) => Transfor } /** * Our main visitor, which will be called recursively for each node in the source file's AST - * - * @param node The node to be visited */ const visitor: Visitor = (node) => { // enter this level @@ -113,18 +115,28 @@ export function factory(cs: ConfigSet): (ctx: TransformationContext) => Transfor // visit each child let resultNode = ts.visitEachChild(node, visitor, ctx) - // check if we have something to hoist in this level if (hoisted[level] && hoisted[level].length) { // re-order children so that hoisted ones appear first // this is actually the main job of this transformer const hoistedStmts = hoisted[level] - const otherStmts = (resultNode as Block).statements.filter((s) => !hoistedStmts.includes(s)) + const otherStmts = (resultNode as Block).statements.filter( + (s) => !hoistedStmts.includes(s) && !isJestGlobalImport(s), + ) const newNode = ts.getMutableClone(resultNode) as Block - resultNode = { - ...newNode, - statements: ts.createNodeArray([...hoistedStmts, ...otherStmts]), - } as Statement + const newStatements = [...hoistedStmts, ...otherStmts] + if (level === 1) { + const jestGlobalsImportStmts = (resultNode as Block).statements.filter((s) => isJestGlobalImport(s)) + resultNode = { + ...newNode, + statements: ts.createNodeArray([...jestGlobalsImportStmts, ...newStatements]), + } as Statement + } else { + resultNode = { + ...newNode, + statements: ts.createNodeArray(newStatements), + } as Statement + } } // exit the level