-
-
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.
Add a serializer for jest.fn to jest-snapshot (#4668)
* Add a serializer for jest.fn to jest-snapshot * Add link to PR in changelog * Don't depend on jest-mock * Remove instances from the snapshots They don't seem as useful as I though
- Loading branch information
Showing
7 changed files
with
127 additions
and
2 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
43 changes: 43 additions & 0 deletions
43
packages/jest-snapshot/src/__tests__/__snapshots__/mock_serializer.test.js.snap
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,43 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`empty with no calls mock 1`] = ` | ||
Object { | ||
"calls": Array [], | ||
"name": "jest.fn()", | ||
} | ||
`; | ||
|
||
exports[`instantiated mock 1`] = ` | ||
Object { | ||
"calls": Array [ | ||
Array [ | ||
Object { | ||
"name": "some fine name", | ||
}, | ||
], | ||
], | ||
"name": "jest.fn()", | ||
} | ||
`; | ||
|
||
exports[`mock with calls 1`] = ` | ||
Object { | ||
"calls": Array [ | ||
Array [], | ||
Array [ | ||
Object { | ||
"foo": "bar", | ||
}, | ||
42, | ||
], | ||
], | ||
"name": "jest.fn()", | ||
} | ||
`; | ||
|
||
exports[`mock with name 1`] = ` | ||
Object { | ||
"calls": Array [], | ||
"name": "name of mock is nice", | ||
} | ||
`; |
36 changes: 36 additions & 0 deletions
36
packages/jest-snapshot/src/__tests__/mock_serializer.test.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,36 @@ | ||
/** | ||
* Copyright (c) 2016-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'; | ||
|
||
const mock = jest.fn(); | ||
|
||
afterEach(() => mock.mockReset()); | ||
|
||
test('empty with no calls mock', () => { | ||
expect(mock).toMatchSnapshot(); | ||
}); | ||
|
||
test('instantiated mock', () => { | ||
// eslint-disable-next-line no-new | ||
new mock({name: 'some fine name'}); | ||
|
||
expect(mock).toMatchSnapshot(); | ||
}); | ||
|
||
test('mock with calls', () => { | ||
mock(); | ||
mock({foo: 'bar'}, 42); | ||
|
||
expect(mock).toMatchSnapshot(); | ||
}); | ||
|
||
test('mock with name', () => { | ||
const mockWithName = jest.fn().mockName('name of mock is nice'); | ||
|
||
expect(mockWithName).toMatchSnapshot(); | ||
}); |
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,30 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; | ||
|
||
export const serialize = ( | ||
val: any, | ||
config: Config, | ||
indentation: string, | ||
depth: number, | ||
refs: Refs, | ||
printer: Printer, | ||
): string => { | ||
const mockObject = { | ||
calls: val.mock.calls, | ||
name: val.getMockName(), | ||
}; | ||
|
||
return printer(mockObject, config, indentation, depth, refs); | ||
}; | ||
|
||
export const test = (val: any) => val && !!val._isMockFunction; | ||
|
||
export default ({serialize, test}: NewPlugin); |
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