import expect from '@kbn/expect' ; import fs from 'fs' ; import { join } from 'path' ; import { cloneDeep , isEqual } from 'lodash' ; const fixturesDir = join ( __dirname , '..' , 'fixtures' ) ; const excludeFieldsFrom = ( from , excluder ? ) : any => { const clone = cloneDeep ( from ) ; if ( excluder ) { excluder ( clone ) ; } return clone ; } ; export const expectFixtureEql = < T > ( data , fixtureName , excluder ? ) => { expect ( data ) . not . to . eql ( null ) ; expect ( data ) . not . to . eql ( undefined ) ; const fixturePath = join ( fixturesDir , `${ fixtureName }.json` ) ; excluder = excluder || ( ( d ) => d ) ; const dataExcluded = excludeFieldsFrom ( data , excluder ) ; expect ( dataExcluded ) . not . to . be ( undefined ) ; const fixtureExists = ( ) => fs . existsSync ( dataExcluded ) ; const fixtureChanged = ( ) => ! isEqual ( dataExcluded , excludeFieldsFrom ( JSON . parse ( fs . readFileSync ( fixturePath , 'utf8' ) ) , excluder ) ) ; if ( process . env . UPDATE_UPTIME_FIXTURES && ( ! fixtureExists ( ) || fixtureChanged ( ) ) ) { fs . writeFileSync ( fixturePath , JSON . stringify ( dataExcluded , null , 2 ) ) ; } const fileContents = fs . readFileSync ( fixturePath , 'utf8' ) ; const fixture = JSON . parse ( fileContents ) ; expect ( dataExcluded ) . to . eql ( excludeFieldsFrom ( fixture , excluder ) ) ; } ;