66 */
77
88jest . mock ( 'fs' ) ;
9+ jest . mock ( 'prettier' ) ;
910
1011const fs = require ( 'fs' ) ;
1112const path = require ( 'path' ) ;
@@ -14,6 +15,7 @@ const chalk = require('chalk');
1415const {
1516 getSnapshotData,
1617 getSnapshotPath,
18+ saveInlineSnapshots,
1719 keyToTestName,
1820 saveSnapshotFile,
1921 serialize,
@@ -26,15 +28,23 @@ const {
2628const writeFileSync = fs . writeFileSync ;
2729const readFileSync = fs . readFileSync ;
2830const existsSync = fs . existsSync ;
31+ const statSync = fs . statSync ;
32+ const readdirSync = fs . readdirSync ;
2933beforeEach ( ( ) => {
3034 fs . writeFileSync = jest . fn ( ) ;
3135 fs . readFileSync = jest . fn ( ) ;
3236 fs . existsSync = jest . fn ( ( ) => true ) ;
37+ fs . statSync = jest . fn ( filePath => ( {
38+ isDirectory : ( ) => ! filePath . endsWith ( '.js' ) ,
39+ } ) ) ;
40+ fs . readdirSync = jest . fn ( ( ) => [ ] ) ;
3341} ) ;
3442afterEach ( ( ) => {
3543 fs . writeFileSync = writeFileSync ;
3644 fs . readFileSync = readFileSync ;
3745 fs . existsSync = existsSync ;
46+ fs . statSync = statSync ;
47+ fs . readdirSync = readdirSync ;
3848} ) ;
3949
4050test ( 'keyToTestName()' , ( ) => {
@@ -84,6 +94,88 @@ test('saveSnapshotFile() works with \r', () => {
8494 ) ;
8595} ) ;
8696
97+ test ( 'saveInlineSnapshots() replaces empty function call with a template literal' , ( ) => {
98+ const filename = path . join ( __dirname , 'my.test.js' ) ;
99+ fs . readFileSync = jest . fn ( ( ) => `expect(1).toMatchInlineSnapshot();\n` ) ;
100+
101+ saveInlineSnapshots ( [
102+ {
103+ frame : { column : 11 , file : filename , line : 1 } ,
104+ snapshot : `1` ,
105+ } ,
106+ ] ) ;
107+
108+ expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
109+ filename ,
110+ 'expect(1).toMatchInlineSnapshot(`1`);\n' ,
111+ ) ;
112+ } ) ;
113+
114+ test ( 'saveInlineSnapshots() replaces existing template literal' , ( ) => {
115+ const filename = path . join ( __dirname , 'my.test.js' ) ;
116+ fs . readFileSync = jest . fn ( ( ) => 'expect(1).toMatchInlineSnapshot(`2`);\n' ) ;
117+
118+ saveInlineSnapshots ( [
119+ {
120+ frame : { column : 11 , file : filename , line : 1 } ,
121+ snapshot : `1` ,
122+ } ,
123+ ] ) ;
124+
125+ expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
126+ filename ,
127+ 'expect(1).toMatchInlineSnapshot(`1`);\n' ,
128+ ) ;
129+ } ) ;
130+
131+ test ( 'saveInlineSnapshots() replaces existing template literal with property matchers' , ( ) => {
132+ const filename = path . join ( __dirname , 'my.test.js' ) ;
133+ fs . readFileSync = jest . fn (
134+ ( ) => 'expect(1).toMatchInlineSnapshot({}, `2`);\n' ,
135+ ) ;
136+
137+ saveInlineSnapshots ( [
138+ {
139+ frame : { column : 11 , file : filename , line : 1 } ,
140+ snapshot : `1` ,
141+ } ,
142+ ] ) ;
143+
144+ expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
145+ filename ,
146+ 'expect(1).toMatchInlineSnapshot({}, `1`);\n' ,
147+ ) ;
148+ } ) ;
149+
150+ test ( 'saveInlineSnapshots() throws if frame does not match' , ( ) => {
151+ const filename = path . join ( __dirname , 'my.test.js' ) ;
152+ fs . readFileSync = jest . fn ( ( ) => 'expect(1).toMatchInlineSnapshot();\n' ) ;
153+
154+ const save = ( ) =>
155+ saveInlineSnapshots ( [
156+ {
157+ frame : { column : 2 /* incorrect */ , file : filename , line : 1 } ,
158+ snapshot : `1` ,
159+ } ,
160+ ] ) ;
161+
162+ expect ( save ) . toThrowError ( / C o u l d n ' t l o c a t e a l l i n l i n e s n a p s h o t s ./ ) ;
163+ } ) ;
164+
165+ test ( 'saveInlineSnapshots() throws if multiple calls to to the same location' , ( ) => {
166+ const filename = path . join ( __dirname , 'my.test.js' ) ;
167+ fs . readFileSync = jest . fn ( ( ) => 'expect(1).toMatchInlineSnapshot();\n' ) ;
168+
169+ const frame = { column : 11 , file : filename , line : 1 } ;
170+
171+ const save = ( ) =>
172+ saveInlineSnapshots ( [ { frame, snapshot : `1` } , { frame, snapshot : `2` } ] ) ;
173+
174+ expect ( save ) . toThrowError (
175+ / M u l t i p l e i n l i n e s n a p s h o t s f o r t h e s a m e c a l l a r e n o t s u p p o r t e d ./ ,
176+ ) ;
177+ } ) ;
178+
87179test ( 'getSnapshotData() throws when no snapshot version' , ( ) => {
88180 const filename = path . join ( __dirname , 'old-snapshot.snap' ) ;
89181 fs . readFileSync = jest . fn ( ( ) => 'exports[`myKey`] = `<div>\n</div>`;\n' ) ;
0 commit comments