-
-
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
Omit empty calls array in plugin for mock functions #4848
Changes from 1 commit
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 |
---|---|---|
@@ -1,43 +1,52 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`empty with no calls mock 1`] = ` | ||
MockFunction { | ||
"calls": Array [], | ||
"name": "jest.fn()", | ||
} | ||
exports[`mock with 0 calls and default name 1`] = `[MockFunction]`; | ||
|
||
exports[`mock with 0 calls and default name in React element 1`] = ` | ||
<button | ||
onClick={[MockFunction]} | ||
> | ||
Mock me! | ||
</button> | ||
`; | ||
|
||
exports[`instantiated mock 1`] = ` | ||
MockFunction { | ||
"calls": Array [ | ||
Array [ | ||
Object { | ||
"name": "some fine name", | ||
}, | ||
exports[`mock with 1 calls and non-default name via new in object 1`] = ` | ||
Object { | ||
"fn": [MockFunction MyConstructor] | ||
calls: Array [ | ||
Array [ | ||
Object { | ||
"name": "some fine name", | ||
}, | ||
], | ||
], | ||
], | ||
"name": "jest.fn()", | ||
} | ||
`; | ||
|
||
exports[`mock with calls 1`] = ` | ||
MockFunction { | ||
"calls": Array [ | ||
exports[`mock with 1 calls in React element 1`] = ` | ||
<button | ||
onClick={ | ||
[MockFunction] | ||
calls: Array [ | ||
Array [ | ||
"Mocking you!", | ||
], | ||
] | ||
} | ||
> | ||
Mock me! | ||
</button> | ||
`; | ||
|
||
exports[`mock with 2 calls 1`] = ` | ||
[MockFunction] | ||
calls: Array [ | ||
Array [], | ||
Array [ | ||
Object { | ||
"foo": "bar", | ||
}, | ||
42, | ||
], | ||
], | ||
"name": "jest.fn()", | ||
} | ||
`; | ||
|
||
exports[`mock with name 1`] = ` | ||
MockFunction { | ||
"calls": Array [], | ||
"name": "name of mock is nice", | ||
} | ||
] | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,30 +7,122 @@ | |
*/ | ||
'use strict'; | ||
|
||
const mock = jest.fn(); | ||
const prettyFormat = require('pretty-format'); | ||
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.
|
||
|
||
afterEach(() => mock.mockReset()); | ||
const plugin = require('../mock_serializer'); | ||
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.
|
||
|
||
test('empty with no calls mock', () => { | ||
expect(mock).toMatchSnapshot(); | ||
test('mock with 0 calls and default name', () => { | ||
const fn = jest.fn(); | ||
expect(fn).toMatchSnapshot(); | ||
}); | ||
|
||
test('instantiated mock', () => { | ||
test('mock with 0 calls and default name in React element', () => { | ||
const fn = jest.fn(); | ||
const val = { | ||
$$typeof: Symbol.for('react.test.json'), | ||
children: ['Mock me!'], | ||
props: { | ||
onClick: fn, | ||
}, | ||
type: 'button', | ||
}; | ||
expect(val).toMatchSnapshot(); | ||
}); | ||
|
||
test('mock with 1 calls and non-default name via new in object', () => { | ||
const fn = jest.fn(); | ||
fn.mockName('MyConstructor'); | ||
// eslint-disable-next-line no-new | ||
new mock({name: 'some fine name'}); | ||
new fn({name: 'some fine name'}); | ||
const val = { | ||
fn, | ||
}; | ||
expect(val).toMatchSnapshot(); | ||
}); | ||
|
||
expect(mock).toMatchSnapshot(); | ||
test('mock with 1 calls in React element', () => { | ||
const fn = jest.fn(); | ||
fn('Mocking you!'); | ||
const val = { | ||
$$typeof: Symbol.for('react.test.json'), | ||
children: ['Mock me!'], | ||
props: { | ||
onClick: fn, | ||
}, | ||
type: 'button', | ||
}; | ||
expect(val).toMatchSnapshot(); | ||
}); | ||
|
||
test('mock with calls', () => { | ||
mock(); | ||
mock({foo: 'bar'}, 42); | ||
test('mock with 2 calls', () => { | ||
const fn = jest.fn(); | ||
fn(); | ||
fn({foo: 'bar'}, 42); | ||
expect(fn).toMatchSnapshot(); | ||
}); | ||
|
||
expect(mock).toMatchSnapshot(); | ||
test('indent option', () => { | ||
const fn = jest.fn(); | ||
fn({key: 'value'}); | ||
const expected = [ | ||
'[MockFunction]', | ||
'calls: Array [', | ||
'Array [', | ||
'Object {', | ||
'"key": "value",', | ||
'},', | ||
'],', | ||
']', | ||
].join('\n'); | ||
expect(prettyFormat(fn, {indent: 0, plugins: [plugin]})).toBe(expected); | ||
}); | ||
|
||
test('mock with name', () => { | ||
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 test is what I referred to in my comment |
||
const mockWithName = jest.fn().mockName('name of mock is nice'); | ||
test('min option', () => { | ||
const fn = jest.fn(); | ||
fn({key: 'value'}); | ||
const expected = '[MockFunction] calls: [[{"key": "value"}]]'; | ||
expect(prettyFormat(fn, {min: true, plugins: [plugin]})).toBe(expected); | ||
}); | ||
|
||
expect(mockWithName).toMatchSnapshot(); | ||
test('maxDepth option', () => { | ||
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. Could you add a mock function with a name to a 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. See 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. Yes, I can see it working, but it would be sweet with a test of just the name, no calls or wrapping objects |
||
const fn1 = jest.fn(); | ||
fn1.mockName('atDepth1'); | ||
fn1('primitive', {key: 'value'}); | ||
const fn2 = jest.fn(); | ||
fn2.mockName('atDepth2'); | ||
fn2('primitive', {key: 'value'}); | ||
const fn3 = jest.fn(); | ||
fn3.mockName('atDepth3'); | ||
fn3('primitive', {key: 'value'}); | ||
const val = { | ||
fn1, | ||
greaterThan1: { | ||
fn2, | ||
greaterThan2: { | ||
fn3, | ||
}, | ||
}, | ||
}; | ||
const expected = [ | ||
'Object {', // ++depth === 1 | ||
' "fn1": [MockFunction atDepth1]', | ||
' calls: Array [', // ++depth === 2 | ||
' Array [', // ++depth === 3 | ||
' "primitive",', | ||
' [Object],', // ++depth === 4 | ||
' ],', | ||
' ],', // trailing comma after property of object | ||
' "greaterThan1": Object {', // ++depth === 2 | ||
' "fn2": [MockFunction atDepth2]', | ||
' calls: Array [', // ++depth === 3 | ||
' [Array],', // ++depth === 4 | ||
' ],', // trailing comma after property of object | ||
' "greaterThan2": Object {', // ++depth === 3 | ||
' "fn3": [MockFunction atDepth3]', | ||
' calls: [Array],', // ++depth === 4 trailing comma after property of object | ||
' },', | ||
' },', | ||
'}', | ||
].join('\n'); | ||
expect(prettyFormat(val, {maxDepth: 3, plugins: [plugin]})).toBe(expected); | ||
}); |
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.
This looks pretty weird