diff --git a/CHANGELOG.md b/CHANGELOG.md index ea759c69356e..6fb9cf4fccb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - `[jest-changed-files]` Change method of obtaining git root ([#8052](https://github.com/facebook/jest/pull/8052)) - `[jest-each]` Fix test function type ([#8145](https://github.com/facebook/jest/pull/8145)) - `[jest-fake-timers]` `getTimerCount` not taking immediates and ticks into account ([#8139](https://github.com/facebook/jest/pull/8139)) +- `[jest-runtime]` Allow json file as manual mock ([#8159](https://github.com/facebook/jest/pull/8159)) - `[pretty-format]` Print `BigInt` as a readable number instead of `{}` ([#8138](https://github.com/facebook/jest/pull/8138)) ### Chore & Maintenance diff --git a/e2e/mock-json/__mocks__/data.json b/e2e/mock-json/__mocks__/data.json new file mode 100644 index 000000000000..c8c4105eb57c --- /dev/null +++ b/e2e/mock-json/__mocks__/data.json @@ -0,0 +1,3 @@ +{ + "foo": "bar" +} diff --git a/e2e/mock-json/__tests__/index.js b/e2e/mock-json/__tests__/index.js new file mode 100644 index 000000000000..5c3534cd7d77 --- /dev/null +++ b/e2e/mock-json/__tests__/index.js @@ -0,0 +1,18 @@ +/** + * 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. + */ + +'use strict'; + +jest.mock('../data.json'); + +const extractData = require('../'); + +describe('extractData', () => { + it('should read the data', () => { + expect(extractData()).toEqual(['foo']); + }); +}); diff --git a/e2e/mock-json/data.json b/e2e/mock-json/data.json new file mode 100644 index 000000000000..5e8275d41838 --- /dev/null +++ b/e2e/mock-json/data.json @@ -0,0 +1,3 @@ +{ + "real": "data" +} diff --git a/e2e/mock-json/index.js b/e2e/mock-json/index.js new file mode 100644 index 000000000000..580b168dce5e --- /dev/null +++ b/e2e/mock-json/index.js @@ -0,0 +1,14 @@ +/** + * 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. + */ + +const data = require('./data.json'); + +function extractData() { + return Object.keys(data); +} + +module.exports = extractData; diff --git a/e2e/mock-json/package.json b/e2e/mock-json/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/e2e/mock-json/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 35df1bf43773..067c247fdaf9 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -341,19 +341,15 @@ class Runtime { loaded: false, }; moduleRegistry[modulePath] = localModule; - if (path.extname(modulePath) === '.json') { - localModule.exports = this._environment.global.JSON.parse( - stripBOM(fs.readFileSync(modulePath, 'utf8')), - ); - } else if (path.extname(modulePath) === '.node') { - localModule.exports = require(modulePath); - } else { - // Only include the fromPath if a moduleName is given. Else treat as root. - const fromPath = moduleName ? from : null; - this._execModule(localModule, options, moduleRegistry, fromPath); - } - localModule.loaded = true; + this._loadModule( + localModule, + from, + moduleName, + modulePath, + options, + moduleRegistry, + ); } return moduleRegistry[modulePath].exports; } @@ -430,11 +426,16 @@ class Runtime { loaded: false, }; - // Only include the fromPath if a moduleName is given. Else treat as root. - const fromPath = moduleName ? from : null; - this._execModule(localModule, undefined, mockRegistry, fromPath); + this._loadModule( + localModule, + from, + moduleName, + modulePath, + undefined, + mockRegistry, + ); + mockRegistry[moduleID] = localModule.exports; - localModule.loaded = true; } else { // Look for a real module to generate an automock from mockRegistry[moduleID] = this._generateMock(from, moduleName); @@ -443,6 +444,28 @@ class Runtime { return mockRegistry[moduleID]; } + private _loadModule( + localModule: InitialModule, + from: Config.Path, + moduleName: string | undefined, + modulePath: Config.Path, + options: InternalModuleOptions | undefined, + moduleRegistry: ModuleRegistry, + ) { + if (path.extname(modulePath) === '.json') { + localModule.exports = this._environment.global.JSON.parse( + stripBOM(fs.readFileSync(modulePath, 'utf8')), + ); + } else if (path.extname(modulePath) === '.node') { + localModule.exports = require(modulePath); + } else { + // Only include the fromPath if a moduleName is given. Else treat as root. + const fromPath = moduleName ? from : null; + this._execModule(localModule, options, moduleRegistry, fromPath); + } + localModule.loaded = true; + } + requireModuleOrMock(from: Config.Path, moduleName: string) { try { if (this._shouldMock(from, moduleName)) {