const confirm = jest . fn ( ) . mockResolvedValue ( false ) ; jest . mock ( 'cli-ux' , ( ) => { const cli = jest . requireActual ( 'cli-ux' ) ; return { ... cli , default : { ... cli . default , confirm } } ; } ) ; import * as path from 'path' ; import * as fs from 'fs-extra' ; import { rm , usageAnalytics } from '../../src/utils' ; import { getCleanProjectDir , getTestOutputDir , TimeThresholds } from '../test-utils' ; const testOutputDir = getTestOutputDir ( __filename ) ; async function readConsentFile ( projectDir ) { const filePath = path . resolve ( projectDir , 'sap-cloud-sdk-analytics.json' ) ; return fs . readFile ( filePath , { encoding : 'utf8' } ) . then ( value => JSON . parse ( value ) ) ; } describe ( 'Usage Analytics Utils' , ( ) => { beforeAll ( async ( ) => { await rm ( testOutputDir ) ; } , TimeThresholds . EXTRA_SHORT ) ; it ( 'Create usage analytics consent file' , async ( ) => { const projectDir = await getCleanProjectDir ( testOutputDir , 'usage-analytics' ) ; await usageAnalytics ( projectDir , true ) ; expect ( ( await readConsentFile ( projectDir ) ) . enabled ) . toBe ( true ) ; await usageAnalytics ( projectDir , true , 'TEST' ) ; expect ( await readConsentFile ( projectDir ) ) . toEqual ( { enabled : true , salt : 'TEST' } ) ; await usageAnalytics ( projectDir , undefined ) ; expect ( ( await readConsentFile ( projectDir ) ) . enabled ) . toBe ( false ) ; await usageAnalytics ( projectDir , false ) ; expect ( ( await readConsentFile ( projectDir ) ) . enabled ) . toBe ( false ) ; } , TimeThresholds . EXTRA_SHORT ) ; } ) ;