| 
									
										
										
										
											2019-04-19 08:53:19 -07:00
										 |  |  | import { writeFileSync, readFileSync } from 'fs'; | 
					
						
							| 
									
										
										
										
											2018-12-17 23:44:26 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const goldens: string[] = process.argv.slice(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const goldenMatcher: jasmine.CustomMatcherFactories = { | 
					
						
							|  |  |  |   toMatchGolden(util: jasmine.MatchersUtil): jasmine.CustomMatcher { | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2020-03-04 17:27:27 -08:00
										 |  |  |       compare(actual: {body?: {}}, golden: string): jasmine.CustomMatcherResult { | 
					
						
							| 
									
										
										
										
											2019-04-19 08:53:19 -07:00
										 |  |  |         if (goldens.includes(golden)) { | 
					
						
							| 
									
										
										
										
											2018-12-17 23:44:26 -08:00
										 |  |  |           console.error(`Writing golden file ${golden}`); | 
					
						
							|  |  |  |           writeFileSync(`./goldens/${golden}`, JSON.stringify(actual, null, 2)); | 
					
						
							|  |  |  |           return { pass : true }; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-19 08:53:19 -07:00
										 |  |  |         const content = readFileSync(`./goldens/${golden}`, 'utf-8'); | 
					
						
							|  |  |  |         const expected = JSON.parse(content.replace("${PWD}", process.env.PWD!)); | 
					
						
							| 
									
										
										
										
											2020-03-04 17:27:27 -08:00
										 |  |  |         const hasBody = Object.hasOwnProperty.call(expected, 'body'); | 
					
						
							|  |  |  |         const pass = hasBody ? util.equals(actual.body, expected.body) : util.equals(actual, expected); | 
					
						
							| 
									
										
										
										
											2018-12-17 23:44:26 -08:00
										 |  |  |         return { | 
					
						
							|  |  |  |           pass, | 
					
						
							| 
									
										
										
										
											2019-04-19 08:53:19 -07:00
										 |  |  |           message: `Expected ${JSON.stringify(actual, null, 2)} to match golden ` + | 
					
						
							|  |  |  |             `${JSON.stringify(expected, null, 2)}.\n` + | 
					
						
							| 
									
										
										
										
											2018-12-17 23:44:26 -08:00
										 |  |  |             `To generate new golden file, run "yarn golden ${golden}".`, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare global { | 
					
						
							|  |  |  |   namespace jasmine { | 
					
						
							|  |  |  |     interface Matchers<T> { | 
					
						
							|  |  |  |       toMatchGolden(golden: string): void | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |