From 348b77f3dbeee162853e6f22522f216f733fae9d Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Mon, 2 Oct 2017 17:34:04 -0400 Subject: [PATCH 01/17] Added ability to specify a name for mocked functions --- docs/en/JestObjectAPI.md | 17 ++++++++++++++--- docs/en/MockFunctionAPI.md | 29 ++++++++++++++++++++++++----- docs/en/MockFunctions.md | 21 +++++++++++++++++++++ packages/expect/src/spy_matchers.js | 29 ++++++++++++----------------- packages/jest-mock/src/index.js | 25 +++++++++++++++++++++++-- 5 files changed, 94 insertions(+), 27 deletions(-) diff --git a/docs/en/JestObjectAPI.md b/docs/en/JestObjectAPI.md index 792e8210ce4a..51a7ead1357f 100644 --- a/docs/en/JestObjectAPI.md +++ b/docs/en/JestObjectAPI.md @@ -15,7 +15,7 @@ The `jest` object is automatically in scope within every test file. The methods - [`jest.clearAllTimers()`](#jestclearalltimers) - [`jest.disableAutomock()`](#jestdisableautomock) - [`jest.enableAutomock()`](#jestenableautomock) - - [`jest.fn(implementation)`](#jestfnimplementation) + - [`jest.fn(implementation, name)`](#jestfnimplementation) - [`jest.isMockFunction(fn)`](#jestismockfunctionfn) - [`jest.genMockFromModule(moduleName)`](#jestgenmockfrommodulemodulename) - [`jest.mock(moduleName, factory, options)`](#jestmockmodulename-factory-options) @@ -65,8 +65,8 @@ Returns the `jest` object for chaining. *Note: this method was previously called `autoMockOn`. When using `babel-jest`, calls to `enableAutomock` will automatically be hoisted to the top of the code block. Use `autoMockOn` if you want to explicitly avoid this behavior.* -### `jest.fn(implementation)` -Returns a new, unused [mock function](/jest/docs/mock-function-api.html). Optionally takes a mock implementation. +### `jest.fn(implementation, name)` +Returns a new, unused [mock function](/jest/docs/mock-function-api.html). Optionally takes a mock implementation and/or a mock name. ```js const mockFn = jest.fn(); @@ -77,6 +77,17 @@ Returns a new, unused [mock function](/jest/docs/mock-function-api.html). Option const returnsTrue = jest.fn(() => true); console.log(returnsTrue()); // true; ``` +With mock name: +```js + const mockFn = jest.fn('doesNothing'); + mockFn(); + expect(mockFn).toHaveBeenCalled(); + + // With a mock implementation: + const returnsTrue = jest.fn(() => true, 'returnsTrue'); + console.log(returnsTrue()); // true; +``` + ### `jest.isMockFunction(fn)` Determines if the given function is a mocked function. diff --git a/docs/en/MockFunctionAPI.md b/docs/en/MockFunctionAPI.md index b6052928ac3d..430e9ab67222 100644 --- a/docs/en/MockFunctionAPI.md +++ b/docs/en/MockFunctionAPI.md @@ -138,13 +138,32 @@ console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn()); > 'first call', 'second call', 'default', 'default' ``` -### `mockFn.mockReturnThis()` -Just a simple sugar function for: +### `mockFn.mockName(value)` +Accepts a string to use in test result output in place of "jest.fn()" to indicate which mock function is being referenced. + +*Note: `jest.fn(implementation, name)` is a shorthand for `jest.fn().mockImplementation(implementation).mockName(name)`. Also, if you only provide a string to jest.fn(), it will be used as the mock name value.* + +For example: ```js -jest.fn(function() { - return this; -}); +const mockFn = jest.fn().mockName('mockedFunction'); +// mockFn(); +expect(mockFn).toHaveBeenCalled(); +``` + +Will result in this error: +``` + expect(mockedFunction).toHaveBeenCalled() + + Expected mock function to have been called. +``` + +Shorthand examples: + +```js +const mockFn = jest.fn('mockedFunction'); + +const mockFn2 = jest.fn(scalar => 42 + scalar, 'add42'); ``` ### `mockFn.mockReturnValue(value)` diff --git a/docs/en/MockFunctions.md b/docs/en/MockFunctions.md index f0a0c8149477..3a4d7ed927ad 100644 --- a/docs/en/MockFunctions.md +++ b/docs/en/MockFunctions.md @@ -217,6 +217,27 @@ const otherObj = { }; ``` +## Mock Names + +You can optionally provide a name for your mock functions, which will be displayed instead of "jest.fn()" in test error output. Use this if you want to be able to quickly identify the mock function reporting an error in your test output. + +```javascript +const myMockFn = jest.fn() + .mockReturnValue('default') + .mockImplementation(scalar => 42 + scalar) + .mockName('add42'); + +const myMockFn2 = jest.fn(scalar => 42 + scalar, 'add42'); +``` + +You can also pass a mock name without providing a mock implementation: + +```javascript +const myMockFn = jest.fn('myMockFunction'); +// myMockFn(); +expect(myMockFn).toHaveBeenCalled(); +``` + ## Custom Matchers Finally, in order to make it simpler to assert how mock functions have been diff --git a/packages/expect/src/spy_matchers.js b/packages/expect/src/spy_matchers.js index 17a681925906..ce6c27bae385 100644 --- a/packages/expect/src/spy_matchers.js +++ b/packages/expect/src/spy_matchers.js @@ -25,17 +25,13 @@ import { import {equals} from './jasmine_utils'; import {iterableEquality, partition} from './utils'; -const RECEIVED_NAME = { - 'mock function': 'jest.fn()', - spy: 'spy', -}; - const createToBeCalledMatcher = matcherName => (received, expected) => { ensureNoExpected(expected, matcherName); ensureMock(received, matcherName); const receivedIsSpy = isSpy(received); const type = receivedIsSpy ? 'spy' : 'mock function'; + const receivedName = receivedIsSpy ? 'spy' : received.mockName(); const count = receivedIsSpy ? received.calls.count() : received.mock.calls.length; @@ -45,12 +41,12 @@ const createToBeCalledMatcher = matcherName => (received, expected) => { const pass = count > 0; const message = pass ? () => - matcherHint('.not' + matcherName, RECEIVED_NAME[type], '') + + matcherHint('.not' + matcherName, receivedName, '') + '\n\n' + `Expected ${type} not to be called ` + formatReceivedCalls(calls, CALL_PRINT_LIMIT, {sameSentence: true}) : () => - matcherHint(matcherName, RECEIVED_NAME[type], '') + + matcherHint(matcherName, receivedName, '') + '\n\n' + `Expected ${type} to have been called.`; @@ -65,6 +61,7 @@ const createToBeCalledWithMatcher = matcherName => ( const receivedIsSpy = isSpy(received); const type = receivedIsSpy ? 'spy' : 'mock function'; + const receivedName = receivedIsSpy ? 'spy' : received.mockName(); const calls = receivedIsSpy ? received.calls.all().map(x => x.args) : received.mock.calls; @@ -76,12 +73,12 @@ const createToBeCalledWithMatcher = matcherName => ( const message = pass ? () => - matcherHint('.not' + matcherName, RECEIVED_NAME[type]) + + matcherHint('.not' + matcherName, receivedName) + '\n\n' + `Expected ${type} not to have been called with:\n` + ` ${printExpected(expected)}` : () => - matcherHint(matcherName, RECEIVED_NAME[type]) + + matcherHint(matcherName, receivedName) + '\n\n' + `Expected ${type} to have been called with:\n` + formatMismatchedCalls(fail, expected, CALL_PRINT_LIMIT); @@ -97,6 +94,7 @@ const createLastCalledWithMatcher = matcherName => ( const receivedIsSpy = isSpy(received); const type = receivedIsSpy ? 'spy' : 'mock function'; + const receivedName = receivedIsSpy ? 'spy' : received.mockName(); const calls = receivedIsSpy ? received.calls.all().map(x => x.args) : received.mock.calls; @@ -104,12 +102,12 @@ const createLastCalledWithMatcher = matcherName => ( const message = pass ? () => - matcherHint('.not' + matcherName, RECEIVED_NAME[type]) + + matcherHint('.not' + matcherName, receivedName) + '\n\n' + `Expected ${type} to not have been last called with:\n` + ` ${printExpected(expected)}` : () => - matcherHint(matcherName, RECEIVED_NAME[type]) + + matcherHint(matcherName, receivedName) + '\n\n' + `Expected ${type} to have been last called with:\n` + formatMismatchedCalls(calls, expected, LAST_CALL_PRINT_LIMIT); @@ -129,23 +127,20 @@ const spyMatchers: MatchersObject = { const receivedIsSpy = isSpy(received); const type = receivedIsSpy ? 'spy' : 'mock function'; + const receivedName = receivedIsSpy ? 'spy' : received.mockName(); const count = receivedIsSpy ? received.calls.count() : received.mock.calls.length; const pass = count === expected; const message = pass ? () => - matcherHint( - '.not' + matcherName, - RECEIVED_NAME[type], - String(expected), - ) + + matcherHint('.not' + matcherName, receivedName, String(expected)) + `\n\n` + `Expected ${type} not to be called ` + `${EXPECTED_COLOR(pluralize('time', expected))}, but it was` + ` called exactly ${RECEIVED_COLOR(pluralize('time', count))}.` : () => - matcherHint(matcherName, RECEIVED_NAME[type], String(expected)) + + matcherHint(matcherName, receivedName, String(expected)) + '\n\n' + `Expected ${type} to have been called ` + `${EXPECTED_COLOR(pluralize('time', expected))},` + diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index 9f14f99f2bb5..89a42a5e55b0 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -30,6 +30,7 @@ type MockFunctionConfig = { isReturnValueLastSet: boolean, defaultReturnValue: any, mockImpl: any, + mockName: string, specificReturnValues: Array, specificMockImpls: Array, }; @@ -269,6 +270,7 @@ class ModuleMockerClass { defaultReturnValue: undefined, isReturnValueLastSet: false, mockImpl: undefined, + mockName: 'jest.fn()', specificMockImpls: [], specificReturnValues: [], }; @@ -433,6 +435,17 @@ class ModuleMockerClass { return this; }); + f.mockName = name => { + if (arguments.length > 0 && name) { + const mockConfig = this._ensureMockConfig(f); + mockConfig.mockName = name; + return f; + } else { + const mockConfig = this._ensureMockConfig(f); + return mockConfig.mockName || 'jest.fn()'; + } + }; + if (metadata.mockImpl) { f.mockImplementation(metadata.mockImpl); } @@ -632,11 +645,19 @@ class ModuleMockerClass { return !!fn._isMockFunction; } - fn(implementation?: any): any { + fn(implementation?: any, mockName?: string): any { const length = implementation ? implementation.length : 0; const fn = this._makeComponent({length, type: 'function'}); if (implementation) { - fn.mockImplementation(implementation); + if (typeof implementation === 'function') { + fn.mockImplementation(implementation); + } else if (typeof implementation === 'string') { + fn.mockName(implementation); + } + } + + if (arguments.length > 1 && mockName) { + fn.mockName(mockName); } return fn; } From c39eb37d92327c670f96579c40339282dd6cd088 Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Fri, 6 Oct 2017 16:04:09 -0400 Subject: [PATCH 02/17] Added integration tests for mock name functionality --- .../__snapshots__/mock_names.test.js.snap | 89 +++++++++++++++++++ .../__tests__/mock_names.test.js | 76 ++++++++++++++++ .../__tests__/index.js | 19 ++++ .../with-empty-mock-name-not-called/index.js | 10 +++ .../package.json | 6 ++ .../with-empty-mock-name/__tests__/index.js | 19 ++++ .../mock-names/with-empty-mock-name/index.js | 10 +++ .../with-empty-mock-name/package.json | 6 ++ .../__tests__/index.js | 18 ++++ .../with-mock-name-long-not-called/index.js | 10 +++ .../package.json | 6 ++ .../with-mock-name-long/__tests__/index.js | 18 ++++ .../mock-names/with-mock-name-long/index.js | 10 +++ .../with-mock-name-long/package.json | 6 ++ .../__tests__/index.js | 18 ++++ .../with-mock-name-not-called/index.js | 10 +++ .../with-mock-name-not-called/package.json | 6 ++ .../__tests__/index.js | 17 ++++ .../with-mock-name-short-not-called/index.js | 10 +++ .../package.json | 6 ++ .../with-mock-name-short/__tests__/index.js | 17 ++++ .../mock-names/with-mock-name-short/index.js | 10 +++ .../with-mock-name-short/package.json | 6 ++ .../with-mock-name/__tests__/index.js | 18 ++++ .../mock-names/with-mock-name/index.js | 10 +++ .../mock-names/with-mock-name/package.json | 6 ++ .../__tests__/index.js | 18 ++++ .../without-mock-name-not-called/index.js | 10 +++ .../without-mock-name-not-called/package.json | 6 ++ .../without-mock-name/__tests__/index.js | 18 ++++ .../mock-names/without-mock-name/index.js | 10 +++ .../mock-names/without-mock-name/package.json | 6 ++ 32 files changed, 505 insertions(+) create mode 100644 integration_tests/__tests__/__snapshots__/mock_names.test.js.snap create mode 100644 integration_tests/__tests__/mock_names.test.js create mode 100644 integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js create mode 100644 integration_tests/mock-names/with-empty-mock-name-not-called/index.js create mode 100644 integration_tests/mock-names/with-empty-mock-name-not-called/package.json create mode 100644 integration_tests/mock-names/with-empty-mock-name/__tests__/index.js create mode 100644 integration_tests/mock-names/with-empty-mock-name/index.js create mode 100644 integration_tests/mock-names/with-empty-mock-name/package.json create mode 100644 integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js create mode 100644 integration_tests/mock-names/with-mock-name-long-not-called/index.js create mode 100644 integration_tests/mock-names/with-mock-name-long-not-called/package.json create mode 100644 integration_tests/mock-names/with-mock-name-long/__tests__/index.js create mode 100644 integration_tests/mock-names/with-mock-name-long/index.js create mode 100644 integration_tests/mock-names/with-mock-name-long/package.json create mode 100644 integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js create mode 100644 integration_tests/mock-names/with-mock-name-not-called/index.js create mode 100644 integration_tests/mock-names/with-mock-name-not-called/package.json create mode 100644 integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js create mode 100644 integration_tests/mock-names/with-mock-name-short-not-called/index.js create mode 100644 integration_tests/mock-names/with-mock-name-short-not-called/package.json create mode 100644 integration_tests/mock-names/with-mock-name-short/__tests__/index.js create mode 100644 integration_tests/mock-names/with-mock-name-short/index.js create mode 100644 integration_tests/mock-names/with-mock-name-short/package.json create mode 100644 integration_tests/mock-names/with-mock-name/__tests__/index.js create mode 100644 integration_tests/mock-names/with-mock-name/index.js create mode 100644 integration_tests/mock-names/with-mock-name/package.json create mode 100644 integration_tests/mock-names/without-mock-name-not-called/__tests__/index.js create mode 100644 integration_tests/mock-names/without-mock-name-not-called/index.js create mode 100644 integration_tests/mock-names/without-mock-name-not-called/package.json create mode 100644 integration_tests/mock-names/without-mock-name/__tests__/index.js create mode 100644 integration_tests/mock-names/without-mock-name/index.js create mode 100644 integration_tests/mock-names/without-mock-name/package.json diff --git a/integration_tests/__tests__/__snapshots__/mock_names.test.js.snap b/integration_tests/__tests__/__snapshots__/mock_names.test.js.snap new file mode 100644 index 000000000000..8afc04ae44b9 --- /dev/null +++ b/integration_tests/__tests__/__snapshots__/mock_names.test.js.snap @@ -0,0 +1,89 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`suite with mock name, long form, mock called 1`] = ` +"PASS __tests__/index.js + ✓ first test + +" +`; + +exports[`suite with mock name, long form, mock not called 1`] = ` +"FAIL __tests__/index.js + ✕ first test + + ● first test + + expect(myMockedFunctionLong).toHaveBeenCalled() + + Expected mock function to have been called. + + at __tests__/index.js:16:18 + +" +`; + +exports[`suite with mock name, mock called 1`] = ` +"PASS __tests__/index.js + ✓ first test + +" +`; + +exports[`suite with mock name, mock not called 1`] = ` +"FAIL __tests__/index.js + ✕ first test + + ● first test + + expect(myMockedFunction).toHaveBeenCalled() + + Expected mock function to have been called. + + at __tests__/index.js:16:18 + +" +`; + +exports[`suite with mock name, short form, mock called 1`] = ` +"PASS __tests__/index.js + ✓ first test + +" +`; + +exports[`suite with mock name, short form, mock not called 1`] = ` +"FAIL __tests__/index.js + ✕ first test + + ● first test + + expect(myMockedFunctionShort).toHaveBeenCalled() + + Expected mock function to have been called. + + at __tests__/index.js:15:18 + +" +`; + +exports[`suite without mock name, mock called 1`] = ` +"PASS __tests__/index.js + ✓ first test + +" +`; + +exports[`suite without mock name, mock not called 1`] = ` +"FAIL __tests__/index.js + ✕ first test + + ● first test + + expect(jest.fn()).toHaveBeenCalled() + + Expected mock function to have been called. + + at __tests__/index.js:16:18 + +" +`; diff --git a/integration_tests/__tests__/mock_names.test.js b/integration_tests/__tests__/mock_names.test.js new file mode 100644 index 000000000000..4ccc24404fb1 --- /dev/null +++ b/integration_tests/__tests__/mock_names.test.js @@ -0,0 +1,76 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ +'use strict'; + +const runJest = require('../runJest'); +const {extractSummary} = require('../utils'); + +test('suite without mock name, mock called', () => { + const {stderr, status} = runJest('mock-names/without-mock-name'); + const {rest} = extractSummary(stderr); + + expect(status).toBe(0); + expect(rest).toMatchSnapshot(); +}); + +test('suite without mock name, mock not called', () => { + const {stderr, status} = runJest('mock-names/without-mock-name-not-called'); + const {rest} = extractSummary(stderr); + + expect(status).toBe(1); + expect(rest).toMatchSnapshot(); +}); + +test('suite with mock name, mock called', () => { + const {stderr, status} = runJest('mock-names/with-mock-name'); + const {rest} = extractSummary(stderr); + + expect(status).toBe(0); + expect(rest).toMatchSnapshot(); +}); + +test('suite with mock name, mock not called', () => { + const {stderr, status} = runJest('mock-names/with-mock-name-not-called'); + const {rest} = extractSummary(stderr); + + expect(status).toBe(1); + expect(rest).toMatchSnapshot(); +}); + +test('suite with mock name, long form, mock called', () => { + const {stderr, status} = runJest('mock-names/with-mock-name-long'); + const {rest} = extractSummary(stderr); + + expect(status).toBe(0); + expect(rest).toMatchSnapshot(); +}); + +test('suite with mock name, long form, mock not called', () => { + const {stderr, status} = runJest('mock-names/with-mock-name-long-not-called'); + const {rest} = extractSummary(stderr); + + expect(status).toBe(1); + expect(rest).toMatchSnapshot(); +}); + +test('suite with mock name, short form, mock called', () => { + const {stderr, status} = runJest('mock-names/with-mock-name-short'); + const {rest} = extractSummary(stderr); + + expect(status).toBe(0); + expect(rest).toMatchSnapshot(); +}); + +test('suite with mock name, short form, mock not called', () => { + const {stderr, status} = runJest('mock-names/with-mock-name-short-not-called'); + const {rest} = extractSummary(stderr); + + expect(status).toBe(1); + expect(rest).toMatchSnapshot(); +}); diff --git a/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js b/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js new file mode 100644 index 000000000000..9bad1a0e0e28 --- /dev/null +++ b/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const importedFn = require('../'); +// empty mock name should result in default 'jest.fn()' output +const mockFn = jest.fn(importedFn, ''); + +test('first test', () => { + // mockFn explicitly not called to test error snapshot + expect(mockFn).toHaveBeenCalled(); + expect(mockFn.mock.calls.length).toBe(1); +}); diff --git a/integration_tests/mock-names/with-empty-mock-name-not-called/index.js b/integration_tests/mock-names/with-empty-mock-name-not-called/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/with-empty-mock-name-not-called/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/with-empty-mock-name-not-called/package.json b/integration_tests/mock-names/with-empty-mock-name-not-called/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/with-empty-mock-name-not-called/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/with-empty-mock-name/__tests__/index.js b/integration_tests/mock-names/with-empty-mock-name/__tests__/index.js new file mode 100644 index 000000000000..3a0550a0f6af --- /dev/null +++ b/integration_tests/mock-names/with-empty-mock-name/__tests__/index.js @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const importedFn = require('../'); +// empty mock name should result in default 'jest.fn()' output +const mockFn = jest.fn(importedFn, ''); + +test('first test', () => { + mockFn(); + expect(mockFn).toHaveBeenCalled(); + expect(mockFn.mock.calls.length).toBe(1); +}); diff --git a/integration_tests/mock-names/with-empty-mock-name/index.js b/integration_tests/mock-names/with-empty-mock-name/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/with-empty-mock-name/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/with-empty-mock-name/package.json b/integration_tests/mock-names/with-empty-mock-name/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/with-empty-mock-name/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js b/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js new file mode 100644 index 000000000000..68c462b9ec35 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const importedFn = require('../'); +const mockFn = jest.fn(importedFn).mockName('myMockedFunctionLong'); + +test('first test', () => { + // mockFn explicitly not called to test error snapshot + expect(mockFn).toHaveBeenCalled(); + expect(mockFn.mock.calls.length).toBe(1); +}); diff --git a/integration_tests/mock-names/with-mock-name-long-not-called/index.js b/integration_tests/mock-names/with-mock-name-long-not-called/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-long-not-called/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-long-not-called/package.json b/integration_tests/mock-names/with-mock-name-long-not-called/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-long-not-called/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/with-mock-name-long/__tests__/index.js b/integration_tests/mock-names/with-mock-name-long/__tests__/index.js new file mode 100644 index 000000000000..4864aa7915d4 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-long/__tests__/index.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const importedFn = require('../'); +const mockFn = jest.fn(importedFn).mockName('myMockedFunctionLong'); + +test('first test', () => { + mockFn(); + expect(mockFn).toHaveBeenCalled(); + expect(mockFn.mock.calls.length).toBe(1); +}); diff --git a/integration_tests/mock-names/with-mock-name-long/index.js b/integration_tests/mock-names/with-mock-name-long/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-long/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-long/package.json b/integration_tests/mock-names/with-mock-name-long/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-long/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js b/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js new file mode 100644 index 000000000000..48cd0b26689b --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const importedFn = require('../'); +const mockFn = jest.fn(importedFn, 'myMockedFunction'); + +test('first test', () => { + // mockFn explicitly not called to test error snapshot + expect(mockFn).toHaveBeenCalled(); + expect(mockFn.mock.calls.length).toBe(1); +}); diff --git a/integration_tests/mock-names/with-mock-name-not-called/index.js b/integration_tests/mock-names/with-mock-name-not-called/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-not-called/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-not-called/package.json b/integration_tests/mock-names/with-mock-name-not-called/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-not-called/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js b/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js new file mode 100644 index 000000000000..a8900b68f3e9 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const mockFn = jest.fn('myMockedFunctionShort'); + +test('first test', () => { + // mockFn explicitly not called to test error snapshot + expect(mockFn).toHaveBeenCalled(); + expect(mockFn.mock.calls.length).toBe(1); +}); diff --git a/integration_tests/mock-names/with-mock-name-short-not-called/index.js b/integration_tests/mock-names/with-mock-name-short-not-called/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-short-not-called/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-short-not-called/package.json b/integration_tests/mock-names/with-mock-name-short-not-called/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-short-not-called/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/with-mock-name-short/__tests__/index.js b/integration_tests/mock-names/with-mock-name-short/__tests__/index.js new file mode 100644 index 000000000000..c2fd6a0c50a8 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-short/__tests__/index.js @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const mockFn = jest.fn('myMockedFunctionShort'); + +test('first test', () => { + mockFn(); + expect(mockFn).toHaveBeenCalled(); + expect(mockFn.mock.calls.length).toBe(1); +}); diff --git a/integration_tests/mock-names/with-mock-name-short/index.js b/integration_tests/mock-names/with-mock-name-short/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-short/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-short/package.json b/integration_tests/mock-names/with-mock-name-short/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-short/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/with-mock-name/__tests__/index.js b/integration_tests/mock-names/with-mock-name/__tests__/index.js new file mode 100644 index 000000000000..e42ee9bc5824 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name/__tests__/index.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const importedFn = require('../'); +const mockFn = jest.fn(importedFn, 'myMockedFunction'); + +test('first test', () => { + mockFn(); + expect(mockFn).toHaveBeenCalled(); + expect(mockFn.mock.calls.length).toBe(1); +}); diff --git a/integration_tests/mock-names/with-mock-name/index.js b/integration_tests/mock-names/with-mock-name/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/with-mock-name/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name/package.json b/integration_tests/mock-names/with-mock-name/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/without-mock-name-not-called/__tests__/index.js b/integration_tests/mock-names/without-mock-name-not-called/__tests__/index.js new file mode 100644 index 000000000000..05becdccc80f --- /dev/null +++ b/integration_tests/mock-names/without-mock-name-not-called/__tests__/index.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const importedFn = require('../'); +const mockFn = jest.fn(importedFn); + +test('first test', () => { + // mockFn explicitly not called to test error snapshot + expect(mockFn).toHaveBeenCalled(); + expect(mockFn.mock.calls.length).toBe(1); +}); diff --git a/integration_tests/mock-names/without-mock-name-not-called/index.js b/integration_tests/mock-names/without-mock-name-not-called/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/without-mock-name-not-called/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/without-mock-name-not-called/package.json b/integration_tests/mock-names/without-mock-name-not-called/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/without-mock-name-not-called/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/without-mock-name/__tests__/index.js b/integration_tests/mock-names/without-mock-name/__tests__/index.js new file mode 100644 index 000000000000..37f3b4283c0d --- /dev/null +++ b/integration_tests/mock-names/without-mock-name/__tests__/index.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const importedFn = require('../'); +const mockFn = jest.fn(importedFn); + +test('first test', () => { + mockFn(); + expect(mockFn).toHaveBeenCalled(); + expect(mockFn.mock.calls.length).toBe(1); +}); diff --git a/integration_tests/mock-names/without-mock-name/index.js b/integration_tests/mock-names/without-mock-name/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/without-mock-name/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/without-mock-name/package.json b/integration_tests/mock-names/without-mock-name/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/without-mock-name/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} From 76936ceaf4afa32d1b15cd7ba9bc7a5603e3f140 Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Fri, 6 Oct 2017 17:49:28 -0400 Subject: [PATCH 03/17] Added unit tests for mock name functionality --- .../jest-mock/src/__tests__/jest_mock.test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/jest-mock/src/__tests__/jest_mock.test.js b/packages/jest-mock/src/__tests__/jest_mock.test.js index ee734a026768..0cad1f6c5a0f 100644 --- a/packages/jest-mock/src/__tests__/jest_mock.test.js +++ b/packages/jest-mock/src/__tests__/jest_mock.test.js @@ -458,6 +458,23 @@ describe('moduleMocker', () => { expect(moduleMocker.isMockFunction(mockFn)).toBe(true); }); + test('mockName sets the mock name', () => { + const fn = jest.fn(); + expect(fn.mockName()).toBe('jest.fn()'); + fn.mockName('myMockFn'); + expect(fn.mockName()).toBe('myMockFn'); + }); + + test('mockName as only arg sets the mock name', () => { + const fn = jest.fn('myMockFn'); + expect(fn.mockName()).toBe('myMockFn'); + }); + + test('mockName as second arg sets the mock name', () => { + const fn = jest.fn(() => {}, 'myMockFn'); + expect(fn.mockName()).toBe('myMockFn'); + }); + describe('spyOn', () => { it('should work', () => { let isOriginalCalled = false; From 2f8066eeb71258e5bd69fb2e8b742daaf1f2a1e2 Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Fri, 6 Oct 2017 17:58:31 -0400 Subject: [PATCH 04/17] Linting fix --- integration_tests/__tests__/mock_names.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/integration_tests/__tests__/mock_names.test.js b/integration_tests/__tests__/mock_names.test.js index 4ccc24404fb1..70f3c2528224 100644 --- a/integration_tests/__tests__/mock_names.test.js +++ b/integration_tests/__tests__/mock_names.test.js @@ -68,7 +68,9 @@ test('suite with mock name, short form, mock called', () => { }); test('suite with mock name, short form, mock not called', () => { - const {stderr, status} = runJest('mock-names/with-mock-name-short-not-called'); + const {stderr, status} = runJest( + 'mock-names/with-mock-name-short-not-called', + ); const {rest} = extractSummary(stderr); expect(status).toBe(1); From 29623ee8769b539be60371d3c780fc6c4e9a42e2 Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Fri, 6 Oct 2017 21:25:57 -0400 Subject: [PATCH 05/17] Switched from snapshot matching to regex matching because the snapshots vary by platform, apparently --- .../__snapshots__/mock_names.test.js.snap | 89 ------------------- .../__tests__/mock_names.test.js | 24 ++--- 2 files changed, 8 insertions(+), 105 deletions(-) delete mode 100644 integration_tests/__tests__/__snapshots__/mock_names.test.js.snap diff --git a/integration_tests/__tests__/__snapshots__/mock_names.test.js.snap b/integration_tests/__tests__/__snapshots__/mock_names.test.js.snap deleted file mode 100644 index 8afc04ae44b9..000000000000 --- a/integration_tests/__tests__/__snapshots__/mock_names.test.js.snap +++ /dev/null @@ -1,89 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`suite with mock name, long form, mock called 1`] = ` -"PASS __tests__/index.js - ✓ first test - -" -`; - -exports[`suite with mock name, long form, mock not called 1`] = ` -"FAIL __tests__/index.js - ✕ first test - - ● first test - - expect(myMockedFunctionLong).toHaveBeenCalled() - - Expected mock function to have been called. - - at __tests__/index.js:16:18 - -" -`; - -exports[`suite with mock name, mock called 1`] = ` -"PASS __tests__/index.js - ✓ first test - -" -`; - -exports[`suite with mock name, mock not called 1`] = ` -"FAIL __tests__/index.js - ✕ first test - - ● first test - - expect(myMockedFunction).toHaveBeenCalled() - - Expected mock function to have been called. - - at __tests__/index.js:16:18 - -" -`; - -exports[`suite with mock name, short form, mock called 1`] = ` -"PASS __tests__/index.js - ✓ first test - -" -`; - -exports[`suite with mock name, short form, mock not called 1`] = ` -"FAIL __tests__/index.js - ✕ first test - - ● first test - - expect(myMockedFunctionShort).toHaveBeenCalled() - - Expected mock function to have been called. - - at __tests__/index.js:15:18 - -" -`; - -exports[`suite without mock name, mock called 1`] = ` -"PASS __tests__/index.js - ✓ first test - -" -`; - -exports[`suite without mock name, mock not called 1`] = ` -"FAIL __tests__/index.js - ✕ first test - - ● first test - - expect(jest.fn()).toHaveBeenCalled() - - Expected mock function to have been called. - - at __tests__/index.js:16:18 - -" -`; diff --git a/integration_tests/__tests__/mock_names.test.js b/integration_tests/__tests__/mock_names.test.js index 70f3c2528224..955d548122e5 100644 --- a/integration_tests/__tests__/mock_names.test.js +++ b/integration_tests/__tests__/mock_names.test.js @@ -13,66 +13,58 @@ const {extractSummary} = require('../utils'); test('suite without mock name, mock called', () => { const {stderr, status} = runJest('mock-names/without-mock-name'); - const {rest} = extractSummary(stderr); expect(status).toBe(0); - expect(rest).toMatchSnapshot(); + expect(stderr).toMatch(/PASS/); }); test('suite without mock name, mock not called', () => { const {stderr, status} = runJest('mock-names/without-mock-name-not-called'); - const {rest} = extractSummary(stderr); expect(status).toBe(1); - expect(rest).toMatchSnapshot(); + expect(stderr).toMatch(/expect\(jest\.fn\(\)\)\.toHaveBeenCalled/); }); test('suite with mock name, mock called', () => { const {stderr, status} = runJest('mock-names/with-mock-name'); - const {rest} = extractSummary(stderr); expect(status).toBe(0); - expect(rest).toMatchSnapshot(); + expect(stderr).toMatch(/PASS/); }); test('suite with mock name, mock not called', () => { const {stderr, status} = runJest('mock-names/with-mock-name-not-called'); - const {rest} = extractSummary(stderr); expect(status).toBe(1); - expect(rest).toMatchSnapshot(); + expect(stderr).toMatch(/expect\(myMockedFunction\)\.toHaveBeenCalled/); }); test('suite with mock name, long form, mock called', () => { const {stderr, status} = runJest('mock-names/with-mock-name-long'); - const {rest} = extractSummary(stderr); expect(status).toBe(0); - expect(rest).toMatchSnapshot(); + expect(stderr).toMatch(/PASS/); }); test('suite with mock name, long form, mock not called', () => { const {stderr, status} = runJest('mock-names/with-mock-name-long-not-called'); - const {rest} = extractSummary(stderr); expect(status).toBe(1); - expect(rest).toMatchSnapshot(); + expect(stderr).toMatch(/expect\(myMockedFunctionLong\)\.toHaveBeenCalled/); }); test('suite with mock name, short form, mock called', () => { const {stderr, status} = runJest('mock-names/with-mock-name-short'); - const {rest} = extractSummary(stderr); expect(status).toBe(0); - expect(rest).toMatchSnapshot(); + expect(stderr).toMatch(/PASS/); }); test('suite with mock name, short form, mock not called', () => { const {stderr, status} = runJest( 'mock-names/with-mock-name-short-not-called', ); - const {rest} = extractSummary(stderr); expect(status).toBe(1); - expect(rest).toMatchSnapshot(); + expect(stderr).toMatch(/expect\(myMockedFunctionShort\)\.toHaveBeenCalled/); }); From 9258feb59d6babf9956ff3eeba11cf81c518ba05 Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Sat, 7 Oct 2017 08:38:28 -0400 Subject: [PATCH 06/17] Re-add accidentally removed doc section --- docs/en/MockFunctionAPI.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/en/MockFunctionAPI.md b/docs/en/MockFunctionAPI.md index 430e9ab67222..dd4adf24cbb6 100644 --- a/docs/en/MockFunctionAPI.md +++ b/docs/en/MockFunctionAPI.md @@ -177,6 +177,15 @@ mock.mockReturnValue(43); mock(); // 43 ``` +### `mockFn.mockReturnThis()` +Just a simple sugar function for: + +```js +jest.fn(function() { + return this; +}); +``` + ### `mockFn.mockReturnValueOnce(value)` Accepts a value that will be returned for one call to the mock function. Can be chained so that successive calls to the mock function return different values. When there are no more `mockReturnValueOnce` values to use, calls will return a value specified by `mockReturnValue`. From 2d284a413f495e027b60b88eaf122eb4a0be696a Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Sat, 7 Oct 2017 08:39:26 -0400 Subject: [PATCH 07/17] Fix the order of replaced doc section --- docs/en/MockFunctionAPI.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/en/MockFunctionAPI.md b/docs/en/MockFunctionAPI.md index dd4adf24cbb6..5786a8489e70 100644 --- a/docs/en/MockFunctionAPI.md +++ b/docs/en/MockFunctionAPI.md @@ -166,6 +166,15 @@ const mockFn = jest.fn('mockedFunction'); const mockFn2 = jest.fn(scalar => 42 + scalar, 'add42'); ``` +### `mockFn.mockReturnThis()` +Just a simple sugar function for: + +```js +jest.fn(function() { + return this; +}); +``` + ### `mockFn.mockReturnValue(value)` Accepts a value that will be returned whenever the mock function is called. @@ -177,15 +186,6 @@ mock.mockReturnValue(43); mock(); // 43 ``` -### `mockFn.mockReturnThis()` -Just a simple sugar function for: - -```js -jest.fn(function() { - return this; -}); -``` - ### `mockFn.mockReturnValueOnce(value)` Accepts a value that will be returned for one call to the mock function. Can be chained so that successive calls to the mock function return different values. When there are no more `mockReturnValueOnce` values to use, calls will return a value specified by `mockReturnValue`. From 10f49e98a6dae00b0c7b630d654c90a5f379c7b7 Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Sat, 7 Oct 2017 08:53:37 -0400 Subject: [PATCH 08/17] Fix test syntax --- .../with-empty-mock-name-not-called/__tests__/index.js | 3 +-- .../mock-names/with-empty-mock-name/__tests__/index.js | 3 +-- .../with-mock-name-long-not-called/__tests__/index.js | 3 +-- .../mock-names/with-mock-name-long/__tests__/index.js | 3 +-- .../mock-names/with-mock-name-not-called/__tests__/index.js | 3 +-- .../with-mock-name-short-not-called/__tests__/index.js | 3 +-- .../mock-names/with-mock-name-short/__tests__/index.js | 3 +-- integration_tests/mock-names/with-mock-name/__tests__/index.js | 3 +-- .../mock-names/without-mock-name-not-called/__tests__/index.js | 3 +-- .../mock-names/without-mock-name/__tests__/index.js | 3 +-- 10 files changed, 10 insertions(+), 20 deletions(-) diff --git a/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js b/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js index 9bad1a0e0e28..9f3bccc3f1c0 100644 --- a/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js +++ b/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js @@ -14,6 +14,5 @@ const mockFn = jest.fn(importedFn, ''); test('first test', () => { // mockFn explicitly not called to test error snapshot - expect(mockFn).toHaveBeenCalled(); - expect(mockFn.mock.calls.length).toBe(1); + expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/with-empty-mock-name/__tests__/index.js b/integration_tests/mock-names/with-empty-mock-name/__tests__/index.js index 3a0550a0f6af..080b94b43bed 100644 --- a/integration_tests/mock-names/with-empty-mock-name/__tests__/index.js +++ b/integration_tests/mock-names/with-empty-mock-name/__tests__/index.js @@ -14,6 +14,5 @@ const mockFn = jest.fn(importedFn, ''); test('first test', () => { mockFn(); - expect(mockFn).toHaveBeenCalled(); - expect(mockFn.mock.calls.length).toBe(1); + expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js b/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js index 68c462b9ec35..e1792539c0de 100644 --- a/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js @@ -13,6 +13,5 @@ const mockFn = jest.fn(importedFn).mockName('myMockedFunctionLong'); test('first test', () => { // mockFn explicitly not called to test error snapshot - expect(mockFn).toHaveBeenCalled(); - expect(mockFn.mock.calls.length).toBe(1); + expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/with-mock-name-long/__tests__/index.js b/integration_tests/mock-names/with-mock-name-long/__tests__/index.js index 4864aa7915d4..024ffc50ce8c 100644 --- a/integration_tests/mock-names/with-mock-name-long/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-long/__tests__/index.js @@ -13,6 +13,5 @@ const mockFn = jest.fn(importedFn).mockName('myMockedFunctionLong'); test('first test', () => { mockFn(); - expect(mockFn).toHaveBeenCalled(); - expect(mockFn.mock.calls.length).toBe(1); + expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js b/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js index 48cd0b26689b..31b523e5407e 100644 --- a/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js @@ -13,6 +13,5 @@ const mockFn = jest.fn(importedFn, 'myMockedFunction'); test('first test', () => { // mockFn explicitly not called to test error snapshot - expect(mockFn).toHaveBeenCalled(); - expect(mockFn.mock.calls.length).toBe(1); + expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js b/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js index a8900b68f3e9..55d07dec43cf 100644 --- a/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js @@ -12,6 +12,5 @@ const mockFn = jest.fn('myMockedFunctionShort'); test('first test', () => { // mockFn explicitly not called to test error snapshot - expect(mockFn).toHaveBeenCalled(); - expect(mockFn.mock.calls.length).toBe(1); + expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/with-mock-name-short/__tests__/index.js b/integration_tests/mock-names/with-mock-name-short/__tests__/index.js index c2fd6a0c50a8..76c21efe29b4 100644 --- a/integration_tests/mock-names/with-mock-name-short/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-short/__tests__/index.js @@ -12,6 +12,5 @@ const mockFn = jest.fn('myMockedFunctionShort'); test('first test', () => { mockFn(); - expect(mockFn).toHaveBeenCalled(); - expect(mockFn.mock.calls.length).toBe(1); + expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/with-mock-name/__tests__/index.js b/integration_tests/mock-names/with-mock-name/__tests__/index.js index e42ee9bc5824..5dcb9ceeb2e7 100644 --- a/integration_tests/mock-names/with-mock-name/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name/__tests__/index.js @@ -13,6 +13,5 @@ const mockFn = jest.fn(importedFn, 'myMockedFunction'); test('first test', () => { mockFn(); - expect(mockFn).toHaveBeenCalled(); - expect(mockFn.mock.calls.length).toBe(1); + expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/without-mock-name-not-called/__tests__/index.js b/integration_tests/mock-names/without-mock-name-not-called/__tests__/index.js index 05becdccc80f..76f3aa9f2573 100644 --- a/integration_tests/mock-names/without-mock-name-not-called/__tests__/index.js +++ b/integration_tests/mock-names/without-mock-name-not-called/__tests__/index.js @@ -13,6 +13,5 @@ const mockFn = jest.fn(importedFn); test('first test', () => { // mockFn explicitly not called to test error snapshot - expect(mockFn).toHaveBeenCalled(); - expect(mockFn.mock.calls.length).toBe(1); + expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/without-mock-name/__tests__/index.js b/integration_tests/mock-names/without-mock-name/__tests__/index.js index 37f3b4283c0d..5ffe16523bd1 100644 --- a/integration_tests/mock-names/without-mock-name/__tests__/index.js +++ b/integration_tests/mock-names/without-mock-name/__tests__/index.js @@ -13,6 +13,5 @@ const mockFn = jest.fn(importedFn); test('first test', () => { mockFn(); - expect(mockFn).toHaveBeenCalled(); - expect(mockFn.mock.calls.length).toBe(1); + expect(mockFn).toHaveBeenCalledTimes(1); }); From f8c7014fb029ca0ba3bce1d511d87e929fafbfdb Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Sat, 7 Oct 2017 09:32:17 -0400 Subject: [PATCH 09/17] Additional unit tests --- .../__tests__/mock_names.test.js | 29 +++++++++++++++++++ .../__tests__/index.js | 2 +- .../__tests__/index.js | 18 ++++++++++++ .../with-mock-name-call-times-fail/index.js | 10 +++++++ .../package.json | 6 ++++ .../__tests__/index.js | 21 ++++++++++++++ .../with-mock-name-call-times-pass/index.js | 10 +++++++ .../package.json | 6 ++++ .../__tests__/index.js | 2 +- .../__tests__/index.js | 17 +++++++++++ .../with-mock-name-not-called-fail/index.js | 10 +++++++ .../package.json | 6 ++++ .../__tests__/index.js | 17 +++++++++++ .../with-mock-name-not-called-pass/index.js | 10 +++++++ .../package.json | 6 ++++ .../__tests__/index.js | 2 +- .../__tests__/index.js | 2 +- .../__tests__/index.js | 2 +- 18 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js create mode 100644 integration_tests/mock-names/with-mock-name-call-times-fail/index.js create mode 100644 integration_tests/mock-names/with-mock-name-call-times-fail/package.json create mode 100644 integration_tests/mock-names/with-mock-name-call-times-pass/__tests__/index.js create mode 100644 integration_tests/mock-names/with-mock-name-call-times-pass/index.js create mode 100644 integration_tests/mock-names/with-mock-name-call-times-pass/package.json create mode 100644 integration_tests/mock-names/with-mock-name-not-called-fail/__tests__/index.js create mode 100644 integration_tests/mock-names/with-mock-name-not-called-fail/index.js create mode 100644 integration_tests/mock-names/with-mock-name-not-called-fail/package.json create mode 100644 integration_tests/mock-names/with-mock-name-not-called-pass/__tests__/index.js create mode 100644 integration_tests/mock-names/with-mock-name-not-called-pass/index.js create mode 100644 integration_tests/mock-names/with-mock-name-not-called-pass/package.json diff --git a/integration_tests/__tests__/mock_names.test.js b/integration_tests/__tests__/mock_names.test.js index 955d548122e5..4acc2f56be36 100644 --- a/integration_tests/__tests__/mock_names.test.js +++ b/integration_tests/__tests__/mock_names.test.js @@ -25,6 +25,35 @@ test('suite without mock name, mock not called', () => { expect(stderr).toMatch(/expect\(jest\.fn\(\)\)\.toHaveBeenCalled/); }); +test('suite with mock name, expect mock not called', () => { + const {stderr, status} = runJest('mock-names/with-mock-name-not-called-pass'); + + expect(status).toBe(0); + expect(stderr).toMatch(/PASS/); +}); + +test('suite with mock name, mock called, expect fail', () => { + const {stderr, status} = runJest('mock-names/with-mock-name-not-called-fail'); + + expect(status).toBe(1); + expect(stderr).toMatch(/expect\(myMockedFunction\)\.not\.toHaveBeenCalled/); +}); + +test('suite with mock name, mock called 5 times', () => { + const {stderr, status} = runJest('mock-names/with-mock-name-call-times-pass'); + + expect(status).toBe(0); + expect(stderr).toMatch(/PASS/); +}); + +test('suite with mock name, mock not called 5 times, expect fail', () => { + const {stderr, status} = runJest('mock-names/with-mock-name-call-times-fail'); + + expect(status).toBe(1); + expect(stderr).toMatch(/expect\(myMockedFunction\)\.toHaveBeenCalledTimes/); +}); + + test('suite with mock name, mock called', () => { const {stderr, status} = runJest('mock-names/with-mock-name'); diff --git a/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js b/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js index 9f3bccc3f1c0..12504571bed6 100644 --- a/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js +++ b/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js @@ -13,6 +13,6 @@ const importedFn = require('../'); const mockFn = jest.fn(importedFn, ''); test('first test', () => { - // mockFn explicitly not called to test error snapshot + // mockFn explicitly not called to test error output expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js b/integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js new file mode 100644 index 000000000000..7ee3c5742612 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const importedFn = require('../'); +const mockFn = jest.fn(importedFn, 'myMockedFunction'); + +test('first test', () => { + mockFn(); + mockFn(); + expect(mockFn).toHaveBeenCalledTimes(5); +}); diff --git a/integration_tests/mock-names/with-mock-name-call-times-fail/index.js b/integration_tests/mock-names/with-mock-name-call-times-fail/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-call-times-fail/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-call-times-fail/package.json b/integration_tests/mock-names/with-mock-name-call-times-fail/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-call-times-fail/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/with-mock-name-call-times-pass/__tests__/index.js b/integration_tests/mock-names/with-mock-name-call-times-pass/__tests__/index.js new file mode 100644 index 000000000000..cca0fbc29525 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-call-times-pass/__tests__/index.js @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const importedFn = require('../'); +const mockFn = jest.fn(importedFn, 'myMockedFunction'); + +test('first test', () => { + mockFn(); + mockFn(); + mockFn(); + mockFn(); + mockFn(); + expect(mockFn).toHaveBeenCalledTimes(5); +}); diff --git a/integration_tests/mock-names/with-mock-name-call-times-pass/index.js b/integration_tests/mock-names/with-mock-name-call-times-pass/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-call-times-pass/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-call-times-pass/package.json b/integration_tests/mock-names/with-mock-name-call-times-pass/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-call-times-pass/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js b/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js index e1792539c0de..07d11bb21bfe 100644 --- a/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js @@ -12,6 +12,6 @@ const importedFn = require('../'); const mockFn = jest.fn(importedFn).mockName('myMockedFunctionLong'); test('first test', () => { - // mockFn explicitly not called to test error snapshot + // mockFn explicitly not called to test error output expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/with-mock-name-not-called-fail/__tests__/index.js b/integration_tests/mock-names/with-mock-name-not-called-fail/__tests__/index.js new file mode 100644 index 000000000000..851cbbf62fe3 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-not-called-fail/__tests__/index.js @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const importedFn = require('../'); +const mockFn = jest.fn(importedFn, 'myMockedFunction'); + +test('first test', () => { + mockFn(); + expect(mockFn).not.toHaveBeenCalled(); +}); diff --git a/integration_tests/mock-names/with-mock-name-not-called-fail/index.js b/integration_tests/mock-names/with-mock-name-not-called-fail/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-not-called-fail/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-not-called-fail/package.json b/integration_tests/mock-names/with-mock-name-not-called-fail/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-not-called-fail/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/with-mock-name-not-called-pass/__tests__/index.js b/integration_tests/mock-names/with-mock-name-not-called-pass/__tests__/index.js new file mode 100644 index 000000000000..c65f4a8282f0 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-not-called-pass/__tests__/index.js @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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('../'); +const importedFn = require('../'); +const mockFn = jest.fn(importedFn, 'myMockedFunction'); + +test('first test', () => { + // mockFn explicitly not called to test error output + expect(mockFn).not.toHaveBeenCalled(); +}); diff --git a/integration_tests/mock-names/with-mock-name-not-called-pass/index.js b/integration_tests/mock-names/with-mock-name-not-called-pass/index.js new file mode 100644 index 000000000000..5233c06878bf --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-not-called-pass/index.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ + +module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-not-called-pass/package.json b/integration_tests/mock-names/with-mock-name-not-called-pass/package.json new file mode 100644 index 000000000000..b54d0a3264e7 --- /dev/null +++ b/integration_tests/mock-names/with-mock-name-not-called-pass/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "clearMocks": true + } +} diff --git a/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js b/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js index 31b523e5407e..2e37b2486a21 100644 --- a/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js @@ -12,6 +12,6 @@ const importedFn = require('../'); const mockFn = jest.fn(importedFn, 'myMockedFunction'); test('first test', () => { - // mockFn explicitly not called to test error snapshot + // mockFn explicitly not called to test error output expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js b/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js index 55d07dec43cf..dbe38f277bd9 100644 --- a/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js @@ -11,6 +11,6 @@ jest.mock('../'); const mockFn = jest.fn('myMockedFunctionShort'); test('first test', () => { - // mockFn explicitly not called to test error snapshot + // mockFn explicitly not called to test error output expect(mockFn).toHaveBeenCalledTimes(1); }); diff --git a/integration_tests/mock-names/without-mock-name-not-called/__tests__/index.js b/integration_tests/mock-names/without-mock-name-not-called/__tests__/index.js index 76f3aa9f2573..f85711e32fd9 100644 --- a/integration_tests/mock-names/without-mock-name-not-called/__tests__/index.js +++ b/integration_tests/mock-names/without-mock-name-not-called/__tests__/index.js @@ -12,6 +12,6 @@ const importedFn = require('../'); const mockFn = jest.fn(importedFn); test('first test', () => { - // mockFn explicitly not called to test error snapshot + // mockFn explicitly not called to test error output expect(mockFn).toHaveBeenCalledTimes(1); }); From 8202562aa3c0686eb7be0b800fe22b8050dd9d1f Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Sat, 7 Oct 2017 09:44:54 -0400 Subject: [PATCH 10/17] Fix linting problems --- integration_tests/__tests__/mock_names.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/integration_tests/__tests__/mock_names.test.js b/integration_tests/__tests__/mock_names.test.js index 4acc2f56be36..03f931a0e807 100644 --- a/integration_tests/__tests__/mock_names.test.js +++ b/integration_tests/__tests__/mock_names.test.js @@ -9,7 +9,6 @@ 'use strict'; const runJest = require('../runJest'); -const {extractSummary} = require('../utils'); test('suite without mock name, mock called', () => { const {stderr, status} = runJest('mock-names/without-mock-name'); @@ -53,7 +52,6 @@ test('suite with mock name, mock not called 5 times, expect fail', () => { expect(stderr).toMatch(/expect\(myMockedFunction\)\.toHaveBeenCalledTimes/); }); - test('suite with mock name, mock called', () => { const {stderr, status} = runJest('mock-names/with-mock-name'); From bb547bb75d2f9d7ff0f212a5104ca7e075ffe66d Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Sat, 7 Oct 2017 21:02:33 -0400 Subject: [PATCH 11/17] Removed functionality of having mock name passed as argument to jest.fn() and only use jest.fn().mockName() to set it --- docs/en/JestObjectAPI.md | 17 ++--------- docs/en/MockFunctionAPI.md | 9 ------ docs/en/MockFunctions.md | 8 ----- .../__tests__/mock_names.test.js | 30 ------------------- .../__tests__/index.js | 2 +- .../with-empty-mock-name/__tests__/index.js | 2 +- .../__tests__/index.js | 3 +- .../__tests__/index.js | 2 +- .../__tests__/index.js | 17 ----------- .../with-mock-name-long-not-called/index.js | 10 ------- .../package.json | 6 ---- .../with-mock-name-long/__tests__/index.js | 17 ----------- .../mock-names/with-mock-name-long/index.js | 10 ------- .../with-mock-name-long/package.json | 6 ---- .../__tests__/index.js | 2 +- .../__tests__/index.js | 2 +- .../__tests__/index.js | 2 +- .../__tests__/index.js | 16 ---------- .../with-mock-name-short-not-called/index.js | 10 ------- .../package.json | 6 ---- .../with-mock-name-short/__tests__/index.js | 16 ---------- .../mock-names/with-mock-name-short/index.js | 10 ------- .../with-mock-name-short/package.json | 6 ---- .../with-mock-name/__tests__/index.js | 2 +- packages/jest-mock/src/index.js | 12 ++------ 25 files changed, 14 insertions(+), 209 deletions(-) delete mode 100644 integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js delete mode 100644 integration_tests/mock-names/with-mock-name-long-not-called/index.js delete mode 100644 integration_tests/mock-names/with-mock-name-long-not-called/package.json delete mode 100644 integration_tests/mock-names/with-mock-name-long/__tests__/index.js delete mode 100644 integration_tests/mock-names/with-mock-name-long/index.js delete mode 100644 integration_tests/mock-names/with-mock-name-long/package.json delete mode 100644 integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js delete mode 100644 integration_tests/mock-names/with-mock-name-short-not-called/index.js delete mode 100644 integration_tests/mock-names/with-mock-name-short-not-called/package.json delete mode 100644 integration_tests/mock-names/with-mock-name-short/__tests__/index.js delete mode 100644 integration_tests/mock-names/with-mock-name-short/index.js delete mode 100644 integration_tests/mock-names/with-mock-name-short/package.json diff --git a/docs/en/JestObjectAPI.md b/docs/en/JestObjectAPI.md index 51a7ead1357f..792e8210ce4a 100644 --- a/docs/en/JestObjectAPI.md +++ b/docs/en/JestObjectAPI.md @@ -15,7 +15,7 @@ The `jest` object is automatically in scope within every test file. The methods - [`jest.clearAllTimers()`](#jestclearalltimers) - [`jest.disableAutomock()`](#jestdisableautomock) - [`jest.enableAutomock()`](#jestenableautomock) - - [`jest.fn(implementation, name)`](#jestfnimplementation) + - [`jest.fn(implementation)`](#jestfnimplementation) - [`jest.isMockFunction(fn)`](#jestismockfunctionfn) - [`jest.genMockFromModule(moduleName)`](#jestgenmockfrommodulemodulename) - [`jest.mock(moduleName, factory, options)`](#jestmockmodulename-factory-options) @@ -65,8 +65,8 @@ Returns the `jest` object for chaining. *Note: this method was previously called `autoMockOn`. When using `babel-jest`, calls to `enableAutomock` will automatically be hoisted to the top of the code block. Use `autoMockOn` if you want to explicitly avoid this behavior.* -### `jest.fn(implementation, name)` -Returns a new, unused [mock function](/jest/docs/mock-function-api.html). Optionally takes a mock implementation and/or a mock name. +### `jest.fn(implementation)` +Returns a new, unused [mock function](/jest/docs/mock-function-api.html). Optionally takes a mock implementation. ```js const mockFn = jest.fn(); @@ -77,17 +77,6 @@ Returns a new, unused [mock function](/jest/docs/mock-function-api.html). Option const returnsTrue = jest.fn(() => true); console.log(returnsTrue()); // true; ``` -With mock name: -```js - const mockFn = jest.fn('doesNothing'); - mockFn(); - expect(mockFn).toHaveBeenCalled(); - - // With a mock implementation: - const returnsTrue = jest.fn(() => true, 'returnsTrue'); - console.log(returnsTrue()); // true; -``` - ### `jest.isMockFunction(fn)` Determines if the given function is a mocked function. diff --git a/docs/en/MockFunctionAPI.md b/docs/en/MockFunctionAPI.md index 5786a8489e70..1366424f7c70 100644 --- a/docs/en/MockFunctionAPI.md +++ b/docs/en/MockFunctionAPI.md @@ -141,8 +141,6 @@ console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn()); ### `mockFn.mockName(value)` Accepts a string to use in test result output in place of "jest.fn()" to indicate which mock function is being referenced. -*Note: `jest.fn(implementation, name)` is a shorthand for `jest.fn().mockImplementation(implementation).mockName(name)`. Also, if you only provide a string to jest.fn(), it will be used as the mock name value.* - For example: ```js @@ -158,13 +156,6 @@ Will result in this error: Expected mock function to have been called. ``` -Shorthand examples: - -```js -const mockFn = jest.fn('mockedFunction'); - -const mockFn2 = jest.fn(scalar => 42 + scalar, 'add42'); -``` ### `mockFn.mockReturnThis()` Just a simple sugar function for: diff --git a/docs/en/MockFunctions.md b/docs/en/MockFunctions.md index 3a4d7ed927ad..39db52b77afe 100644 --- a/docs/en/MockFunctions.md +++ b/docs/en/MockFunctions.md @@ -230,14 +230,6 @@ const myMockFn = jest.fn() const myMockFn2 = jest.fn(scalar => 42 + scalar, 'add42'); ``` -You can also pass a mock name without providing a mock implementation: - -```javascript -const myMockFn = jest.fn('myMockFunction'); -// myMockFn(); -expect(myMockFn).toHaveBeenCalled(); -``` - ## Custom Matchers Finally, in order to make it simpler to assert how mock functions have been diff --git a/integration_tests/__tests__/mock_names.test.js b/integration_tests/__tests__/mock_names.test.js index 03f931a0e807..0ba6cf786110 100644 --- a/integration_tests/__tests__/mock_names.test.js +++ b/integration_tests/__tests__/mock_names.test.js @@ -65,33 +65,3 @@ test('suite with mock name, mock not called', () => { expect(status).toBe(1); expect(stderr).toMatch(/expect\(myMockedFunction\)\.toHaveBeenCalled/); }); - -test('suite with mock name, long form, mock called', () => { - const {stderr, status} = runJest('mock-names/with-mock-name-long'); - - expect(status).toBe(0); - expect(stderr).toMatch(/PASS/); -}); - -test('suite with mock name, long form, mock not called', () => { - const {stderr, status} = runJest('mock-names/with-mock-name-long-not-called'); - - expect(status).toBe(1); - expect(stderr).toMatch(/expect\(myMockedFunctionLong\)\.toHaveBeenCalled/); -}); - -test('suite with mock name, short form, mock called', () => { - const {stderr, status} = runJest('mock-names/with-mock-name-short'); - - expect(status).toBe(0); - expect(stderr).toMatch(/PASS/); -}); - -test('suite with mock name, short form, mock not called', () => { - const {stderr, status} = runJest( - 'mock-names/with-mock-name-short-not-called', - ); - - expect(status).toBe(1); - expect(stderr).toMatch(/expect\(myMockedFunctionShort\)\.toHaveBeenCalled/); -}); diff --git a/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js b/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js index 12504571bed6..5cd94b1c2440 100644 --- a/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js +++ b/integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js @@ -10,7 +10,7 @@ jest.mock('../'); const importedFn = require('../'); // empty mock name should result in default 'jest.fn()' output -const mockFn = jest.fn(importedFn, ''); +const mockFn = jest.fn(importedFn).mockName(''); test('first test', () => { // mockFn explicitly not called to test error output diff --git a/integration_tests/mock-names/with-empty-mock-name/__tests__/index.js b/integration_tests/mock-names/with-empty-mock-name/__tests__/index.js index 080b94b43bed..5f49fc4afef6 100644 --- a/integration_tests/mock-names/with-empty-mock-name/__tests__/index.js +++ b/integration_tests/mock-names/with-empty-mock-name/__tests__/index.js @@ -10,7 +10,7 @@ jest.mock('../'); const importedFn = require('../'); // empty mock name should result in default 'jest.fn()' output -const mockFn = jest.fn(importedFn, ''); +const mockFn = jest.fn(importedFn).mockName(''); test('first test', () => { mockFn(); diff --git a/integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js b/integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js index 7ee3c5742612..0f6be784e6b5 100644 --- a/integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js @@ -9,7 +9,8 @@ jest.mock('../'); const importedFn = require('../'); -const mockFn = jest.fn(importedFn, 'myMockedFunction'); +const mockFn = jest.fn(importedFn).mockName('myMockedFunction'); + test('first test', () => { mockFn(); diff --git a/integration_tests/mock-names/with-mock-name-call-times-pass/__tests__/index.js b/integration_tests/mock-names/with-mock-name-call-times-pass/__tests__/index.js index cca0fbc29525..1d4df77fdddc 100644 --- a/integration_tests/mock-names/with-mock-name-call-times-pass/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-call-times-pass/__tests__/index.js @@ -9,7 +9,7 @@ jest.mock('../'); const importedFn = require('../'); -const mockFn = jest.fn(importedFn, 'myMockedFunction'); +const mockFn = jest.fn(importedFn).mockName('myMockedFunction'); test('first test', () => { mockFn(); diff --git a/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js b/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js deleted file mode 100644 index 07d11bb21bfe..000000000000 --- a/integration_tests/mock-names/with-mock-name-long-not-called/__tests__/index.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. 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('../'); -const importedFn = require('../'); -const mockFn = jest.fn(importedFn).mockName('myMockedFunctionLong'); - -test('first test', () => { - // mockFn explicitly not called to test error output - expect(mockFn).toHaveBeenCalledTimes(1); -}); diff --git a/integration_tests/mock-names/with-mock-name-long-not-called/index.js b/integration_tests/mock-names/with-mock-name-long-not-called/index.js deleted file mode 100644 index 5233c06878bf..000000000000 --- a/integration_tests/mock-names/with-mock-name-long-not-called/index.js +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. 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. - * - * @flow - */ - -module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-long-not-called/package.json b/integration_tests/mock-names/with-mock-name-long-not-called/package.json deleted file mode 100644 index b54d0a3264e7..000000000000 --- a/integration_tests/mock-names/with-mock-name-long-not-called/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "jest": { - "testEnvironment": "node", - "clearMocks": true - } -} diff --git a/integration_tests/mock-names/with-mock-name-long/__tests__/index.js b/integration_tests/mock-names/with-mock-name-long/__tests__/index.js deleted file mode 100644 index 024ffc50ce8c..000000000000 --- a/integration_tests/mock-names/with-mock-name-long/__tests__/index.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. 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('../'); -const importedFn = require('../'); -const mockFn = jest.fn(importedFn).mockName('myMockedFunctionLong'); - -test('first test', () => { - mockFn(); - expect(mockFn).toHaveBeenCalledTimes(1); -}); diff --git a/integration_tests/mock-names/with-mock-name-long/index.js b/integration_tests/mock-names/with-mock-name-long/index.js deleted file mode 100644 index 5233c06878bf..000000000000 --- a/integration_tests/mock-names/with-mock-name-long/index.js +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. 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. - * - * @flow - */ - -module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-long/package.json b/integration_tests/mock-names/with-mock-name-long/package.json deleted file mode 100644 index b54d0a3264e7..000000000000 --- a/integration_tests/mock-names/with-mock-name-long/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "jest": { - "testEnvironment": "node", - "clearMocks": true - } -} diff --git a/integration_tests/mock-names/with-mock-name-not-called-fail/__tests__/index.js b/integration_tests/mock-names/with-mock-name-not-called-fail/__tests__/index.js index 851cbbf62fe3..01aada892249 100644 --- a/integration_tests/mock-names/with-mock-name-not-called-fail/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-not-called-fail/__tests__/index.js @@ -9,7 +9,7 @@ jest.mock('../'); const importedFn = require('../'); -const mockFn = jest.fn(importedFn, 'myMockedFunction'); +const mockFn = jest.fn(importedFn).mockName('myMockedFunction'); test('first test', () => { mockFn(); diff --git a/integration_tests/mock-names/with-mock-name-not-called-pass/__tests__/index.js b/integration_tests/mock-names/with-mock-name-not-called-pass/__tests__/index.js index c65f4a8282f0..abfe2fcfe604 100644 --- a/integration_tests/mock-names/with-mock-name-not-called-pass/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-not-called-pass/__tests__/index.js @@ -9,7 +9,7 @@ jest.mock('../'); const importedFn = require('../'); -const mockFn = jest.fn(importedFn, 'myMockedFunction'); +const mockFn = jest.fn(importedFn).mockName('myMockedFunction'); test('first test', () => { // mockFn explicitly not called to test error output diff --git a/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js b/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js index 2e37b2486a21..57ed74faed86 100644 --- a/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js @@ -9,7 +9,7 @@ jest.mock('../'); const importedFn = require('../'); -const mockFn = jest.fn(importedFn, 'myMockedFunction'); +const mockFn = jest.fn(importedFn).mockName('myMockedFunction'); test('first test', () => { // mockFn explicitly not called to test error output diff --git a/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js b/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js deleted file mode 100644 index dbe38f277bd9..000000000000 --- a/integration_tests/mock-names/with-mock-name-short-not-called/__tests__/index.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. 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('../'); -const mockFn = jest.fn('myMockedFunctionShort'); - -test('first test', () => { - // mockFn explicitly not called to test error output - expect(mockFn).toHaveBeenCalledTimes(1); -}); diff --git a/integration_tests/mock-names/with-mock-name-short-not-called/index.js b/integration_tests/mock-names/with-mock-name-short-not-called/index.js deleted file mode 100644 index 5233c06878bf..000000000000 --- a/integration_tests/mock-names/with-mock-name-short-not-called/index.js +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. 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. - * - * @flow - */ - -module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-short-not-called/package.json b/integration_tests/mock-names/with-mock-name-short-not-called/package.json deleted file mode 100644 index b54d0a3264e7..000000000000 --- a/integration_tests/mock-names/with-mock-name-short-not-called/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "jest": { - "testEnvironment": "node", - "clearMocks": true - } -} diff --git a/integration_tests/mock-names/with-mock-name-short/__tests__/index.js b/integration_tests/mock-names/with-mock-name-short/__tests__/index.js deleted file mode 100644 index 76c21efe29b4..000000000000 --- a/integration_tests/mock-names/with-mock-name-short/__tests__/index.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. 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('../'); -const mockFn = jest.fn('myMockedFunctionShort'); - -test('first test', () => { - mockFn(); - expect(mockFn).toHaveBeenCalledTimes(1); -}); diff --git a/integration_tests/mock-names/with-mock-name-short/index.js b/integration_tests/mock-names/with-mock-name-short/index.js deleted file mode 100644 index 5233c06878bf..000000000000 --- a/integration_tests/mock-names/with-mock-name-short/index.js +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. 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. - * - * @flow - */ - -module.exports = () => {}; diff --git a/integration_tests/mock-names/with-mock-name-short/package.json b/integration_tests/mock-names/with-mock-name-short/package.json deleted file mode 100644 index b54d0a3264e7..000000000000 --- a/integration_tests/mock-names/with-mock-name-short/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "jest": { - "testEnvironment": "node", - "clearMocks": true - } -} diff --git a/integration_tests/mock-names/with-mock-name/__tests__/index.js b/integration_tests/mock-names/with-mock-name/__tests__/index.js index 5dcb9ceeb2e7..b4970abf018e 100644 --- a/integration_tests/mock-names/with-mock-name/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name/__tests__/index.js @@ -9,7 +9,7 @@ jest.mock('../'); const importedFn = require('../'); -const mockFn = jest.fn(importedFn, 'myMockedFunction'); +const mockFn = jest.fn(importedFn).mockName('myMockedFunction'); test('first test', () => { mockFn(); diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index 89a42a5e55b0..4432f94f9b0f 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -645,19 +645,11 @@ class ModuleMockerClass { return !!fn._isMockFunction; } - fn(implementation?: any, mockName?: string): any { + fn(implementation?: any): any { const length = implementation ? implementation.length : 0; const fn = this._makeComponent({length, type: 'function'}); if (implementation) { - if (typeof implementation === 'function') { - fn.mockImplementation(implementation); - } else if (typeof implementation === 'string') { - fn.mockName(implementation); - } - } - - if (arguments.length > 1 && mockName) { - fn.mockName(mockName); + fn.mockImplementation(implementation); } return fn; } From 8a05ec45f5a0e72b2eff6e023c5d0ee359a8461d Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Sat, 7 Oct 2017 21:16:55 -0400 Subject: [PATCH 12/17] Linting fix Remove obsolete unit tests --- .../with-mock-name-call-times-fail/__tests__/index.js | 1 - packages/jest-mock/src/__tests__/jest_mock.test.js | 10 ---------- 2 files changed, 11 deletions(-) diff --git a/integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js b/integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js index 0f6be784e6b5..6b0ca540f6d6 100644 --- a/integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js +++ b/integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js @@ -11,7 +11,6 @@ jest.mock('../'); const importedFn = require('../'); const mockFn = jest.fn(importedFn).mockName('myMockedFunction'); - test('first test', () => { mockFn(); mockFn(); diff --git a/packages/jest-mock/src/__tests__/jest_mock.test.js b/packages/jest-mock/src/__tests__/jest_mock.test.js index 0cad1f6c5a0f..2b83cde0815a 100644 --- a/packages/jest-mock/src/__tests__/jest_mock.test.js +++ b/packages/jest-mock/src/__tests__/jest_mock.test.js @@ -465,16 +465,6 @@ describe('moduleMocker', () => { expect(fn.mockName()).toBe('myMockFn'); }); - test('mockName as only arg sets the mock name', () => { - const fn = jest.fn('myMockFn'); - expect(fn.mockName()).toBe('myMockFn'); - }); - - test('mockName as second arg sets the mock name', () => { - const fn = jest.fn(() => {}, 'myMockFn'); - expect(fn.mockName()).toBe('myMockFn'); - }); - describe('spyOn', () => { it('should work', () => { let isOriginalCalled = false; From b53639b21dfb7080611d651fc652d9353a5fe29d Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Sun, 8 Oct 2017 14:03:13 -0400 Subject: [PATCH 13/17] Add a test to confirm that mockReset() clears out the mockName() value. --- packages/jest-mock/src/__tests__/jest_mock.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/jest-mock/src/__tests__/jest_mock.test.js b/packages/jest-mock/src/__tests__/jest_mock.test.js index 2b83cde0815a..93db91b6313e 100644 --- a/packages/jest-mock/src/__tests__/jest_mock.test.js +++ b/packages/jest-mock/src/__tests__/jest_mock.test.js @@ -465,6 +465,15 @@ describe('moduleMocker', () => { expect(fn.mockName()).toBe('myMockFn'); }); + test('mockName gets reset by mockReset', () => { + const fn = jest.fn(); + expect(fn.mockName()).toBe('jest.fn()'); + fn.mockName('myMockFn'); + expect(fn.mockName()).toBe('myMockFn'); + fn.mockReset(); + expect(fn.mockName()).toBe('jest.fn()'); + }); + describe('spyOn', () => { it('should work', () => { let isOriginalCalled = false; From 8037727470cbe7d68a43a325517eda4b54346e90 Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Sun, 8 Oct 2017 14:03:30 -0400 Subject: [PATCH 14/17] Updated documentation to note that mockReset() clears out the mockName() value also --- docs/en/MockFunctionAPI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/MockFunctionAPI.md b/docs/en/MockFunctionAPI.md index 1366424f7c70..8ff8c9dfeff4 100644 --- a/docs/en/MockFunctionAPI.md +++ b/docs/en/MockFunctionAPI.md @@ -55,7 +55,7 @@ Beware that `mockClear` will replace `mockFn.mock`, not just [`mockFn.mock.calls The [`clearMocks`](configuration.html#clearmocks-boolean) configuration option is available to clear mocks automatically between tests. ### `mockFn.mockReset()` -Resets all information stored in the mock, including any inital implementation given. +Resets all information stored in the mock, including any initial implementation and mock name given. This is useful when you want to completely restore a mock back to its initial state. From 1aaa2e0ebd73e87832442f642a1574e9c090ab69 Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Sun, 8 Oct 2017 14:20:49 -0400 Subject: [PATCH 15/17] Added tests to ensure mockClear() & mockReset() do not affect mockName() value --- .../jest-mock/src/__tests__/jest_mock.test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/jest-mock/src/__tests__/jest_mock.test.js b/packages/jest-mock/src/__tests__/jest_mock.test.js index 93db91b6313e..181e6fc1f70b 100644 --- a/packages/jest-mock/src/__tests__/jest_mock.test.js +++ b/packages/jest-mock/src/__tests__/jest_mock.test.js @@ -474,6 +474,22 @@ describe('moduleMocker', () => { expect(fn.mockName()).toBe('jest.fn()'); }); + test('mockName is not reset by mockRestore', () => { + const fn = jest.fn(() => false); + fn.mockName('myMockFn'); + expect(fn.mockName()).toBe('myMockFn'); + fn.mockRestore(); + expect(fn.mockName()).toBe('myMockFn'); + }); + + test('mockName is not reset by mockClear', () => { + const fn = jest.fn(() => false); + fn.mockName('myMockFn'); + expect(fn.mockName()).toBe('myMockFn'); + fn.mockClear(); + expect(fn.mockName()).toBe('myMockFn'); + }); + describe('spyOn', () => { it('should work', () => { let isOriginalCalled = false; From a116716c18429d6680446ebc763edec478f7cce1 Mon Sep 17 00:00:00 2001 From: Gabriel Ricard <4550801+gricard@users.noreply.github.com> Date: Sun, 8 Oct 2017 16:34:23 -0400 Subject: [PATCH 16/17] Changed mockName() API to return the current mock name via a separate getMockName() instead of overloading mockName() function to return it when no arguments are passed --- docs/en/MockFunctionAPI.md | 3 +++ packages/expect/src/spy_matchers.js | 8 +++---- .../jest-mock/src/__tests__/jest_mock.test.js | 22 +++++++++++-------- packages/jest-mock/src/index.js | 12 +++++----- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/docs/en/MockFunctionAPI.md b/docs/en/MockFunctionAPI.md index 8ff8c9dfeff4..ae72327e73a0 100644 --- a/docs/en/MockFunctionAPI.md +++ b/docs/en/MockFunctionAPI.md @@ -18,6 +18,9 @@ Mock functions are also known as "spies", because they let you spy on the behavi ## Reference +### `mockFn.getMockName()` +Returns the mock name string set by calling `mockFn.mockName(value)` + ### `mockFn.mock.calls` An array that represents all calls that have been made into this mock function. Each call is represented by an array of arguments that were passed during the call. diff --git a/packages/expect/src/spy_matchers.js b/packages/expect/src/spy_matchers.js index ce6c27bae385..8310e0baad7d 100644 --- a/packages/expect/src/spy_matchers.js +++ b/packages/expect/src/spy_matchers.js @@ -31,7 +31,7 @@ const createToBeCalledMatcher = matcherName => (received, expected) => { const receivedIsSpy = isSpy(received); const type = receivedIsSpy ? 'spy' : 'mock function'; - const receivedName = receivedIsSpy ? 'spy' : received.mockName(); + const receivedName = receivedIsSpy ? 'spy' : received.getMockName(); const count = receivedIsSpy ? received.calls.count() : received.mock.calls.length; @@ -61,7 +61,7 @@ const createToBeCalledWithMatcher = matcherName => ( const receivedIsSpy = isSpy(received); const type = receivedIsSpy ? 'spy' : 'mock function'; - const receivedName = receivedIsSpy ? 'spy' : received.mockName(); + const receivedName = receivedIsSpy ? 'spy' : received.getMockName(); const calls = receivedIsSpy ? received.calls.all().map(x => x.args) : received.mock.calls; @@ -94,7 +94,7 @@ const createLastCalledWithMatcher = matcherName => ( const receivedIsSpy = isSpy(received); const type = receivedIsSpy ? 'spy' : 'mock function'; - const receivedName = receivedIsSpy ? 'spy' : received.mockName(); + const receivedName = receivedIsSpy ? 'spy' : received.getMockName(); const calls = receivedIsSpy ? received.calls.all().map(x => x.args) : received.mock.calls; @@ -127,7 +127,7 @@ const spyMatchers: MatchersObject = { const receivedIsSpy = isSpy(received); const type = receivedIsSpy ? 'spy' : 'mock function'; - const receivedName = receivedIsSpy ? 'spy' : received.mockName(); + const receivedName = receivedIsSpy ? 'spy' : received.getMockName(); const count = receivedIsSpy ? received.calls.count() : received.mock.calls.length; diff --git a/packages/jest-mock/src/__tests__/jest_mock.test.js b/packages/jest-mock/src/__tests__/jest_mock.test.js index 181e6fc1f70b..b012442bf368 100644 --- a/packages/jest-mock/src/__tests__/jest_mock.test.js +++ b/packages/jest-mock/src/__tests__/jest_mock.test.js @@ -458,36 +458,40 @@ describe('moduleMocker', () => { expect(moduleMocker.isMockFunction(mockFn)).toBe(true); }); + test('default mockName is jest.fn()', () => { + const fn = jest.fn(); + expect(fn.getMockName()).toBe('jest.fn()'); + }); + test('mockName sets the mock name', () => { const fn = jest.fn(); - expect(fn.mockName()).toBe('jest.fn()'); fn.mockName('myMockFn'); - expect(fn.mockName()).toBe('myMockFn'); + expect(fn.getMockName()).toBe('myMockFn'); }); test('mockName gets reset by mockReset', () => { const fn = jest.fn(); - expect(fn.mockName()).toBe('jest.fn()'); + expect(fn.getMockName()).toBe('jest.fn()'); fn.mockName('myMockFn'); - expect(fn.mockName()).toBe('myMockFn'); + expect(fn.getMockName()).toBe('myMockFn'); fn.mockReset(); - expect(fn.mockName()).toBe('jest.fn()'); + expect(fn.getMockName()).toBe('jest.fn()'); }); test('mockName is not reset by mockRestore', () => { const fn = jest.fn(() => false); fn.mockName('myMockFn'); - expect(fn.mockName()).toBe('myMockFn'); + expect(fn.getMockName()).toBe('myMockFn'); fn.mockRestore(); - expect(fn.mockName()).toBe('myMockFn'); + expect(fn.getMockName()).toBe('myMockFn'); }); test('mockName is not reset by mockClear', () => { const fn = jest.fn(() => false); fn.mockName('myMockFn'); - expect(fn.mockName()).toBe('myMockFn'); + expect(fn.getMockName()).toBe('myMockFn'); fn.mockClear(); - expect(fn.mockName()).toBe('myMockFn'); + expect(fn.getMockName()).toBe('myMockFn'); }); describe('spyOn', () => { diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index 4432f94f9b0f..2be1e576bba2 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -436,14 +436,16 @@ class ModuleMockerClass { }); f.mockName = name => { - if (arguments.length > 0 && name) { + if (name) { const mockConfig = this._ensureMockConfig(f); mockConfig.mockName = name; - return f; - } else { - const mockConfig = this._ensureMockConfig(f); - return mockConfig.mockName || 'jest.fn()'; } + return f; + }; + + f.getMockName = () => { + const mockConfig = this._ensureMockConfig(f); + return mockConfig.mockName || 'jest.fn()'; }; if (metadata.mockImpl) { From b3038760d1573806d07b4420cc47fcb840a922d1 Mon Sep 17 00:00:00 2001 From: Christoph Nakazawa Date: Sun, 8 Oct 2017 21:37:36 +0100 Subject: [PATCH 17/17] Update MockFunctionAPI.md --- docs/en/MockFunctionAPI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/MockFunctionAPI.md b/docs/en/MockFunctionAPI.md index ae72327e73a0..29c151831311 100644 --- a/docs/en/MockFunctionAPI.md +++ b/docs/en/MockFunctionAPI.md @@ -19,7 +19,7 @@ Mock functions are also known as "spies", because they let you spy on the behavi ## Reference ### `mockFn.getMockName()` -Returns the mock name string set by calling `mockFn.mockName(value)` +Returns the mock name string set by calling `mockFn.mockName(value)`. ### `mockFn.mock.calls` An array that represents all calls that have been made into this mock function. Each call is represented by an array of arguments that were passed during the call.