import { parseUsageCollection } from './ts_parser' ; import * as ts from 'typescript' ; import * as path from 'path' ; import { parsedWorkingCollector } from './__fixture__/parsed_working_collector' ; import { parsedNestedCollector } from './__fixture__/parsed_nested_collector' ; import { parsedExternallyDefinedCollector } from './__fixture__/parsed_externally_defined_collector' ; import { parsedImportedUsageInterface } from './__fixture__/parsed_imported_usage_interface' ; import { parsedImportedSchemaCollector } from './__fixture__/parsed_imported_schema' ; import { parsedSchemaDefinedWithSpreadsCollector } from './__fixture__/parsed_schema_defined_with_spreads_collector' ; export function loadFixtureProgram ( fixtureName ) { const fixturePath = path . resolve ( process . cwd ( ) , 'src' , 'fixtures' , 'telemetry_collectors' , `${ fixtureName }.ts` ) ; const tsConfig = ts . findConfigFile ( './' , ts . sys . fileExists , 'tsconfig.json' ) ; if ( ! tsConfig ) { throw new Error ( 'Could not find a valid tsconfig.json.' ) ; } const program = ts . createProgram ( [ fixturePath ] , tsConfig as any ) ; const checker = program . getTypeChecker ( ) ; const sourceFile = program . getSourceFile ( fixturePath ) ; if ( ! sourceFile ) { throw Error ( 'sourceFile is undefined!' ) ; } return { program , checker , sourceFile } ; } describe ( 'parseUsageCollection' , ( ) => { it . todo ( 'throws when a function is returned from fetch' ) ; it . todo ( 'throws when an object is not returned from fetch' ) ; it ( 'throws when mapping fields is not defined' , ( ) => { const { program , sourceFile } = loadFixtureProgram ( 'unmapped_collector' ) ; expect ( ( ) => [ ... parseUsageCollection ( sourceFile , program ) ] ) . toThrowErrorMatchingSnapshot ( ) ; } ) ; it ( 'parses root level defined collector' , ( ) => { const { program , sourceFile } = loadFixtureProgram ( 'working_collector' ) ; const result = [ ... parseUsageCollection ( sourceFile , program ) ] ; expect ( result ) . toEqual ( [ parsedWorkingCollector ] ) ; } ) ; it ( 'parses collector with schema defined as union of spreads' , ( ) => { const { program , sourceFile } = loadFixtureProgram ( 'schema_defined_with_spreads_collector' ) ; const result = [ ... parseUsageCollection ( sourceFile , program ) ] ; expect ( result ) . toEqual ( [ parsedSchemaDefinedWithSpreadsCollector ] ) ; } ) ; it ( 'parses nested collectors' , ( ) => { const { program , sourceFile } = loadFixtureProgram ( 'nested_collector' ) ; const result = [ ... parseUsageCollection ( sourceFile , program ) ] ; expect ( result ) . toEqual ( [ parsedNestedCollector ] ) ; } ) ; it ( 'parses imported schema property' , ( ) => { const { program , sourceFile } = loadFixtureProgram ( 'imported_schema' ) ; const result = [ ... parseUsageCollection ( sourceFile , program ) ] ; expect ( result ) . toEqual ( parsedImportedSchemaCollector ) ; } ) ; it ( 'parses externally defined collectors' , ( ) => { const { program , sourceFile } = loadFixtureProgram ( 'externally_defined_collector' ) ; const result = [ ... parseUsageCollection ( sourceFile , program ) ] ; expect ( result ) . toEqual ( parsedExternallyDefinedCollector ) ; } ) ; it ( 'parses imported Usage interface' , ( ) => { const { program , sourceFile } = loadFixtureProgram ( 'imported_usage_interface' ) ; const result = [ ... parseUsageCollection ( sourceFile , program ) ] ; expect ( result ) . toEqual ( parsedImportedUsageInterface ) ; } ) ; it ( 'skips files that do not define a collector' , ( ) => { const { program , sourceFile } = loadFixtureProgram ( 'file_with_no_collector' ) ; const result = [ ... parseUsageCollection ( sourceFile , program ) ] ; expect ( result ) . toEqual ( [ ] ) ; } ) ; } ) ;