3
3
*
4
4
* This source code is licensed under the MIT license found in the
5
5
* LICENSE file in the root directory of this source tree.
6
- *
7
- * @flow
8
6
*/
9
7
10
8
jest . mock ( 'fs' ) ;
11
9
jest . mock ( 'prettier' ) ;
12
10
13
- const fs = require ( 'fs' ) ;
14
- const path = require ( 'path' ) ;
15
- const prettier = require ( 'prettier' ) ;
16
- const babelTraverse = require ( '@babel/traverse' ) . default ;
11
+ import fs from 'fs' ;
12
+ import path from 'path' ;
13
+ import prettier from 'prettier' ;
14
+ import babelTraverse from '@babel/traverse' ;
15
+ import { Frame } from 'jest-message-util' ;
17
16
18
- const { saveInlineSnapshots} = require ( '../inline_snapshots' ) ;
17
+ import { saveInlineSnapshots } from '../inline_snapshots' ;
19
18
20
19
const writeFileSync = fs . writeFileSync ;
21
20
const readFileSync = fs . readFileSync ;
22
21
const existsSync = fs . existsSync ;
23
22
const statSync = fs . statSync ;
24
23
const readdirSync = fs . readdirSync ;
25
24
beforeEach ( ( ) => {
26
- // $FlowFixMe mock
27
25
fs . writeFileSync = jest . fn ( ) ;
28
- // $FlowFixMe mock
29
26
fs . readFileSync = jest . fn ( ) ;
30
- // $FlowFixMe mock
31
27
fs . existsSync = jest . fn ( ( ) => true ) ;
32
- // $FlowFixMe mock
33
- fs . statSync = jest . fn ( filePath => ( {
28
+ ( fs . statSync as jest . Mock ) . mockImplementation ( filePath => ( {
34
29
isDirectory : ( ) => ! filePath . endsWith ( '.js' ) ,
35
30
} ) ) ;
36
- // $FlowFixMe mock
37
31
fs . readdirSync = jest . fn ( ( ) => [ ] ) ;
38
32
39
- prettier . resolveConfig . sync . mockReset ( ) ;
33
+ ( prettier . resolveConfig . sync as jest . Mock ) . mockReset ( ) ;
40
34
} ) ;
41
35
afterEach ( ( ) => {
42
- // $FlowFixMe mock
43
36
fs . writeFileSync = writeFileSync ;
44
- // $FlowFixMe mock
45
37
fs . readFileSync = readFileSync ;
46
- // $FlowFixMe mock
47
38
fs . existsSync = existsSync ;
48
- // $FlowFixMe mock
49
39
fs . statSync = statSync ;
50
- // $FlowFixMe mock
51
40
fs . readdirSync = readdirSync ;
52
41
} ) ;
53
42
54
43
test ( 'saveInlineSnapshots() replaces empty function call with a template literal' , ( ) => {
55
44
const filename = path . join ( __dirname , 'my.test.js' ) ;
56
- // $FlowFixMe mock
57
- fs . readFileSync = ( jest . fn (
45
+ ( fs . readFileSync as jest . Mock ) . mockImplementation (
58
46
( ) => `expect(1).toMatchInlineSnapshot();\n` ,
59
- ) : any ) ;
47
+ ) ;
60
48
61
49
saveInlineSnapshots (
62
50
[
63
51
{
64
- frame : { column : 11 , file : filename , line : 1 } ,
52
+ frame : { column : 11 , file : filename , line : 1 } as Frame ,
65
53
snapshot : `1` ,
66
54
} ,
67
55
] ,
@@ -79,25 +67,26 @@ test.each([['babylon'], ['flow'], ['typescript']])(
79
67
'saveInlineSnapshots() replaces existing template literal - %s parser' ,
80
68
parser => {
81
69
const filename = path . join ( __dirname , 'my.test.js' ) ;
82
- // $FlowFixMe mock
83
- fs . readFileSync = ( jest . fn (
70
+ ( fs . readFileSync as jest . Mock ) . mockImplementation (
84
71
( ) => 'expect(1).toMatchInlineSnapshot(`2`);\n' ,
85
- ) : any ) ;
72
+ ) ;
86
73
87
- prettier . resolveConfig . sync . mockReturnValue ( { parser} ) ;
74
+ ( prettier . resolveConfig . sync as jest . Mock ) . mockReturnValue ( { parser} ) ;
88
75
89
76
saveInlineSnapshots (
90
77
[
91
78
{
92
- frame : { column : 11 , file : filename , line : 1 } ,
79
+ frame : { column : 11 , file : filename , line : 1 } as Frame ,
93
80
snapshot : `1` ,
94
81
} ,
95
82
] ,
96
83
prettier ,
97
84
babelTraverse ,
98
85
) ;
99
86
100
- expect ( prettier . resolveConfig . sync . mock . results [ 0 ] . value ) . toEqual ( { parser} ) ;
87
+ expect (
88
+ ( prettier . resolveConfig . sync as jest . Mock ) . mock . results [ 0 ] . value ,
89
+ ) . toEqual ( { parser} ) ;
101
90
102
91
expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
103
92
filename ,
@@ -108,15 +97,14 @@ test.each([['babylon'], ['flow'], ['typescript']])(
108
97
109
98
test ( 'saveInlineSnapshots() replaces existing template literal with property matchers' , ( ) => {
110
99
const filename = path . join ( __dirname , 'my.test.js' ) ;
111
- // $FlowFixMe mock
112
- fs . readFileSync = ( jest . fn (
100
+ ( fs . readFileSync as jest . Mock ) . mockImplementation (
113
101
( ) => 'expect(1).toMatchInlineSnapshot({}, `2`);\n' ,
114
- ) : any ) ;
102
+ ) ;
115
103
116
104
saveInlineSnapshots (
117
105
[
118
106
{
119
- frame : { column : 11 , file : filename , line : 1 } ,
107
+ frame : { column : 11 , file : filename , line : 1 } as Frame ,
120
108
snapshot : `1` ,
121
109
} ,
122
110
] ,
@@ -132,16 +120,15 @@ test('saveInlineSnapshots() replaces existing template literal with property mat
132
120
133
121
test ( 'saveInlineSnapshots() throws if frame does not match' , ( ) => {
134
122
const filename = path . join ( __dirname , 'my.test.js' ) ;
135
- // $FlowFixMe mock
136
- fs . readFileSync = ( jest . fn (
123
+ ( fs . readFileSync as jest . Mock ) . mockImplementation (
137
124
( ) => 'expect(1).toMatchInlineSnapshot();\n' ,
138
- ) : any ) ;
125
+ ) ;
139
126
140
127
const save = ( ) =>
141
128
saveInlineSnapshots (
142
129
[
143
130
{
144
- frame : { column : 2 /* incorrect */ , file : filename , line : 1 } ,
131
+ frame : { column : 2 /* incorrect */ , file : filename , line : 1 } as Frame ,
145
132
snapshot : `1` ,
146
133
} ,
147
134
] ,
@@ -154,12 +141,11 @@ test('saveInlineSnapshots() throws if frame does not match', () => {
154
141
155
142
test ( 'saveInlineSnapshots() throws if multiple calls to to the same location' , ( ) => {
156
143
const filename = path . join ( __dirname , 'my.test.js' ) ;
157
- // $FlowFixMe mock
158
- fs . readFileSync = ( jest . fn (
144
+ ( fs . readFileSync as jest . Mock ) . mockImplementation (
159
145
( ) => 'expect(1).toMatchInlineSnapshot();\n' ,
160
- ) : any ) ;
146
+ ) ;
161
147
162
- const frame = { column : 11 , file : filename , line : 1 } ;
148
+ const frame = { column : 11 , file : filename , line : 1 } as Frame ;
163
149
const save = ( ) =>
164
150
saveInlineSnapshots (
165
151
[ { frame, snapshot : `1` } , { frame, snapshot : `2` } ] ,
@@ -174,12 +160,11 @@ test('saveInlineSnapshots() throws if multiple calls to to the same location', (
174
160
175
161
test ( 'saveInlineSnapshots() uses escaped backticks' , ( ) => {
176
162
const filename = path . join ( __dirname , 'my.test.js' ) ;
177
- // $FlowFixMe mock
178
- fs . readFileSync = ( jest . fn (
163
+ ( fs . readFileSync as jest . Mock ) . mockImplementation (
179
164
( ) => 'expect("`").toMatchInlineSnapshot();\n' ,
180
- ) : any ) ;
165
+ ) ;
181
166
182
- const frame = { column : 13 , file : filename , line : 1 } ;
167
+ const frame = { column : 13 , file : filename , line : 1 } as Frame ;
183
168
saveInlineSnapshots ( [ { frame, snapshot : '`' } ] , prettier , babelTraverse ) ;
184
169
185
170
expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
@@ -190,19 +175,18 @@ test('saveInlineSnapshots() uses escaped backticks', () => {
190
175
191
176
test ( 'saveInlineSnapshots() works with non-literals in expect call' , ( ) => {
192
177
const filename = path . join ( __dirname , 'my.test.js' ) ;
193
- // $FlowFixMe mock
194
- fs . readFileSync = ( jest . fn (
178
+ ( fs . readFileSync as jest . Mock ) . mockImplementation (
195
179
( ) => `expect({a: 'a'}).toMatchInlineSnapshot();\n` ,
196
- ) : any ) ;
197
- prettier . resolveConfig . sync . mockReturnValue ( {
180
+ ) ;
181
+ ( prettier . resolveConfig . sync as jest . Mock ) . mockReturnValue ( {
198
182
bracketSpacing : false ,
199
183
singleQuote : true ,
200
184
} ) ;
201
185
202
186
saveInlineSnapshots (
203
187
[
204
188
{
205
- frame : { column : 18 , file : filename , line : 1 } ,
189
+ frame : { column : 18 , file : filename , line : 1 } as Frame ,
206
190
snapshot : `{a: 'a'}` ,
207
191
} ,
208
192
] ,
0 commit comments