-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for naming mocked functions (#4586)
* Added ability to specify a name for mocked functions * Added integration tests for mock name functionality * Added unit tests for mock name functionality * Linting fix * Switched from snapshot matching to regex matching because the snapshots vary by platform, apparently * Re-add accidentally removed doc section * Fix the order of replaced doc section * Fix test syntax * Additional unit tests * Fix linting problems * Removed functionality of having mock name passed as argument to jest.fn() and only use jest.fn().mockName() to set it * Linting fix Remove obsolete unit tests * Add a test to confirm that mockReset() clears out the mockName() value. * Updated documentation to note that mockReset() clears out the mockName() value also * Added tests to ensure mockClear() & mockReset() do not affect mockName() value * 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 * Update MockFunctionAPI.md
- Loading branch information
Showing
36 changed files
with
503 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* 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'); | ||
|
||
test('suite without mock name, mock called', () => { | ||
const {stderr, status} = runJest('mock-names/without-mock-name'); | ||
|
||
expect(status).toBe(0); | ||
expect(stderr).toMatch(/PASS/); | ||
}); | ||
|
||
test('suite without mock name, mock not called', () => { | ||
const {stderr, status} = runJest('mock-names/without-mock-name-not-called'); | ||
|
||
expect(status).toBe(1); | ||
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'); | ||
|
||
expect(status).toBe(0); | ||
expect(stderr).toMatch(/PASS/); | ||
}); | ||
|
||
test('suite with mock name, mock not called', () => { | ||
const {stderr, status} = runJest('mock-names/with-mock-name-not-called'); | ||
|
||
expect(status).toBe(1); | ||
expect(stderr).toMatch(/expect\(myMockedFunction\)\.toHaveBeenCalled/); | ||
}); |
18 changes: 18 additions & 0 deletions
18
integration_tests/mock-names/with-empty-mock-name-not-called/__tests__/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('../'); | ||
// empty mock name should result in default 'jest.fn()' output | ||
const mockFn = jest.fn(importedFn).mockName(''); | ||
|
||
test('first test', () => { | ||
// mockFn explicitly not called to test error output | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
}); |
10 changes: 10 additions & 0 deletions
10
integration_tests/mock-names/with-empty-mock-name-not-called/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = () => {}; |
6 changes: 6 additions & 0 deletions
6
integration_tests/mock-names/with-empty-mock-name-not-called/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
integration_tests/mock-names/with-empty-mock-name/__tests__/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('../'); | ||
// empty mock name should result in default 'jest.fn()' output | ||
const mockFn = jest.fn(importedFn).mockName(''); | ||
|
||
test('first test', () => { | ||
mockFn(); | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
}); |
10 changes: 10 additions & 0 deletions
10
integration_tests/mock-names/with-empty-mock-name/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = () => {}; |
6 changes: 6 additions & 0 deletions
6
integration_tests/mock-names/with-empty-mock-name/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
integration_tests/mock-names/with-mock-name-call-times-fail/__tests__/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('myMockedFunction'); | ||
|
||
test('first test', () => { | ||
mockFn(); | ||
mockFn(); | ||
expect(mockFn).toHaveBeenCalledTimes(5); | ||
}); |
10 changes: 10 additions & 0 deletions
10
integration_tests/mock-names/with-mock-name-call-times-fail/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = () => {}; |
6 changes: 6 additions & 0 deletions
6
integration_tests/mock-names/with-mock-name-call-times-fail/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
integration_tests/mock-names/with-mock-name-call-times-pass/__tests__/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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).mockName('myMockedFunction'); | ||
|
||
test('first test', () => { | ||
mockFn(); | ||
mockFn(); | ||
mockFn(); | ||
mockFn(); | ||
mockFn(); | ||
expect(mockFn).toHaveBeenCalledTimes(5); | ||
}); |
10 changes: 10 additions & 0 deletions
10
integration_tests/mock-names/with-mock-name-call-times-pass/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = () => {}; |
6 changes: 6 additions & 0 deletions
6
integration_tests/mock-names/with-mock-name-call-times-pass/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
integration_tests/mock-names/with-mock-name-not-called-fail/__tests__/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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).mockName('myMockedFunction'); | ||
|
||
test('first test', () => { | ||
mockFn(); | ||
expect(mockFn).not.toHaveBeenCalled(); | ||
}); |
10 changes: 10 additions & 0 deletions
10
integration_tests/mock-names/with-mock-name-not-called-fail/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = () => {}; |
6 changes: 6 additions & 0 deletions
6
integration_tests/mock-names/with-mock-name-not-called-fail/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
integration_tests/mock-names/with-mock-name-not-called-pass/__tests__/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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).mockName('myMockedFunction'); | ||
|
||
test('first test', () => { | ||
// mockFn explicitly not called to test error output | ||
expect(mockFn).not.toHaveBeenCalled(); | ||
}); |
10 changes: 10 additions & 0 deletions
10
integration_tests/mock-names/with-mock-name-not-called-pass/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = () => {}; |
6 changes: 6 additions & 0 deletions
6
integration_tests/mock-names/with-mock-name-not-called-pass/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
integration_tests/mock-names/with-mock-name-not-called/__tests__/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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).mockName('myMockedFunction'); | ||
|
||
test('first test', () => { | ||
// mockFn explicitly not called to test error output | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
}); |
10 changes: 10 additions & 0 deletions
10
integration_tests/mock-names/with-mock-name-not-called/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = () => {}; |
6 changes: 6 additions & 0 deletions
6
integration_tests/mock-names/with-mock-name-not-called/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
Oops, something went wrong.