-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for naming mocked functions #4586
Changes from 6 commits
5ed77b0
348b77f
c39eb37
76936ce
2f8066e
29623ee
9258feb
2d284a4
10f49e9
f8c7014
8202562
bb547bb
8a05ec4
b53639b
8037727
1aaa2e0
a116716
b303876
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,6 +217,27 @@ const otherObj = { | |
}; | ||
``` | ||
|
||
## Mock Names | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should it include a "availble in jest 21.4" or something? |
||
|
||
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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is wrong now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Damn. I thought I caught all the doc references. Can I still commit changes for this PR, or is that done since it's merged? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New PR is needed |
||
``` | ||
|
||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test for failing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, a test where the assertion And a test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I've added more tests to cover those. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. Thanks! |
||
* 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'); | ||
|
||
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, 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/); | ||
}); | ||
|
||
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/); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
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 = () => {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any particular reason why you do this instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No reason. I just copied what was in another test suite in there that I based mine on. |
||
}); |
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 = () => {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
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('myMockedFunctionLong'); | ||
|
||
test('first test', () => { | ||
// mockFn explicitly not called to test error snapshot | ||
expect(mockFn).toHaveBeenCalled(); | ||
expect(mockFn.mock.calls.length).toBe(1); | ||
}); |
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 = () => {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
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('myMockedFunctionLong'); | ||
|
||
test('first test', () => { | ||
mockFn(); | ||
expect(mockFn).toHaveBeenCalled(); | ||
expect(mockFn.mock.calls.length).toBe(1); | ||
}); |
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 = () => {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
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, 'myMockedFunction'); | ||
|
||
test('first test', () => { | ||
// mockFn explicitly not called to test error snapshot | ||
expect(mockFn).toHaveBeenCalled(); | ||
expect(mockFn.mock.calls.length).toBe(1); | ||
}); |
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 = () => {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
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 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); | ||
}); |
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 = () => {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"clearMocks": true | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like an accident on my part. I'll fix it.