Skip to content

Commit

Permalink
Implement 'jest.requireMock' (#4292)
Browse files Browse the repository at this point in the history
Similar to #4260
  • Loading branch information
TheParadoxTurtle authored and aaronabramov committed Aug 21, 2017
1 parent cb6bcba commit 07855c3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
60 changes: 60 additions & 0 deletions integration_tests/__tests__/jest_require_mock.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/

'use strict';

const path = require('path');
const os = require('os');
const skipOnWindows = require('../../scripts/skip_on_windows');
const {cleanup, writeFiles} = require('../utils');
const runJest = require('../runJest');

const DIR = path.resolve(os.tmpdir(), 'jest_require_mock_test');

skipOnWindows.suite();

beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));

test('understands dependencies using require.requireMock', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
'__tests__/a.test.js': `
const a = require.requireMock('../a');
test('a', () => {});
`,
'__tests__/b.test.js': `test('b', () => {});`,
'a.js': `module.exports = {}`,
'package.json': JSON.stringify({jest: {}}),
});

let stdout;
let stderr;
({stdout, stderr} = runJest(DIR, ['--findRelatedTests', 'a.js']));

expect(stdout).not.toMatch('No tests found');
expect(stderr).toMatch('PASS __tests__/a.test.js');
expect(stderr).not.toMatch('PASS __tests__/b.test.js');

// change to jest.requireMock
writeFiles(DIR, {
'__tests__/a.test.js': `
const a = jest.requireMock('../a');
test('a', () => {});
`,
});

({stderr, stdout} = runJest(DIR, ['--findRelatedTests', 'a.js']));
expect(stdout).not.toMatch('No tests found');
expect(stderr).toMatch('PASS __tests__/a.test.js');
expect(stderr).not.toMatch('PASS __tests__/b.test.js');
});
10 changes: 10 additions & 0 deletions packages/jest-haste-map/src/lib/__tests__/extract_requires.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,13 @@ it('understands jest.requireActual', () => {
const code = `jest.requireActual('whiskey');`;
expect(extractRequires(code)).toEqual(['whiskey']);
});

it('understands require.requireMock', () => {
const code = `require.requireMock('cheeseburger');`;
expect(extractRequires(code)).toEqual(['cheeseburger']);
});

it('understands jest.requireMock', () => {
const code = `jest.requireMock('scotch');`;
expect(extractRequires(code)).toEqual(['scotch']);
});
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/lib/extract_requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const lineCommentRe = /\/\/.*/g;
const replacePatterns = {
EXPORT_RE: /(\bexport\s+(?!type )(?:[^'"]+\s+from\s+)??)(['"])([^'"]+)(\2)/g,
IMPORT_RE: /(\bimport\s+(?!type )(?:[^'"]+\s+from\s+)??)(['"])([^'"]+)(\2)/g,
REQUIRE_EXTENSIONS_PATTERN: /(?:^|[^.]\s*)(\b(?:require\s*?\.\s*?(?:requireActual|requireMock)|jest\s*?\.\s*?(?:requireActual|genMockFromModule))\s*?\(\s*?)([`'"])([^`'"]+)(\2\s*?\))/g,
REQUIRE_EXTENSIONS_PATTERN: /(?:^|[^.]\s*)(\b(?:require\s*?\.\s*?(?:requireActual|requireMock)|jest\s*?\.\s*?(?:requireActual|requireMock|genMockFromModule))\s*?\(\s*?)([`'"])([^`'"]+)(\2\s*?\))/g,
REQUIRE_RE: /(?:^|[^.]\s*)(\brequire\s*?\(\s*?)([`'"])([^`'"]+)(\2\s*?\))/g,
};

Expand Down
1 change: 1 addition & 0 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ class Runtime {

mock,
requireActual: localRequire.requireActual,
requireMock: localRequire.requireMock,
resetAllMocks,
resetModuleRegistry: resetModules,
resetModules,
Expand Down
1 change: 1 addition & 0 deletions types/Jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type Jest = {|
isMockFunction(fn: Function): boolean,
mock(moduleName: string, moduleFactory?: any, options?: Object): Jest,
requireActual: LocalModuleRequire,
requireMock: LocalModuleRequire,
resetAllMocks(): Jest,
resetModuleRegistry(): Jest,
resetModules(): Jest,
Expand Down

0 comments on commit 07855c3

Please sign in to comment.