From ddd3ceb24926d2a0f691f94ed3a2ea73576f9cb9 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 19 Mar 2019 15:19:48 +0000 Subject: [PATCH 1/3] fix: allow json file as manual mock --- CHANGELOG.md | 1 + e2e/mock-json/__mocks__/data.json | 3 ++ e2e/mock-json/__tests__/index.js | 18 ++++++++++ e2e/mock-json/data.json | 3 ++ e2e/mock-json/index.js | 14 ++++++++ e2e/mock-json/package.json | 5 +++ packages/jest-runtime/src/index.ts | 55 ++++++++++++++++++++++-------- 7 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 e2e/mock-json/__mocks__/data.json create mode 100644 e2e/mock-json/__tests__/index.js create mode 100644 e2e/mock-json/data.json create mode 100644 e2e/mock-json/index.js create mode 100644 e2e/mock-json/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index f920e1c54f5f..d4c99b7bf785 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 - `[jest-worker]` Move from `process.exit` to `exit` ([#7327](https://github.com/facebook/jest/pull/7327)) - `[pretty-format]` Print `BigInt` as a readable number instead of `{}` ([#8138](https://github.com/facebook/jest/pull/8138)) 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..c43658ca69dc 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -341,17 +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); - } + + this._loadModule( + localModule, + from, + moduleName, + modulePath, + options, + moduleRegistry, + ); localModule.loaded = true; } @@ -430,11 +428,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 +446,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)) { From 2bec913d6b17bd1e3adac3767a7409b56e534236 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 19 Mar 2019 15:23:45 +0000 Subject: [PATCH 2/3] link to PR --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4c99b7bf785..0ee8b2f58446 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +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 +- `[jest-runtime]` Allow json file as manual mock ([#8159](https://github.com/facebook/jest/pull/8159)) - `[jest-worker]` Move from `process.exit` to `exit` ([#7327](https://github.com/facebook/jest/pull/7327)) - `[pretty-format]` Print `BigInt` as a readable number instead of `{}` ([#8138](https://github.com/facebook/jest/pull/8138)) From 5a43846700bc7064bb75c3940bb2f6c8df3a5f88 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 19 Mar 2019 15:45:11 +0000 Subject: [PATCH 3/3] remove extra assignment --- packages/jest-runtime/src/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index c43658ca69dc..067c247fdaf9 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -350,8 +350,6 @@ class Runtime { options, moduleRegistry, ); - - localModule.loaded = true; } return moduleRegistry[modulePath].exports; }