import fs from 'fs' ; import * as babel from '@babel/core' ; import glob from 'glob' ; type Test = ( description : string , execute : ( ) => void ) => void ; interface Expectable { toMatchSnapshot ( ) : void ; } type Expect = ( code : string ) => Expectable ; interface TestTools { test : Test ; expect : Expect ; } export const createTestRunner = ( cwd , tools , babelOptions ) => { const { test , expect } = tools ; const transformAndSnapshot = ( description , inputCode , filename ? ) => test ( description , ( ) => { const output = babel . transformSync ( inputCode , { filename , ... babelOptions , } ) ; expect ( output ?. code ?? '' ) . toMatchSnapshot ( ) ; } ) ; return ( suite ? ) => { const fixtures = suite ? glob . sync ( `${ cwd }/fixtures/${ suite }/**/*.{js,tsx}` ) : glob . sync ( `${ cwd }/fixtures/**/*.{js,tsx}` ) ; for ( const fixture of fixtures ) { const code = fs . readFileSync ( fixture , 'utf-8' ) ; const description = code . split ( '\n' ) [ 0 ] . substring ( 3 ) ; transformAndSnapshot ( description , code , fixture ) ; } } ; } ;