-
-
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 all commits
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 |
---|---|---|
|
@@ -7,30 +7,132 @@ | |
*/ | ||
'use strict'; | ||
|
||
const mock = jest.fn(); | ||
import prettyFormat from 'pretty-format'; | ||
|
||
afterEach(() => mock.mockReset()); | ||
import plugin from '../mock_serializer'; | ||
|
||
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 0 calls and non-default name', () => { | ||
const fn = jest.fn(); | ||
fn.mockName('MyConstructor'); | ||
expect(fn).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', () => { | ||
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 | ||
' ],', | ||
' ],', | ||
' },', | ||
' "greaterThan1": Object {', // ++depth === 2 | ||
' "fn2": [MockFunction atDepth2] {', | ||
' "calls": Array [', // ++depth === 3 | ||
' [Array],', // ++depth === 4 | ||
' ],', | ||
' },', | ||
' "greaterThan2": Object {', // ++depth === 3 | ||
' "fn3": [MockFunction atDepth3] {', | ||
' "calls": [Array],', // ++depth === 4 | ||
' },', | ||
' },', | ||
' },', | ||
'}', | ||
].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 test is what I referred to in my comment